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

时间:2017-04-04 04:33:01

标签: maven-3 maven-assembly-plugin maven-jar-plugin pf4j

我对maven-assembly-plugin和maven-jar-plugin的执行顺序有疑问。我想要做的是为 pf4j 框架(java的插件框架)整理一个uberjar文件。为此,我需要首先使用依赖项汇编所有代码,然后使用清单文件打包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-assembly-plugin 之后运行 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\1.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\1.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文件。

有人可以解释一下我在这里做错了吗?

1 个答案:

答案 0 :(得分:1)

一种可能的解决方案是以这种方式制作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 maven-assembly-plugin 之后执行 验证 maven-jar-plugin 。如果phase设置为 package ,那么 maven-jar-plugin maven-assembly-plugin之前执行