3

我对 maven-assembly-plugin 和 maven-jar-plugin 的执行顺序有疑问。我想要做的是为pf4j框架(java 的插件框架)组合一个 uberjar 文件。为此,我需要首先组装所有具有依赖关系的代码,然后将 jar 与清单文件一起打包,该清单文件具有pf4j框架所需的一些特定条目。在“更改 maven 插件执行顺序”的问题 中,我在答案中读到绑定到同一阶段的插件顺序由 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 文件。

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

4

1 回答 1

2

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

于 2017-04-04T11:19:13.987 回答