Introduction

In this post we will see how to prevent JAR creation using maven build tool if your application does not have the required Java classes or resource files. We will use maven-jar-plugin which is used by default by maven build tool to create a JAR file. So 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. Obviously the packaging type is jar, which is a default value.

Example

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.

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

Tags:

Leave a Reply

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