Creating Multiple Executable Jars using Maven

Introduction

You will see in this post how to create multiple executable jars using maven build tool. You have seen how to create executable jar using maven build tool. I had created only single executable jar file in previous example but here I will create multiple jars.

Related Posts:

Prerequisites

Apache Maven 3.6.3, Java at least 1.8

Multiple Executable JARs

To create executable jar you need maven-assembly-plugin. You will see how to use this maven plugin to create multiple executable jars.

You need also maven-compiler-plugin to compile Java files in your project.

I will give you example in a pom.xml file but you can adjust the below pom.xml file according to your project.

Look carefully, the below configurations for multiple executable jars generation are slightly different.

Here you need to put all configurations for each jar generation inside the <execution> tag inside <executions> section.

For each <execution> tag you need to give unique id to separate from each other.

If you want you may generate more executable jars by adding more <execution> tag.

<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>com.roytuts</groupId>
    <artifactId>executablejar</artifactId>
    <packaging>jar</packaging>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <junit.version>4.12</junit.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
	    <plugin>
              <artifactId>maven-assembly-plugin</artifactId>
				<executions>
					<execution>
						<id>build-a</id>
						<configuration>
							<archive>
								<manifest>
									<mainClass>main class name with full package</mainClass>
								</manifest>
							</archive>
							<descriptorRefs>
								<descriptorRef>jar-with-dependencies</descriptorRef>
							</descriptorRefs>
							<!-- <appendAssemblyId>false</appendAssemblyId> -->
							<finalName>final output name which will be the jar name</finalName>
						</configuration>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
					<execution>
						<id>build-b</id>
						<configuration>
							<archive>
								<manifest>
									<mainClass>another main class name with full package</mainClass>
								</manifest>
							</archive>
							<descriptorRefs>
								<descriptorRef>jar-with-dependencies</descriptorRef>
							</descriptorRefs>
							<!-- <appendAssemblyId>false</appendAssemblyId> -->
							<finalName>final output name which will be the jar name</finalName>
						</configuration>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
					<execution>
						<id>build-c</id>
						<configuration>
							<archive>
								<manifest>
									<mainClass>other main class name with full package</mainClass>
								</manifest>
							</archive>
							<descriptorRefs>
								<descriptorRef>jar-with-dependencies</descriptorRef>
							</descriptorRefs>
							<!-- <appendAssemblyId>false</appendAssemblyId> -->
							<finalName>final output name which will be the jar name</finalName>
						</configuration>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
            </plugin>
        </plugins>
    </build>
</project>

The above pom.xml file configurations will generate multiple executable jar files for different main classes found in the Java based application.

Along with multiple generated executable jar files you will also find maven generates default non-executable jar file. If you do not need such default non-executable jar file then you can remove this default generated jar file.

Source Code

Download

1 thought on “Creating Multiple Executable Jars using Maven

Leave a Reply

Your email address will not be published. Required fields are marked *