maven-assembly-plugin和maven-jar-plugin的执行顺序

Execution order of maven-assembly-plugin and maven-jar-plugin

我对 maven-assembly-plugin 和 maven-jar-plugin 的执行顺序有疑问。我想做的是为 pf4j 框架(java 的插件框架)整理一个 uberjar 文件。为此,我需要首先 assemble 所有具有依赖关系的代码,然后将 jar 与清单文件打包在一起,清单文件包含 pf4j 框架所需的一些特定条目. 在本期"Changing the order of maven plugin execution" 我在答案中读到,绑定到同一阶段的插件顺序由 pom.xml 文件中声明的顺序定义。现在我有以下 pom.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <groupId>org.assembly.test</groupId>
   <artifactId>assembly-test</artifactId>
   <version>1.0-SNAPSHOT</version>
   <packaging>jar</packaging>


   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <java.version>1.8</java.version>

      <plugin.id>some-plugin</plugin.id>
      <plugin.class>org.assembly.test.Main</plugin.class>
      <plugin.version>0.0.1</plugin.version>
      <plugin.provider>Developers</plugin.provider>
      <plugin.dependencies />
   </properties>

   <build>
      <plugins>

         <!-- Compiler Plugin -->
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
               <source>${java.version}</source>
               <target>${java.version}</target>
            </configuration>
         </plugin>

         <!-- Assembly Plugin -->
         <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.0.0</version>
            <configuration>
               <filters>
                  <filter>src/main/assembly/filter.properties</filter>
               </filters>
               <descriptors>
                  <descriptor>src/main/assembly/assembly.xml</descriptor>
               </descriptors>
            </configuration>
            <executions>
               <execution>
                  <id>make-assembly</id> 
                  <phase>package</phase>
                  <goals>
                     <goal>single</goal>
                  </goals>
               </execution>
            </executions>
         </plugin>

         <!-- Jar Plugin -->
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
               <classesDirectory>${project.build.directory}/${project.artifactId}-${project.version}-distro</classesDirectory>
               <archive>
                  <manifestEntries>
                     <Plugin-Id>${plugin.id}</Plugin-Id>
                     <Plugin-Class>${plugin.class}</Plugin-Class>
                     <Plugin-Version>${plugin.version}</Plugin-Version>
                     <Plugin-Provider>${plugin.provider}</Plugin-Provider>
                     <Plugin-Dependencies>${plugin.dependencies}</Plugin-Dependencies>
                  </manifestEntries>
               </archive>
            </configuration>
         </plugin>
      </plugins>
   </build>

   <dependencies>

      <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
      <dependency>
         <groupId>org.apache.commons</groupId>
         <artifactId>commons-lang3</artifactId>
         <version>3.5</version>
      </dependency>

      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>4.11</version>
         <scope>test</scope>
      </dependency>
   </dependencies>
</project>

从 pom.xml 文件中可以清楚地看出,我想在 maven- 之后 运行 maven-jar-plugin程序集插件。但是我得到了以下 Maven 输出:

[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for org.assembly.test:assembly-test:jar:1.0-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-jar-plugin is missing. @ line 82, column 12
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building assembly-test 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ assembly-test ---
[INFO] Deleting D:\JavaTools\project_btc\projects\assembly-test\target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ assembly-test ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 3 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ assembly-test ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 5 source files to D:\JavaTools\project_btc\projects\assembly-test\target\classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ assembly-test ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ assembly-test ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ assembly-test ---
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ assembly-test ---
[WARNING] JAR will be empty - no content was marked for inclusion!
[INFO] Building jar: D:\JavaTools\project_btc\projects\assembly-test\target\assembly-test-1.0-SNAPSHOT.jar
[INFO] 
[INFO] --- maven-assembly-plugin:3.0.0:single (make-assembly) @ assembly-test ---
[INFO] Reading assembly descriptor: src/main/assembly/assembly.xml
[ERROR] OS=Windows and the assembly descriptor contains a *nix-specific root-relative-reference (starting with slash) /
[INFO] Copying files to D:\JavaTools\project_btc\projects\assembly-test\target\assembly-test-1.0-SNAPSHOT-distro
[WARNING] Assembly file: D:\JavaTools\project_btc\projects\assembly-test\target\assembly-test-1.0-SNAPSHOT-distro is not a regular file (it may be a directory). It cannot be attached to the project build for installation or deployment.
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ assembly-test ---
[INFO] Installing D:\JavaTools\project_btc\projects\assembly-test\target\assembly-test-1.0-SNAPSHOT.jar to D:\JavaTools\maven_repository\org\assembly\test\assembly-test.0-SNAPSHOT\assembly-test-1.0-SNAPSHOT.jar
[INFO] Installing D:\JavaTools\project_btc\projects\assembly-test\pom.xml to D:\JavaTools\maven_repository\org\assembly\test\assembly-test.0-SNAPSHOT\assembly-test-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.376 s
[INFO] Finished at: 2017-04-04T06:04:11+02:00
[INFO] Final Memory: 21M/168M
[INFO] ------------------------------------------------------------------------

现在从输出可以看出maven-jar-plugin在maven-assembly-plugin之前执行,这不是我想要的。这样命令的效果是一个只有MANIFEST_INF内容的jar文件。

有人可以解释一下我做错了什么吗?

一个可能的解决方案是这样制作pom文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <groupId>org.assembly.test</groupId>
   <artifactId>assembly-test</artifactId>
   <version>1.0-SNAPSHOT</version>
   <packaging>jar</packaging>


   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <java.version>1.8</java.version>

      <plugin.id>some-plugin</plugin.id>
      <plugin.class>org.assembly.test.Main</plugin.class>
      <plugin.version>0.0.1</plugin.version>
      <plugin.provider>Developers</plugin.provider>
      <plugin.dependencies />
   </properties>

   <build>
      <plugins>

         <!-- Compiler Plugin -->
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
               <source>${java.version}</source>
               <target>${java.version}</target>
            </configuration>
         </plugin>

         <!-- Assembly Plugin -->
         <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.0.0</version>
            <configuration>
               <filters>
                  <filter>src/main/assembly/filter.properties</filter>
               </filters>
               <descriptors>
                  <descriptor>src/main/assembly/assembly.xml</descriptor>
               </descriptors>
            </configuration>
            <executions>
               <execution>
                  <id>make-assembly</id> 
                  <phase>package</phase>
                  <goals>
                     <goal>single</goal>
                  </goals>
               </execution>
            </executions>
         </plugin>

         <!-- Jar Plugin -->
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
               <classesDirectory>${project.build.directory}/${project.artifactId}-${project.version}-distro</classesDirectory>
               <archive>
                  <manifest>
                     <mainClass>org.assembly.test.Main</mainClass>
                  </manifest>
                  <manifestEntries>
                     <Plugin-Id>${plugin.id}</Plugin-Id>
                     <Plugin-Class>${plugin.class}</Plugin-Class>
                     <Plugin-Version>${plugin.version}</Plugin-Version>
                     <Plugin-Provider>${plugin.provider}</Plugin-Provider>
                     <Plugin-Dependencies>${plugin.dependencies}</Plugin-Dependencies>
                  </manifestEntries>
               </archive>
            </configuration>
            <executions>
               <execution>
                  <id>default-jar</id> 
                  <phase>verify</phase>
                  <goals>
                     <goal>jar</goal>
                  </goals>
               </execution>
            </executions>
         </plugin>
      </plugins>
   </build>

   <dependencies>

      <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
      <dependency>
         <groupId>org.apache.commons</groupId>
         <artifactId>commons-lang3</artifactId>
         <version>3.5</version>
      </dependency>

      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>4.11</version>
         <scope>test</scope>
      </dependency>
   </dependencies>
</project>

这里我将maven-jar-plugin的执行部分添加到:

<executions>
  <execution>
    <id>default-jar</id> 
    <phase>verify</phase>
    <goals>
      <goal>jar</goal>
    </goals>
  </execution>
</executions>

重要的是将id设置为default-jar,因为在这种情况下默认执行被覆盖并将 phase 设置为 verify 以便 maven-jar-plugin 之后执行maven-assembly-plugin。如果 phase 设置为 package 然后 maven-jar-plugin 被执行在 maven-assembly-plugin.

之前