One module in another project
The modules of a reactor find each other without a repository, because the build has just compiled them. Two projects in two checkouts do not: they meet in the local repository, where mvn install leaves the jar and its pom, and a dependency on those coordinates resolves from there. This article installs a small library and uses it from a second project that knows nothing about the first one except its coordinates.
The library
A record with two methods and the shortest pom of the course, the one of the coordinates article:
price/pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>price</artifactId>
<version>1.0.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>21</maven.compiler.release>
</properties>
</project>
price/src/main/java/com/example/price/Price.java
package com.example.price;
public record Price(long cents) {
public Price plus(Price other) {
return new Price(cents + other.cents);
}
public String format() {
return "%d.%02d".formatted(cents / 100, cents % 100);
}
}
Installed
cd price
mvn -B install > install.log 2>&1; echo "status: $?"; sed "s#$PWD#<project>#g" install.log
status: 0
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< com.example:price >--------------------------
[INFO] Building price 1.0.0
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- install:3.1.4:install (default-install) @ price ---
[INFO] Installing <project>/pom.xml to <HOME>/.m2/repository/com/example/price/1.0.0/price-1.0.0.pom
[INFO] Installing <project>/target/price-1.0.0.jar to <HOME>/.m2/repository/com/example/price/1.0.0/price-1.0.0.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: <TIME>
[INFO] Finished at: <TIMESTAMP>
[INFO] ------------------------------------------------------------------------
find ~/.m2/repository/com/example/price -type f | sort | sed "s#$HOME#~#g"
~/.m2/repository/com/example/price/1.0.0/_remote.repositories
~/.m2/repository/com/example/price/1.0.0/price-1.0.0.jar
~/.m2/repository/com/example/price/1.0.0/price-1.0.0.pom
~/.m2/repository/com/example/price/maven-metadata-local.xml
The four files are the contract: a jar to compile and run against, a pom to read the transitive dependencies from, a bookkeeping file of the resolver and the metadata of the version. A project that installs over an older version of the same coordinates overwrites it, which is the reason a version number changes when the code does.
The project that uses it
A second project in a second folder, with no parent and no reactor: one dependency on the coordinates of the library, and one class that calls it.
report/pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>report</artifactId>
<version>1.0.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>21</maven.compiler.release>
</properties>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>price</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
report/src/main/java/com/example/report/Report.java
package com.example.report;
import com.example.price.Price;
public class Report {
public static void main(String[] args) {
Price subtotal = new Price(1999);
Price shipping = new Price(250);
System.out.println("subtotal " + subtotal.format() + ", with shipping " + subtotal.plus(shipping).format());
}
}
cd ../report
mvn -B package > build.log 2>&1; echo "status: $?"; sed "s#$PWD#<project>#g" build.log
status: 0
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< com.example:report >-------------------------
[INFO] Building report 1.0.0
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO]
[INFO] --- jar:3.5.0:jar (default-jar) @ report ---
[INFO] Building jar: <project>/target/report-1.0.0.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: <TIME>
[INFO] Finished at: <TIMESTAMP>
[INFO] ------------------------------------------------------------------------
The compile resolved com.example:price:1.0.0 from the local repository, added the jar to the compile class path, and the test phase had nothing to do. The tree of the project is one line, which is the whole of what this project knows about the library:
mvn -B dependency:tree
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< com.example:report >-------------------------
[INFO] Building report 1.0.0
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- dependency:3.7.0:tree (default-cli) @ report ---
[INFO] com.example:report:jar:1.0.0
[INFO] \- com.example:price:jar:1.0.0:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: <TIME>
[INFO] Finished at: <TIMESTAMP>
[INFO] ------------------------------------------------------------------------
Running it
The jar of report does not contain the library, so a run needs both on the class path. dependency:build-classpath writes the class path of the project into a file, one separator between the entries and no newline:
mvn -B -q dependency:build-classpath -Dmdep.outputFile=cp.txt; echo "status: $?"; cat cp.txt | sed "s#$HOME#~#g"
status: 0
~/.m2/repository/com/example/price/1.0.0/price-1.0.0.jar
One jar, because the library has no dependencies of its own. Handing that file to java together with the classes of the project runs the program:
java -cp target/classes:$(cat cp.txt) com.example.report.Report
subtotal 19.99, with shipping 22.49
$(cat cp.txt) is a command substitution, so the class path of the run is the classes folder, a colon and the jar the library was installed as. The same program from the fat jar article would be one file instead, which is what a project that ships to a machine without Maven wants.
The repository as a contract
The two projects never see each other’s folders: they agree on three coordinates and on a version. That has four consequences worth stating, because each one is a rule of how a library is released:
- A released version never changes. The library at 1.0.0 is what it was on the day it was installed; a fix is 1.0.1 and a change of the API is 2.0.0. A client that asks for 1.0.0 keeps getting the code it was tested with.
- A -SNAPSHOT version may change, and a client that uses one is asking for the newest build of that version. The next article is about the suffix.
- The pom is part of the artifact. The transitive dependencies of the library reach the client because the pom was installed next to the jar, and a client that gets a jar without its pom loses them.
- The repository is per machine. A teammate does not have what this article installed: they have to build the library, or get it from a repository the two of them share, which is where mvn deploy and a Nexus come in. Neither is available offline, and the settings article shows what their configuration would look like.
Where to go next
SNAPSHOT versions takes this pair of projects and gives the library a version that a build may replace.
Article author: Arthur Isaev