Maven:assembly-plugin根本没有运行

pos*_*def 4 java packaging maven

我对Maven很陌生,之前曾尝试过几次学习,并认为没有Maven我会更好.现在,(联合国)幸运的是,我必须在一个我想贡献的项目中使用Maven.我在这一点上的问题是关于包装.我想制作一个包含所有依赖项的自包含(又称"胖")jar,并且与Maven一起玩的同事帮我解决了pom文件.

我检查了他的pom文件,以及SO上的示例.xml看起来合法,但插件根本不运行.请注意,我没有错误,一切都很顺利,除了我没有"胖子".任何想法可能是这个问题的原因?

下面是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>
Run Code Online (Sandbox Code Playgroud)

按@khmarbaisemvn 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] ------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

khm*_*ise 14

我建议使用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>
Run Code Online (Sandbox Code Playgroud)

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

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            ...all your configuration here..
        </plugin>
    </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

此外,我已经看到许多存储库定义,应该由存储库管理器来处理.关于你的pom中的存储库我可以建议阅读声纳类型信息,此外你应该考虑其他人将使用代理等背后的项目,而不是他必须改变你的pom让我工作或不能使用它导致你定义的存储库在你的pom他无法达到.

  • 刚刚试了一下,确实是 pluginManagement 标签才是关键。奇怪的是,它在项目附带的原始 pom 文件中,猜猜开发人员还没有真正尝试过...... :) (2认同)

Dav*_*dky 11

我对这个问题有另一种看法,这帮助我解决了%subj%。如果您挖掘有关如何制作可执行 jar 和具有依赖项的 jar 的文章,它们通常会提供代码片段,新手会尝试将其复制粘贴到其 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>
Run Code Online (Sandbox Code Playgroud)

如果您知道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>
Run Code Online (Sandbox Code Playgroud)

  • 非常感谢 David :) 我多次尝试更改 pom.xml 和 assembly.xml 中的所有内容,但仍然没有获得 ZIP,真的很恼火!再次感谢! (2认同)