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

Prevent JAR Creation

By default maven uses maven-jar-plugin to generate jar file.

To prevent maven tool from generating such jar we 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 and files) then it will not create JAR file.

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

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

Thanks for reading.

Related posts

Leave a Comment