Maven Magic: Removing Primary Artifacts While Keeping RPMs

This technical guide demonstrates how to efficiently manage Maven artifacts when building both WAR and RPM files, specifically focusing on removing the primary WAR artifact during install and deploy phases while retaining the RPM. Using the rpm-maven-plugin for package creation and a simple Groovy script through gmaven-plugin, developers can optimize their repository space by deploying only the necessary RPM artifacts. This approach provides a cleaner alternative to changing the Maven packaging type while maintaining build flexibility.

1 Minutes reading time

Behold the masterpiece that AI hallucinated while reading this post:

"The Little Package That Stayed: A Tale of Maven Repository Cleanup"

(after I fed it way too many marketing blogs and memes)

Created using DALL-E 3

AI-Generated: The Little Package That Stayed: A Tale of Maven Repository Cleanup

Sometimes we use Maven “war” packaging and add the RPM Plugin to create a rpm which contains the project primary artifact. When this rpm needs to be published to the Maven repository, it can be added as a secondary artifact to the build. Here is an example of the plugin configuration in pom.xml:

<plugin>
	<groupId>org.codehaus.mojo</groupId>
	<artifactId>rpm-maven-plugin</artifactId>
	<executions>
	    <execution>
		<goals>
		    <goal>attached-rpm</goal>
		</goals>
		<phase>package</phase>
	    </execution>
	</executions>
</plugin>

This configuration also installs the war and the rpm to the local and even remote repository. If you setup your Maven repository, for instance Nexus, as a yum repository, we are only interested in the rpm, so deploying the war to the repository is a waste of disk space. Of course we could change the Maven packaging to RPM, but then we would have to declare the war plugin configuration by hand. But there is also another solution to remove the primary artifact from install and deploy phase. With a little bit of Groovy, we can do the following:

<plugin>
	<groupId>org.codehaus.groovy.maven</groupId>
	<artifactId>gmaven-plugin</artifactId>
	<version>1.0</version>
	<executions>
	    <execution>
		<id>remove-primary-artefact</id>
		<phase>package</phase>
		<goals>
		    <goal>execute</goal>
		</goals>
		<configuration>
		    <source>project.getArtifact().setFile(null)</source>
		</configuration>
	    </execution>
	</executions>
</plugin>

This will do the trick :-)

Git revision: 2e692ad