Maven Tip #1 - Produce both fat and thin JAR

Sometimes you want to be able to run a Java application as a single JAR and then you need a JAR that contains everything, but at the same time you want to be able to also use it as part of a larger project. How would you deal with a situation like that? Well, with Maven the recipe below would make it possible.


    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.4.3</version>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>shade</goal>
                </goals>
                <configuration>
                    <shadedArtifactAttached>true</shadedArtifactAttached>
                    <shadedClassifierName>all</shadedClassifierName>
                    <transformers>
                        <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                            <mainClass>com.mycompany.myproduct.Main</mainClass>
                        </transformer>
                    </transformers>
                </configuration>
            </execution>
        </executions>
    </plugin>
        

And voila you have the 2 JARs you wanted.

Posted October 13, 2016

Up