Removing Default Jar created by Maven Build

Introduction

Here you will see an example on how to remove default jar by maven build tool. Generally this may be required in a situation where you are generating executable jar or executable jars and your application does not require additional non-executable jar generated by maven build. Then you can remove such jar from the target folder.

Prerequisites

Apache Maven 3.6.3, Java at least 1.8

Remove Default JAR

You have seen in previous examples on how to generate single executable jar file or multiple executable jar files.

But if you had noticed carefully you saw there was an additional jar which was created in the target folder and this jar was created by maven build tool. By default maven uses maven-jar-plugin to generate jar file.

To prevent maven tool from generating such default jar you need to override the maven-jar-plugin.

You need to add this additional plugin in the plugin section along with maven-assembly-plugin.

The example may be given in the below pom.xml file:

<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>removedefaultjar</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-jar-plugin</artifactId>
				<executions>
					<execution>
						<id>default-jar</id>
						<phase>none</phase>
					</execution>
				</executions>
			</plugin>
			<plugin>
              <artifactId>maven-assembly-plugin</artifactId>
			  ...
            </plugin>
        </plugins>
    </build>
</project>

Thanks for reading.

Leave a Comment