Zoltán
Zoltán

Reputation: 22206

Maven custom packaging

I am using a library (RootBeer), which requires an additional build step: after creating my JAR, I have to run the RootBeer JAR with my JAR as its parameter to create the final RootBeer-enabled JAR.

E.g., if my jar is myjar.jar, this is how I have to create the final artefact myjar-final.jar with RootBeer:

java -jar rootbeer.jar myjar.jar myjar-final.jar

I would like to know if there is a mechanism in Maven which would enable me to build an artifact in this way.

Right now I'm using the gmaven-plugin with a Groovy script, but this just feels too hacky, and I'm pretty sure I couldn't use the resulting artefact as a Maven dependency in other projects:

<plugin>
   <groupId>org.codehaus.groovy.maven</groupId>
   <artifactId>gmaven-plugin</artifactId>
   <executions>
      <execution>
         <id>groovy-magic</id>
         <phase>package</phase>
         <goals>
            <goal>execute</goal>
         </goals>
         <configuration>
             <source>
                println """java -jar target/rootbeer-1.2.0.jar target/myjar.jar target/myjar-final.jar"""
                   .execute().in.eachLine {
                      line -> println line
                }
             </source>
          </configuration>
       </execution>
    </executions>
</plugin>

Any suggestions?

Upvotes: 1

Views: 171

Answers (1)

khmarbaise
khmarbaise

Reputation: 97557

You can use the exec-maven-plugin to execute the final step what you have implemented into Groovy furthermore you need to added build-helper-maven-plugin to add the supplemental artifact to Maven to get it deployed with the rest of your artifacts.

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.3.2</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>java</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <!-- The main class of rootbeer.jar -->
                <mainClass>org.trifort.rootbeer.entry.Main</mainClass>
                <!-- by setting equal source and target jar names, the main artefact is
                replaced with the one built in the final step, which is exactly what I need. -->
                <arguments>
                    <argument>${project.build.directory}/${project.artifactId}.jar</argument>
                    <argument>${project.build.directory}/${project.artifactId}.jar</argument>
                    <argument>-nodoubles</argument>
                </arguments>
            </configuration>
        </plugin>

Upvotes: 3

Related Questions