Introduction

We will see in this post how to create multiple executable jars using maven build tool. We have seen how to create executable jar using maven build tool. We had created only single executable jar file in previous example but here we will create multiple jars. You can also read how to remove default jar created by maven build tool.

Prerequisites

Apache Maven 3.6.0, Java 1.8

Example

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

We need also maven-compiler-plugin to compile Java files in our 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 we 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.jeejava</groupId>
    <artifactId>executablejar</artifactId>
    <packaging>jar</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>executable jar</name>
    <url>http://maven.apache.org</url>

    <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.

Thanks for reading.

Tags:

Leave a Reply

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