Search code examples
maven-3maven-assembly-pluginmaven-jar-pluginpf4j

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


I have a question regarding the execution order of maven-assembly-plugin and maven-jar-plugin. What I am trying to do is to put together an uberjar file for the pf4j framework (plugin framework for java). For this to be able to do I need to first assemble all the code with dependencies and then package the jar with the manifest file which has some specific entries needed by the pf4j framework. In the issue "Changing the order of maven plugin execution" I read in the answer that order of plugins which are bound to the same phase is defined by the order declared in the pom.xml file. Now I have the following pom.xml file:

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

From the pom.xml file it should be clear that I want to run the maven-jar-plugin after maven-assembly-plugin. But instead I get the following maven output:

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

Now from the output it can be seen that maven-jar-plugin is executed before the maven-assembly-plugin, which is not what I wanted. The effect of such an order is a jar file with only MANIFEST_INF content.

Can somebody explain me what am I doing wrong here?


Solution

  • One possible solution is to make pom file in such a way:

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

    Here I added execution section to maven-jar-plugin to:

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

    It is important to set the id to default-jar, because in this case the default execution is overridden and set the phase to verify for maven-jar-plugin to be executed after maven-assembly-plugin. In case the phase was set to package then maven-jar-plugin was executed before maven-assembly-plugin.