0

I have a multi-module maven project (say project-xxx). Lets say it consists of 5 modules:

project-xxx (war)
--> module-1 (jar)
--> module-2 (jar)
--> module-3 (jar)
--> module-4 (jar)
--> module-5 (jar)

When the maven project is built, the war file is generated and it includes the jar files of the 5 modules as well.

Now, for a different purpose (i.e., deploy to a distributed cache, so we can run queries from command-line), I want to generate a single `jar' file as well which includes the java classes from all modules. I'm aware that it is against maven's philosophy to generate more than one artifact and I read this blog post and a few other questions on SO.

But creating this single jar file would greatly simplify a few other things in my project. What would be the best approach to go about generating this single jar file?

Community
  • 1
  • 1
Anand Jayabalan
  • 12,294
  • 5
  • 41
  • 52

2 Answers2

1

I think you should consider introducing first separate profiles, so that profile1 would contain the configuration to result a proper war packaging. Profile2 could contain configuration using the maven-shade-plugin, in order to create an UBER jar out of your existing modules. Profiles are a very clean and maven-ish way to split different concerns.

For maven profiles see here. For the maven-shade-plugin see here.

Hope that helps.

javapapo
  • 1,342
  • 14
  • 26
  • For my requirements, creating a new module as described in the [answer](https://stackoverflow.com/a/34536316/1128163) by @heenenee seems to be a much better fit. Thanks for the `shade` plugin pointer. – Anand Jayabalan Dec 30 '15 at 22:26
1

I am very much in favor of the one artifact per Maven project convention. That being said, if you need a single artifact that contains all of the classes for all of the modules, then make a dedicated single project to do so:

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>your-group-id</groupId>
    <artifactId>one-jar-to-rule-them-all</artifactId>
    <version>your-version</version>
    <dependencies>
        <dependency>
            <groupId>your-group-id</groupId>
            <artifactId>module-1</artifactId>
            <version>your-version</version>
        </dependency>
        .
        .
        .
        <dependency>
            <groupId>your-group-id</groupId>
            <artifactId>module-5</artifactId>
            <version>your-version</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.2</version>
                <configuration>
                    <!--
                        This restricts the jar to classes from your group;
                        you may or may not want to do this.
                    -->
                    <artifactSet>
                        <includes>
                            <include>your-group-id</include>
                        </includes>
                    </artifactSet>
                    <createDependencyReducedPom>true</createDependencyReducedPom>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

This example project depends on each module and then uses the maven-shade-plugin to combine all of those modules together into a single jar artifact. You could also make this a child module of your parent project-xxx so it gets built by the reactor. This way, you can have both a war and an uber jar, but still keep the modularity of a standard Maven build.

heenenee
  • 19,914
  • 1
  • 60
  • 86