【问题标题】:Maven: assembly-plugin is not run at allMaven:程序集插件根本没有运行
【发布时间】:2012-06-07 09:32:35
【问题描述】:

我对 Maven 很陌生,之前曾尝试学习过几次,但我认为没有 Maven 会更好。现在,(不)幸运的是,我必须在我想贡献的项目中使用 Maven。我此时的问题是关于包装。我想制作一个包含所有依赖项的自包含(又名“胖”)jar,一位玩过 Maven 的同事帮我解决了 pom 文件。

我已经检查了他的 pom 文件以及 SO 上的示例。 xml 看起来合法,但插件根本没有运行。请注意,我没有收到任何错误,一切都很好,除了我没有收到“fatjar”。有什么想法可能是导致此问题的原因吗?

以下是 pom.xml 的相关部分。关于<configuration><executions> 标签的定位,我看到了相互矛盾的代码示例,几乎尝试了我发现的所有变体,但仍然没有乐趣。

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <version>2.3</version>
  <executions>
    <execution>
        <id>make-jar-with-dependencies</id> <!-- this is used for inheritance merges -->
        <phase>package</phase> <!-- bind to the packaging phase -->
        <goals>
            <goal>single</goal>
        </goals>
    </execution>
  </executions>
  <configuration>
    <finalName>ProjectName</finalName>
    <appendAssemblyId>true</appendAssemblyId>
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
    <archive>
      <manifest>
        <mainClass>org.mydomain.ProjectName</mainClass>
        <addClasspath>true</addClasspath>
      </manifest>
    </archive>
  </configuration>
</plugin>

Edit_1@khmarbaise 提出的来自mvn clean package 的控制台输出

[INFO] 
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ myproject ---
[INFO] Deleting /home/user/workspace/project/target
[INFO] 
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ myproject ---
[debug] execute contextualize
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 41 resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ myproject ---
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 292 source files to /home/user/workspace/project/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ myproject ---
[debug] execute contextualize
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/user/workspace/project/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ myproject ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ myproject ---
[INFO] No tests to run.
[INFO] Surefire report directory: /home/user/workspace/project/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] --- maven-jar-plugin:2.3.2:jar (default-jar) @ myproject ---
[INFO] Building jar: /home/user/workspace/project/target/myproject-2.0.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 12.210s
[INFO] Finished at: Thu Jun 07 11:45:14 CEST 2012
[INFO] Final Memory: 18M/184M
[INFO] ------------------------------------------------------------------------

【问题讨论】:

  • 你怎么称呼这个项目?
  • 我不是 100% 确定您的意思,但如果我理解正确,您会问我如何运行该项目?我通过终端和 Eclipse 内部都试过了。没有区别...
  • 只需检查您是否只是从控制台执行 mvn clean package。可以在控制台上发布执行过程中的输出吗?
  • 您是否在 pluginManagement 块中定义了程序集插件?您可以发布完整的 pom(可能在 pastebin 上)吗?或者你有多模块构建?
  • 这里是 POM 文件的大部分 pastebin.com/KxAxwJfh 请注意,我删除了一些与原作者隐私相关的 java 依赖项,我很确定它们与手头的问题无关,所以应该问题不大。

标签: java maven packaging


【解决方案1】:

我建议使用maven-shade-plugin 创建一个 uberjar,因为它的意图正是这个目的。您也可以使用 maven-assembly-plugin 来做到这一点。

因此,在查看您的 pom 后,我明白了问题所在。首先,您在 pluginManagement 块中定义了 maven-assembly-plugin,它将 执行插件,此外,您已将 maven-assembly-plugin 定义为单独的依赖项,这是多余的。这意味着只需从您的 pom 中删除以下内容:

<dependency>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-assembly-plugin</artifactId>
   <version>2.3</version>
   <type>maven-plugin</type>
</dependency>

您应该像这样定义 maven-assembler-plugin:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            ...all your configuration here..
        </plugin>
    </plugins>
</build>

此外,我还看到了许多应由存储库管理器处理的存储库定义。关于您的 pom 中的存储库,我建议您阅读sonatype information,此外,您应该考虑其他人将使用代理等背后的项目,而不是他必须更改您的 pom 才能让我工作或无法使用它,因为您定义了他无法访问的 pom 中的存储库。

【讨论】:

  • 上述插件有什么区别吗?顺便说一句,汇编插件应该也可以工作,就像你提到的那样,所以我不确定切换到不同的插件是否会是这里的神奇解决方案。
  • [re:edit] 将插件作为依赖项是解决问题的绝望尝试,我可以证明它在此之前也不起作用。您能否详细说明您关于存储库定义的陈述。 (我没有编写大部分 pom 文件,所以我不确定是我参与了一些还是项目附带的东西)
  • 刚试了一下,确实是pluginManagement标签才是症结所在。奇怪的是,它在项目附带的原始 pom 文件中,猜想开发人员还没有真正尝试过...... :)
  • 带有嵌套插件的 pluginManagement 标签是一个很好的选择。必须注意有一个单独的 部分与 pluginManagement 处于同一级别以激活插件。该嵌套可以通过 mvn 项目创建者进入 pom(例如 mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false )这很好本身,但不是准备执行插件的地方。
【解决方案2】:

我对这个问题有另一种看法,它帮助我解决了 %subj%。如果您挖掘有关如何制作可执行 jar 和带有依赖项的 jar 的文章,它们通常会提供代码 sn-ps,新手试图将其复制粘贴到他们的 pom.xml 中。那些对 pom.xml 文件(又名我)的实际结构不太了解的人,这是一项相当艰巨的任务。我以这个(不工作)解决方案结束:

 <project>
  ......

 <build>
   <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>3.2.0</version>
          <configuration>
            ............
          </configuration>

          <executions>
            ..............
          </executions>
        </plugin>
      </plugins>
   </pluginManagement>
  </build>
</project>

如果你知道pluginManagement 做了什么,你就不会犯同样的错误。但对我来说,这是一个很大的未知数,如果您从未使用过父项目,请确保您不会落入同一个陷阱。经过一些调整,这里是一个有效的 maven-assembly-plugin 设置(在 pom.xml 中有正确的上下文)

<?xml version="1.0" encoding="UTF-8"?>

<project>
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.blah.blah</groupId>
  <artifactId>some-artifact</artifactId>
  <version>1.0-SNAPSHOT</version>

  <dependencies>
   ...
  </dependencies>

  <build>
      <plugins>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>3.2.0</version>
          <configuration>
            <appendAssemblyId>false</appendAssemblyId> <!-- nicer looking jar name -->
            <archive>
              <manifest>
                <addClasspath>true</addClasspath>
                <mainClass>com.blah.blah.YourMainClass</mainClass>
              </manifest>
            </archive>
            <descriptorRefs>
              <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
          </configuration>

          <executions>
            <execution>
              <id>make-assembly</id> 
              <phase>package</phase>
              <goals>
                <goal>single</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
      </plugins>
  </build>
</project>

【讨论】:

  • 非常感谢大卫 :) 我尝试多次更改 pom.xml 和 assembly.xml 中的所有内容,但仍然没有得到 ZIP,真的很烦!再次感谢!
【解决方案3】:

我想再提供一个关于我遇到的错误的建议:我的 xml 看起来像这样:

<plugins>
    <plugin>
        <groupId>org.maven...</groupId>
        <artifactId>myArtifact</artifact>
        <configuration>
            <descriptorRefs>....</descriptorRefs>
            ...
            <executions>
                <execution>
                    <phase>package</phase>
                    ....
                </execution>
            </executions>
        </configuration>
    </plugin>
</plugins>

&lt;executions&gt; 块应该是&lt;configuration&gt; 块的兄弟,而不是子块。修复该问题后,我的插件再次开始在我的构建中执行。

【讨论】:

【解决方案4】:

也许您可以改用 shade 插件(这是我使用的那个)。

请注意,依赖项包含在最终 JAR 中。

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <finalName>standalone-${artifactId}</finalName>
                <archive>
                    <manifest>
                        <mainClass>com.mypckg.Launcher</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

问候

【讨论】:

  • 糟糕,刚刚看到 khmarbaise 的答案,非常接近。
【解决方案5】:

依赖看起来像

    <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptors>
                    <descriptor>src/main/assembly/assembly.xml</descriptor>
                </descriptors>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>attached</goal>
                    </goals>
                </execution>
            </executions>
    </plugin>

你还需要定义一个 assembly.xml ,这里是一个例子

<assembly>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/lib</outputDirectory>
        </dependencySet>
    </dependencySets>
    <fileSets>
        <!-- move all the .jar from target to /lib -->
        <fileSet>
            <directory>target</directory>
            <outputDirectory>/lib</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
            <!--  include *.jar places the same version of >App.jar
            twice in the lib, we need remove one -->
            <excludes>
                <exclude>App*.jar</exclude>
            </excludes>

        </fileSet>
        <!-- move .bat from scripts to /scripts -->
        <fileSet>
            <directory>src/main/assembly/scripts</directory>
            <outputDirectory>/bin</outputDirectory>
            <includes>
                <include>*.bat</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

【讨论】:

  • 不想听起来不受欢迎,但我很确定在这种情况下不需要单独的 XML,因为我在同事的计算机上看到相同的代码适用于不同的项目。同样在插件主页上,他们也没有提到任何关于单独的 xml 文件的内容......
  • 附加目标被标记为已弃用,仅应在从命令行调用时使用!
  • 这是一种让它工作的方法,也是我个人认为让它工作的最好方法(ofc 还有其他方法)。不知道他们为什么不推荐使用它,干净且易于读。它完成了这项工作
猜你喜欢
  • 2010-12-12
  • 2010-11-07
  • 2020-06-29
  • 2010-12-19
  • 1970-01-01
  • 2021-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多