Explain Codes LogoExplain Codes Logo

Is it possible to rename a maven jar-with-dependencies?

maven
assembly-descriptor
maven-assembly-plugin
dependency-management
Alex KataevbyAlex Kataev·Feb 24, 2025
TLDR

Sure, it's a cinch! Set the <finalName> tag in your pom.xml's <build> section to rename your Maven jar-with-dependencies. For unique identification purposes, add a <classifier> within the maven-assembly-plugin. Here you go:

<build> <finalName>AppName</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <executions> <execution> <phase>package</phase> <goals> <goal>single</goal> </goals> <configuration> <classifier>bundle</classifier> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </execution> </executions> </plugin> </plugins> </build>

This gives you a classy AppName-bundle.jar, melding both <finalName> and <classifier>. If you need more granularity, read on.

Getting fancy with assembly descriptor

For a custom assembly descriptor, create a jar-assembly.xml with your specific instructions for the assembly:

<assembly> <!-- Oh look, my own custom ID is here! --> <id>custom-bundle</id> <formats> <format>jar</format> </formats> <includeBaseDirectory>false</includeBaseDirectory> <dependencySets> <!-- It's like packing for a trip, but for dependencies --> <dependencySet> <outputDirectory>/</outputDirectory> <unpack>true</unpack> </dependencySet> </dependencySets> </assembly>

Refer to this in your maven-assembly-plugin configuration via the <descriptor> tag. <appendAssemblyId> should be false if you're not keen on adding the assembly id to your jar's name.

Keeping classifier consistency intact

Ensure your classifier stays consistent during installation or deployment to accurately find your artifact:

<configuration> <!-- I promise not to change the classifier --> <classifier>custom</classifier> </configuration>

Tailoring build and region specific profiles

You can manage different build and regional profiles by parameterizing your assembly.xml:

<profiles> <profile> <!-- It's like a "region-specific" disguise for my build --> <id>region-specific</id> <properties> <assembly.descriptorId>region1</assembly.descriptorId> </properties> </profile> </profiles>

This, along with other advanced configurations, offers some nifty flexibility.

Including the main class and manifest

Keep your jar-with-dependencies fully executable by declaring the main class:

<archive> <!-- A class so main, it's in the manifest --> <manifest> <mainClass>com.example.MainClass</mainClass> </manifest> </archive>

Picking the right dependency scopes

You control the included dependencies via your custom dependencySet:

<dependencySet> <!-- Like a VIP pass for runtime dependencies only --> <scope>runtime</scope> </dependencySet>

Exploring output format options

Tailor the archive structure with formats and fileSets for precise customization:

<!-- Being picky about my formats and file sets --> <formats> <format>dir</format> <format>tar.gz</format> </formats> <fileSets> <fileSet> <directory>${project.basedir}/src/main/resources</directory> <outputDirectory>/resources</outputDirectory> </fileSet> </fileSets>