Question

I followed the example in Building a fat jar using maven and now I can run the following to build/test and install my jars.

mvn clean compile install

However, install now takes a lot longer because we are now building a fat jar. Is it possible to have two versions of install where one just builds jars without dependencies and the other does that and in addition builds the fat jar, like:

mvn clean compile install
mvn clean compile install-fatjar

I know install-fatjar is not a valid phase but just want to give an idea of what I'm trying to accomplish, i.e. a conditional install where the fat jar is built only when an option is provided.

Was it helpful?

Solution

Create a profile for the fat jar and configure the maven assembly plugin to create the fat jar in this profile.

For example use this profile:

<profiles>
    <profile>
        <id>fatjar</id>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <configuration>
                        <archive>
                            <manifest>
                                <mainClass>fully.qualified.MainClass</mainClass>
                            </manifest>
                        </archive>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>       
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

Then you can build it by activating the profile

mvn -P fatjar clean install

where fatjar is the profile id.

Multiple profiles can also be activated

mvn -P fatjar,release install

OTHER TIPS

Normally we don't use the fat-jar (or, uber-jar) as the main artifact.

You can make use of Assembly or Shade plugin to create a variation of the artifact (with a different classifier) which is a uber-jar.

It is strongly recommended that you still keep your "normal" artifact as is. If you want the uber-jar only occasionally, put the use of assembly/shade plugin for "uber-jar" creation in a profile, and activate the profile whenever you want. However, this should still generate you an "extra" uber-jar instead of making your main artifact a uber-jar.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top