Maven:assembly-plugin根本没有运行

时间:2012-06-07 09:32:36

标签: java maven packaging

我对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>

Edit_1 来自mvn clean package的控制台输出,正如@khmarbaise所要求的那样

[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] ------------------------------------------------------------------------

5 个答案:

答案 0 :(得分:13)

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

所以在看了你的pom后我明白了这个问题。首先,你在pluginManagement块中定义了maven-assembly-plugin,它将 NOT 执行一个插件,而且你已经将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中定义了存储库。

答案 1 :(得分:3)

我想提出一个错误的建议:我的xml看起来像这样:

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

<executions>块应该是<configuration>块的兄弟,而不是孩子。很快我修复了这个问题,我的插件就开始在我的构建中重新执行了。

答案 2 :(得分:1)

我对此问题有另一种看法,这有助于我解决%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>

如果您知道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>

答案 3 :(得分:0)

依赖关系看起来像

    <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>

答案 4 :(得分:0)

也许您可以使用 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>

此致

相关问题