Maven
Jar
Build Configuration
Artifact Naming
Java Development

Maven finalName: control the jar file name (default artifactId-version)

Codemia Knowledge Hub ·

Set <finalName> inside the <build> section of pom.xml. Without it, Maven names the packaged output (JAR, WAR and so on) artifactId-version, followed by the packaging extension. The final name is the file name of the packaged output once the build completes, so it is what you see when you deploy or distribute the project.

The default name

Maven's default pattern is:

 
<artifactId>-<version>.<packaging>

With artifactId my-app and version 1.0.0, the build produces my-app-1.0.0.jar.

Setting <finalName>

xml
1<project>
2    ...
3    <build>
4        <finalName>my-custom-name</finalName>
5    </build>
6    ...
7</project>

Whatever the artifactId and version, the JAR is now named my-custom-name.jar. The value can be a fixed string, as here, or built from project properties, as in the examples below.

Reasons to do this include:

  1. Naming conventions: Some deployment systems or teams require specific file names.
  2. Dropping the version: Simpler file names when the version is already part of the directory structure.
  3. Environment-specific names: Telling test and production builds apart.
  4. Fitting other tools: Larger build systems or release processes may expect a particular name.

Different names per environment with profiles

Profiles let each environment set its own final name:

xml
1   <profiles>
2       <profile>
3           <id>production</id>
4           <build>
5               <finalName>my-app-prod</finalName>
6           </build>
7       </profile>
8       <profile>
9           <id>development</id>
10           <build>
11               <finalName>my-app-dev</finalName>
12           </build>
13       </profile>
14   </profiles>

Running mvn clean package -P production produces my-app-prod.jar.

Leaving the version out

To build a JAR without the version number:

xml
   <build>
       <finalName>${project.artifactId}</finalName>
   </build>

This produces my-app.jar.

What else a custom name affects

  • CI/CD pipelines: Update any pipeline steps that look for the file by name.
  • Descriptor files: If .yaml, .xml or .json files in the project reference the artifact by name, update them too.
  • Artifact repositories: finalName changes the file built in target/. When you run mvn install or mvn deploy, the copy stored in the repository (Nexus, Artifactory, or your local repository) is still named after the artifactId and version.
FeatureDefault BehaviorCustomized Behavior
NamingartifactId-version.jarCustom name defined in <finalName>
ProfilesNot environment-specificSupports environment-specific configurations
VersionIncluded by defaultCan be excluded or modified

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms