Sub topic 4
Naming Artifacts: groupId, artifactId, and version
In the last chapter you built a JAR file by hand and named it app.jar. That name works on your own machine. Now picture thousands of teams, each publishing a file called app.jar. Nobody could tell them apart. This chapter covers the naming system that Maven and Gradle use to give every library a unique name.
What an Artifact Is
An artifact is a file that a build produces and that other projects can use. Most of the time it is a JAR file. A JAR file (Java ARchive) is a zip file that holds compiled .class files.
Your project produces an artifact. The libraries your project uses are artifacts too. A library is code someone else wrote and packaged so you can reuse it. JUnit is one example.
To use a library, a build tool needs to find exactly the right file. It does this with coordinates.
The Three Coordinates
Coordinates are a set of values that together name one artifact. Think of a street address, where country, city, and street together point to one house. Java artifacts have three main coordinates:
| Coordinate | What it names | Example |
|---|---|---|
| groupId | The organization or project family | `org.junit.jupiter` |
| artifactId | The specific module or library | `junit-jupiter` |
| version | Which release of that library | `5.10.2` |
People often write all three on one line, separated by colons. This short form is called GAV (Group, Artifact, Version):
org.junit.jupiter:junit-jupiter:5.10.2You will see this format in Gradle build scripts, in error messages, and in dependency reports.
groupId
The groupId says who made the artifact. By convention it is your organization's internet domain written backwards. If your company owns example.com, your groupId starts with com.example.
Domains are already unique around the world, so reversing them gives you a unique prefix for free. Java package names use the same convention, which is why the class in this tutorial lives in package com.example.
You can add more parts to split a large organization into teams or products:
com.examplefor a small companycom.example.paymentsfor one product inside itorg.apache.commonsfor the Apache Commons project
artifactId
The artifactId names one thing the group publishes. One group can publish many artifacts. Apache Commons publishes commons-lang3, commons-io, and commons-text, all under related groups.
Good artifactIds are:
- lowercase
- made of words joined by hyphens, like
order-service - short and descriptive
The artifactId also becomes part of the file name. An artifactId of hello with version 1.0-SNAPSHOT gives a file named hello-1.0-SNAPSHOT.jar.
version
The version says which release of the artifact you mean. Code changes over time, and the version lets you pick a specific point in that history.
Many projects use semantic versioning, a convention with three numbers: MAJOR.MINOR.PATCH.
- MAJOR goes up when a change can break code that uses the library.
- MINOR goes up when new features arrive and old code still works.
- PATCH goes up for bug fixes only.
So moving from 5.10.1 to 5.10.2 should be safe. Moving from 4.13 to 5.0 may require changes in your code. JUnit 4 to JUnit 5 is a real example of a major change.
Releases and Snapshots
A release version is fixed forever. Once 5.10.2 is published, its contents never change. This matters because your build should give the same result today and next year.
A SNAPSHOT version is a work in progress. Any version that ends in -SNAPSHOT, such as 1.0-SNAPSHOT, tells the build tool that the artifact may change. Build tools check again for newer copies of snapshots. They never re-check releases.
New projects usually start at 1.0-SNAPSHOT or 0.1.0-SNAPSHOT. When you are ready to publish, you remove the -SNAPSHOT suffix to make a release.
How Coordinates Become a File Path
Build tools store artifacts in a repository, a folder or server that holds artifacts in a fixed layout. A later chapter covers repositories in detail. For now, the key point is that the coordinates map straight to a path:
- Replace each dot in the groupId with a folder separator.
- Add the artifactId as a folder.
- Add the version as a folder.
- Name the file
artifactId-version.jar.
This small program applies those rules. Save it as CoordinatePath.java and run it with java CoordinatePath.java. Java 11 and later can run a single source file without compiling it first.
public class CoordinatePath {
public static void main(String[] args) {
String coords = args.length > 0 ? args[0] : "org.junit.jupiter:junit-jupiter:5.10.2";
String[] parts = coords.split(":");
if (parts.length != 3) {
System.err.println("Expected groupId:artifactId:version");
System.exit(1);
}
String groupId = parts[0];
String artifactId = parts[1];
String version = parts[2];
String path = groupId.replace('.', '/') + "/"
+ artifactId + "/"
+ version + "/"
+ artifactId + "-" + version + ".jar";
System.out.println(path);
}
}Running it with the default value prints:
org/junit/jupiter/junit-jupiter/5.10.2/junit-jupiter-5.10.2.jarTry your own project's coordinates:
java CoordinatePath.java com.example:hello:1.0-SNAPSHOTWhere You Write Coordinates
Every project declares its own coordinates, and it uses the same format to point at the libraries it needs.
In Maven, the coordinates sit near the top of pom.xml. You will build a full POM in the next chapter. Here is just the part that names the project:
<groupId>com.example</groupId>
<artifactId>hello</artifactId>
<version>1.0-SNAPSHOT</version>In Gradle, the artifactId comes from the project name in settings.gradle.kts. The group and version go in build.gradle.kts:
// settings.gradle.kts
rootProject.name = "hello"
// build.gradle.kts
group = "com.example"
version = "1.0-SNAPSHOT"When you name a library to use, Gradle takes the single-line GAV form:
testImplementation("org.junit.jupiter:junit-jupiter:5.10.2")Edge Cases Worth Knowing
Versions are not always plain numbers
Some versions carry a qualifier, which is extra text after the numbers. Guava uses 33.2.1-jre and 33.2.1-android to mark builds for different platforms. Other common qualifiers are -alpha, -beta, and -RC1 (release candidate). Maven treats 1.0-alpha and 1.0-RC1 as older than 1.0.
Versions are compared as numbers, not text
As text, 1.10 sorts before 1.9, because the character 1 comes before 9. Build tools compare each part as a number, so 1.10 is newer than 1.9. Keep this in mind when you read version lists.
Changing a groupId creates a new identity
If a library moves to a new groupId, build tools treat it as a different artifact. JUnit 4 used junit:junit. JUnit 5 uses org.junit.jupiter. A project could end up with both, because the tool cannot tell that they are related.
Optional extra coordinates
Two more values can appear. Packaging says the file type, such as jar or war. It defaults to jar. A classifier marks a variant of the same version, such as sources for a JAR of source code. Most of the time you can ignore both.
Exercises
Exercise
Your company owns the domain shop.io. You are building a library that calculates taxes. Suggest a groupId, artifactId, and first version, and write them as a GAV string.
Show answer
A good choice is io.shop:tax-calculator:1.0-SNAPSHOT. The groupId reverses the domain, the artifactId is lowercase with a hyphen, and the version is a snapshot because the library is still in development.
Exercise
What repository path holds the artifact com.google.guava:guava:33.2.1-jre?
Show answer
com/google/guava/guava/33.2.1-jre/guava-33.2.1-jre.jar. The dots in the groupId become folders, then come the artifactId folder, the version folder, and the file named artifactId-version.jar.
Exercise
A teammate's project depends on my-lib 2.3.0-SNAPSHOT and they plan to release tomorrow. What should they change first, and why?
Show answer
They should switch to a release version of my-lib, such as 2.3.0. A SNAPSHOT can change at any time, so a build that depends on one cannot be reproduced reliably.
Exercise
Which version is newer, 2.9.0 or 2.10.0? Which is newer, 3.0-RC1 or 3.0?
Show answer
2.10.0 is newer, because build tools compare 10 and 9 as numbers. 3.0 is newer than 3.0-RC1, because a release candidate comes before the final release.