Controlling Maven final name of jar artifact
Change the default jar name in Maven by adding the <finalName> tag in your pom.xml:
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:
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:
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:
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:
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:
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.
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!
Was this article helpful?