Prevent JAR Creation using Maven

Introduction

In this post you will see how to prevent JAR creation using maven build tool if your application does not have the required Java classes or resource files. Actually you don’t need to create jar file when your project does not contain any Java or resource files. I will use maven-jar-plugin which is used by default by maven build tool to create a JAR file.

For example, you will not basically let your maven tool to create a JAR file or you don’t want to create JAR file for your application when your application doesn’t have any resources and in this case you want to prevent your build tool to create jar file. Obviously the default packaging type for Java project is jar.

Prevent JAR Creation

By default maven uses maven-jar-plugin to generate a jar file from your Java project.

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

You can do this by applying two approaches – by overriding configuration and by overriding phase.

Overriding Configuration

Use below configuration in your maven-jar-plugin.

So if your application does not contain any resources (Java or resource files) then it will not create any JAR file for your Java project.

<build>
	<plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-jar-plugin</artifactId>
                        <version>3.8.1</version>
			<configuration>
				<skipIfEmpty>true</skipIfEmpty>
			</configuration>
		</plugin>
	</plugins>
</build>

Overriding Phase

Use below plugin configuration to prevent JAR file creation.

This will not create JAR file even if there is any Java or resource file.

If you are sure that your application does not contain any Java or resource file then you can use this configuration, otherwise you can use the previous configuration example otherwise this will not create a jar file even if your project has the Java files.

<build>
	<plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-jar-plugin</artifactId>
			<version>3.8.1</version>
			<executions>
			  <execution>
				<id>default-jar</id>
				<phase/>
			  </execution>
			</executions>
		</plugin>
	</plugins>
</build>

That’s all. Hope you got idea which configuration to use in what situation to prevent JAR creation using maven build tool.

Author: Soumitra

Hi, I am, Soumitra, the owner of roytuts.com and it is my passion for sharing my knowledge and I have been writing blogs on various technologies since 2014. If you like my tutorials, you may also want to like my Facebook Page, follow me on Twitter, Github.

Leave a Reply

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