Explain Codes LogoExplain Codes Logo

Controlling Maven final name of jar artifact

java
maven
xml
build
Nikita BarsukovbyNikita Barsukov·Sep 29, 2024
⚡TLDR

Change the default jar name in Maven by adding the <finalName> tag in your pom.xml:

<build> <finalName>YourJarNameHere</finalName> </build>

Just replace YourJarNameHere with your intended artifact name. No need to add the .jar extension; that's done automatically by Maven!

How to handle Maven 3.x

For Maven versions 3.0.0 and onwards, the <finalName> tag works well within the <build> tags. But remember—Maven may have a zest for surprise due to a known bug. So always inspect your build output vigilantly to validate your expected final names are in place:

<!-- When Maven behaves capriciously, always faithfully inspect your output! --> <build> <finalName>Catch_Me_If_You_Can-${project.version}</finalName> </build>

Compatibility with older Maven versions

For our fellow devs still on the legacy Maven versions, don't fret. The solution sits within the maven-jar-plugin configuration:

<!-- Old is gold—but comes with a manual changing the bulb procedure --> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <finalName>Golden_Oldie-${project.version}</finalName> </configuration> </plugin> </plugins> </build>

The great escape: Using Profiles

Have multiple configurations to deal with? Use profiles. It's like having a bag of candies, each with a different flavor:

<!-- Profiles: Maven's own multiverse --> <profiles> <profile> <id>testing</id> <build> <finalName>QuantumArtifacts-${project.version}-test</finalName> </build> </profile> </profiles>

Activate your selected universe by launching Maven with -Ptesting.

Taking Control: Using Properties

You're a power user who wants greater control or are managing a multi-module enterprise. Sounds like an epic adventure! Leverage Maven properties:

<!-- Assuming control of the Maven-verse --> <properties> <jar.finalName>Avengers-${project.version}</jar.finalName> </properties>

You can modify the <finalName> in real-time using -Djar.finalName=myCustomName.

Custom Installations: The Maven Install Plugin

Planning to customize your installation? The maven-install-plugin is the gem you are looking for:

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> <configuration> <!-- In the name of cleanliness, create checksum --> <createChecksum>true</createChecksum> </configuration> </plugin>

Fine-tune the location and meta-data using <file> or <pomFile>.

Expanding the scope: WAR and EAR artifacts

So you have war and ear artifacts to manage? Maven’s got you covered with specific plugins:

  • maven-war-plugin: With <outputFileNameMapping> under your control, every war is winnable.
  • maven-ear-plugin: <fileNameMapping> gives you control over module names.
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.3.1</version> <configuration> <outputFileNameMapping>@{artifactId}@-@{version}@.@{extension}@</outputFileNameMapping> </configuration> </plugin>

Hitchhiker's Guide to Troubleshooting

  • Case Sensitivity: XML is case-sensitive! Think of <finalName> as your VIP guest. Spell it right, and all is good.
  • Clean Builds: If you encounter issues, consider starting from scratch with mvn clean install.
  • Conflict Resolution: Ensure no other sneaky settings or plugins are trespassing your <finalName> territory.
  • Research: Documentation is your best friend—the good samaritan always ready to help. Official Maven plugin documentation is your go-to source of truth.

Tip of the Hat

A mega shout-out to Matthew for his valuable comments and for spotlighting the potential Maven bug related to <finalName>. These community insights are what make us better developers every day!