TExcelTAD
Home › Java Builds: Maven & Gradle › Mini Project: One App, Two Builds, One Repository

Mini Project: One App, Two Builds, One Repository

Time to put the course together. You'll build tally, a tiny invoicing tool, as a real-world-shaped multi-module project with both Maven and Gradle in the same source tree, publish its library to the team repository (/srv/maven-repo) from each tool, and consume each publication from the other tool.

Module Kind What it shows
money library, published a public API using Commons Lang internally (implementation/runtime), JUnit 5 tests
cli application, never published depends on money within the build, SLF4J backend at runtime only
consumer-maven, consumer-gradle separate projects use the published money like any third-party library

The layout

text
tally/
├── pom.xml                       parent + aggregator: dependencyManagement, pinned plugins, enforcer, distributionManagement
├── settings.gradle.kts           projects, central repositories, includeBuild("build-logic")
├── gradle/libs.versions.toml     the same versions, for Gradle
├── build-logic/                  convention plugin tally.java (toolchain, JUnit, reproducible jars)
├── money/
│   ├── pom.xml   build.gradle.kts
│   ├── src/main/java/tally/money/{Money,Invoice}.java
│   └── src/test/java/tally/money/InvoiceTest.java
└── cli/
    ├── pom.xml   build.gradle.kts
    └── src/main/java/tally/cli/Main.java

Rules from the course that the design follows:

  • One place for versions per tool: the parent POM's dependencyManagement and pluginManagement; the version catalog for Gradle. Module files carry no versions.
  • Pinned plugins and a fixed project.build.outputTimestamp in Maven; a convention plugin in an included build (not allprojects) for Gradle.
  • money uses Commons Lang only internally: implementation in Gradle, which publishes it as runtime scope (maven-publish). In Maven it has to be compile, the only scope that is both compiled against and shipped.
  • Releases are immutable: Maven deploys money 1.0.0, Gradle publishes 1.1.0.

Part 1: build and test with both tools

bash · try it yourself
mkdir -p tally && cd tally
# ---------- sources ----------
mkdir -p money/src/main/java/tally/money money/src/test/java/tally/money cli/src/main/java/tally/cli
cat > money/src/main/java/tally/money/Money.java <<'SRC'
package tally.money;

public record Money(long cents) {
    public Money plus(Money o) { return new Money(cents + o.cents); }
    public Money times(int n) { return new Money(cents * n); }
    @Override public String toString() { return String.format("%d.%02d", cents / 100, cents % 100); }
}
SRC
cat > money/src/main/java/tally/money/Invoice.java <<'SRC'
package tally.money;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;

public class Invoice {
    private record Line(String item, int qty, Money price) {}
    private final List<Line> lines = new ArrayList<>();

    public Invoice add(String item, int qty, Money price) {
        lines.add(new Line(item, qty, price));
        return this;
    }

    public Money total() {
        return lines.stream().map(l -> l.price().times(l.qty())).reduce(new Money(0), Money::plus);
    }

    public String render() {
        StringBuilder out = new StringBuilder();
        for (Line l : lines) {
            out.append(StringUtils.rightPad(l.item(), 10)).append(StringUtils.leftPad(l.qty() + " x " + l.price(), 12)).append('\n');
        }
        return out.append(StringUtils.rightPad("TOTAL", 10)).append(StringUtils.leftPad(total().toString(), 12)).toString();
    }
}
SRC
cat > money/src/test/java/tally/money/InvoiceTest.java <<'SRC'
package tally.money;

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;

class InvoiceTest {
    @Test
    void totals() {
        Invoice inv = new Invoice().add("coffee", 2, new Money(450)).add("bagel", 1, new Money(275));
        assertEquals(new Money(1175), inv.total());
    }

    @Test
    void formatsCents() {
        assertEquals("3.05", new Money(305).toString());
    }
}
SRC
cat > cli/src/main/java/tally/cli/Main.java <<'SRC'
package tally.cli;

import org.slf4j.LoggerFactory;
import tally.money.Invoice;
import tally.money.Money;

public class Main {
    public static void main(String[] args) {
        LoggerFactory.getLogger(Main.class).info("rendering invoice");
        System.out.println(new Invoice().add("coffee", 2, new Money(450)).add("bagel", 1, new Money(275)).render());
    }
}
SRC
# ---------- Maven ----------
cat > pom.xml <<'POM'
<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.tally</groupId>
  <artifactId>tally-parent</artifactId>
  <version>1.0.0</version>
  <packaging>pom</packaging>
  <modules><module>money</module><module>cli</module></modules>
  <properties>
    <maven.compiler.release>21</maven.compiler.release>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.build.outputTimestamp>2026-01-01T00:00:00Z</project.build.outputTimestamp>
  </properties>
  <dependencyManagement>
    <dependencies>
      <dependency><groupId>com.tally</groupId><artifactId>money</artifactId><version>${project.version}</version></dependency>
      <dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.21.0</version></dependency>
      <dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>2.0.20</version></dependency>
      <dependency><groupId>org.slf4j</groupId><artifactId>slf4j-simple</artifactId><version>2.0.20</version></dependency>
      <dependency><groupId>org.junit</groupId><artifactId>junit-bom</artifactId><version>5.14.4</version><type>pom</type><scope>import</scope></dependency>
    </dependencies>
  </dependencyManagement>
  <build>
    <pluginManagement>
      <plugins>
        <plugin><artifactId>maven-clean-plugin</artifactId><version>3.5.0</version></plugin>
        <plugin><artifactId>maven-resources-plugin</artifactId><version>3.5.0</version></plugin>
        <plugin><artifactId>maven-compiler-plugin</artifactId><version>3.16.0</version></plugin>
        <plugin><artifactId>maven-surefire-plugin</artifactId><version>3.6.0</version><configuration><runOrder>alphabetical</runOrder></configuration></plugin>
        <plugin><artifactId>maven-jar-plugin</artifactId><version>3.5.1</version></plugin>
        <plugin><artifactId>maven-install-plugin</artifactId><version>3.2.0</version></plugin>
        <plugin><artifactId>maven-deploy-plugin</artifactId><version>3.2.0</version></plugin>
        <plugin><artifactId>maven-dependency-plugin</artifactId><version>3.11.0</version></plugin>
      </plugins>
    </pluginManagement>
    <plugins>
      <plugin>
        <artifactId>maven-enforcer-plugin</artifactId><version>3.6.3</version>
        <executions><execution><id>rules</id><goals><goal>enforce</goal></goals><configuration><rules>
          <requireJavaVersion><version>[21,)</version></requireJavaVersion>
          <dependencyConvergence/>
        </rules></configuration></execution></executions>
      </plugin>
    </plugins>
  </build>
  <distributionManagement>
    <repository><id>team</id><url>file:///srv/maven-repo</url></repository>
  </distributionManagement>
</project>
POM
cat > money/pom.xml <<'POM'
<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <parent><groupId>com.tally</groupId><artifactId>tally-parent</artifactId><version>1.0.0</version></parent>
  <artifactId>money</artifactId>
  <dependencies>
    <dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId></dependency>
    <dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter</artifactId><scope>test</scope></dependency>
  </dependencies>
</project>
POM
cat > cli/pom.xml <<'POM'
<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <parent><groupId>com.tally</groupId><artifactId>tally-parent</artifactId><version>1.0.0</version></parent>
  <artifactId>cli</artifactId>
  <dependencies>
    <dependency><groupId>com.tally</groupId><artifactId>money</artifactId></dependency>
    <dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId></dependency>
    <dependency><groupId>org.slf4j</groupId><artifactId>slf4j-simple</artifactId><scope>runtime</scope></dependency>
  </dependencies>
</project>
POM
# ---------- Gradle ----------
mkdir -p gradle build-logic/src/main/kotlin
cat > gradle/libs.versions.toml <<'TOML'
[libraries]
commons-lang3 = "org.apache.commons:commons-lang3:3.21.0"
slf4j-api = "org.slf4j:slf4j-api:2.0.20"
slf4j-simple = "org.slf4j:slf4j-simple:2.0.20"
TOML
cat > build-logic/settings.gradle.kts <<'KTS'
rootProject.name = "build-logic"
dependencyResolutionManagement { repositories { gradlePluginPortal() } }
KTS
echo 'plugins { `kotlin-dsl` }' > build-logic/build.gradle.kts
cat > build-logic/src/main/kotlin/tally.java.gradle.kts <<'KTS'
plugins { java }
group = "com.tally"
java { toolchain { languageVersion = JavaLanguageVersion.of(21) } }
dependencies {
    testImplementation(platform("org.junit:junit-bom:5.14.4"))
    testImplementation("org.junit.jupiter:junit-jupiter")
    testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}
tasks.withType<Test>().configureEach { useJUnitPlatform() }
tasks.withType<AbstractArchiveTask>().configureEach {
    isPreserveFileTimestamps = false
    isReproducibleFileOrder = true
}
KTS
cat > settings.gradle.kts <<'KTS'
pluginManagement { includeBuild("build-logic") }
rootProject.name = "tally"
dependencyResolutionManagement {
    repositoriesMode = RepositoriesMode.FAIL_ON_PROJECT_REPOS
    repositories { mavenCentral() }
}
include("money", "cli")
KTS
cat > money/build.gradle.kts <<'KTS'
plugins { id("tally.java"); `java-library`; `maven-publish` }
version = "1.1.0"
dependencies { implementation(libs.commons.lang3) }
publishing {
    publications { create<MavenPublication>("money") { from(components["java"]) } }
    repositories { maven { name = "team"; url = uri("/srv/maven-repo") } }
}
KTS
cat > cli/build.gradle.kts <<'KTS'
plugins { id("tally.java"); application }
dependencies {
    implementation(project(":money"))
    implementation(libs.slf4j.api)
    runtimeOnly(libs.slf4j.simple)
}
application { mainClass = "tally.cli.Main" }
KTS
echo "===== maven verify"
mvn verify | grep -E 'Tests run:.*in tally|Reactor Summary|SUCCESS|FAILURE' | sed 's/ \[ *[0-9.]* s\]//;s/, Time elapsed.*//'
echo "===== gradle build"
gradle build | grep -E 'actionable|BUILD'
echo "===== both tested the same tests"
grep -h -o 'tests="[0-9]*"' money/target/surefire-reports/TEST-*.xml money/build/test-results/test/TEST-*.xml
echo "===== run (gradle)"
gradle -q :cli:run 2>&1
echo "===== run (maven)"
mvn -q -pl cli -am package dependency:copy-dependencies -DincludeScope=runtime -DskipTests
java -cp "cli/target/classes:cli/target/dependency/*" tally.cli.Main 2>&1
===== maven verify
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO] Reactor Summary for tally-parent 1.0.0:
[INFO] tally-parent ....................................... SUCCESS
[INFO] money .............................................. SUCCESS
[INFO] cli ................................................ SUCCESS
[INFO] BUILD SUCCESS
===== gradle build
BUILD SUCCESSFUL in 24s
18 actionable tasks: 18 executed
===== both tested the same tests
tests="2"
tests="2"
===== run (gradle)
[main] INFO tally.cli.Main - rendering invoice
coffee        2 x 4.50
bagel         1 x 2.75
TOTAL            11.75
===== run (maven)
[main] INFO tally.cli.Main - rendering invoice
coffee        2 x 4.50
bagel         1 x 2.75
TOTAL            11.75

Both builds ran the same two tests (tests="2" in both JUnit XML reports) and produce the same program. Note how little the module build files contain: everything shared lives in the parent POM and in tally.java.

Part 2: publish from each tool, consume from the other

The library goes to the team repository twice: 1.0.0 with mvn deploy, 1.1.0 with Gradle's publish. Then a Maven project consumes the Gradle-published 1.1.0, and a Gradle project the Maven-deployed 1.0.0:

bash · try it yourself
mkdir -p tally/money/src/main/java/tally/money && cd tally
cat > money/src/main/java/tally/money/Money.java <<'SRC'
package tally.money;

public record Money(long cents) {
    public Money plus(Money o) { return new Money(cents + o.cents); }
    @Override public String toString() {
        return org.apache.commons.lang3.StringUtils.leftPad(String.format("%d.%02d", cents / 100, cents % 100), 8);
    }
}
SRC
# A compact copy of the library and of its two build descriptions (every session starts empty)
cat > money/pom.xml <<'POM'
<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.tally</groupId>
  <artifactId>money</artifactId>
  <version>1.0.0</version>
  <properties>
    <maven.compiler.release>21</maven.compiler.release>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.build.outputTimestamp>2026-01-01T00:00:00Z</project.build.outputTimestamp>
  </properties>
  <dependencies>
    <dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.21.0</version></dependency>
  </dependencies>
  <distributionManagement>
    <repository><id>team</id><url>file:///srv/maven-repo</url></repository>
  </distributionManagement>
</project>
POM
cat > settings.gradle.kts <<'KTS'
rootProject.name = "tally"
dependencyResolutionManagement { repositories { mavenCentral() } }
include("money")
KTS
cat > money/build.gradle.kts <<'KTS'
plugins { `java-library`; `maven-publish` }
group = "com.tally"
version = "1.1.0"
dependencies { implementation("org.apache.commons:commons-lang3:3.21.0") }
publishing {
    publications { create<MavenPublication>("money") { from(components["java"]) } }
    repositories { maven { name = "team"; url = uri("/srv/maven-repo") } }
}
KTS
(cd money && mvn -q -ntp deploy)
gradle -q :money:publish
echo "===== the team repository"
find /srv/maven-repo/com/tally -name '*.jar' -o -name '*.pom' -o -name '*.module' | sort
grep -E '<(release|version)>' /srv/maven-repo/com/tally/money/maven-metadata.xml
echo "===== commons-lang3 scope in each POM"
for v in 1.0.0 1.1.0; do echo "$v: $(grep -A3 'commons-lang3' /srv/maven-repo/com/tally/money/$v/money-$v.pom | grep -o '<scope>.*</scope>' || echo '(no scope = compile)')"; done

echo "===== Maven consumer, money 1.1.0 (published by Gradle)"
mkdir -p ~/consumer-maven/src/main/java/app && cd ~/consumer-maven
cat > pom.xml <<'POM'
<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId><artifactId>consumer-maven</artifactId><version>1.0</version>
  <properties><maven.compiler.release>21</maven.compiler.release></properties>
  <repositories><repository><id>team</id><url>file:///srv/maven-repo</url></repository></repositories>
  <dependencies>
    <dependency><groupId>com.tally</groupId><artifactId>money</artifactId><version>1.1.0</version></dependency>
  </dependencies>
  <build><plugins><plugin>
    <groupId>org.codehaus.mojo</groupId><artifactId>exec-maven-plugin</artifactId><version>3.6.4</version>
    <configuration><mainClass>app.Main</mainClass></configuration>
  </plugin></plugins></build>
</project>
POM
echo 'package app; import tally.money.Money; public class Main { public static void main(String[] a) { System.out.println("maven  consumer:" + new Money(1999).plus(new Money(1))); } }' > src/main/java/app/Main.java
mvn -q compile exec:java

echo "===== Gradle consumer, money 1.0.0 (deployed by Maven)"
mkdir -p ~/consumer-gradle/src/main/java/app && cd ~/consumer-gradle
cat > settings.gradle.kts <<'KTS'
rootProject.name = "consumer-gradle"
dependencyResolutionManagement {
    repositories { maven { url = uri("/srv/maven-repo") }; mavenCentral() }
}
KTS
cat > build.gradle.kts <<'KTS'
plugins { application }
dependencies { implementation("com.tally:money:1.0.0") }
application { mainClass = "app.Main" }
KTS
echo 'package app; import tally.money.Money; public class Main { public static void main(String[] a) { System.out.println("gradle consumer:" + new Money(250).plus(new Money(5))); } }' > src/main/java/app/Main.java
gradle -q run
gradle -q dependencies --configuration compileClasspath | grep -- '--- '
===== the team repository
/srv/maven-repo/com/tally/money/1.0.0/money-1.0.0.jar
/srv/maven-repo/com/tally/money/1.0.0/money-1.0.0.pom
/srv/maven-repo/com/tally/money/1.1.0/money-1.1.0.jar
/srv/maven-repo/com/tally/money/1.1.0/money-1.1.0.module
/srv/maven-repo/com/tally/money/1.1.0/money-1.1.0.pom
    <release>1.1.0</release>
      <version>1.0.0</version>
      <version>1.1.0</version>
===== commons-lang3 scope in each POM
1.0.0: (no scope = compile)
1.1.0: <scope>runtime</scope>
===== Maven consumer, money 1.1.0 (published by Gradle)
maven  consumer:   20.00
===== Gradle consumer, money 1.0.0 (deployed by Maven)
gradle consumer:    2.55
\--- com.tally:money:1.0.0
     \--- org.apache.commons:commons-lang3:3.21.0

Read the last lines carefully: the Gradle consumer does see commons-lang3 on its compile classpath, because the Maven-built 1.0.0 declares it with (default) compile scope; the Gradle-built 1.1.0 declares it runtime, so a Maven consumer of 1.1.0 compiles without it. The same library, published by the two tools, makes different promises about its API. When a library moves from Maven to Gradle, that difference is part of its public contract.

Extend it

  1. Add a cli distribution: gradle :cli:installDist and, in Maven, the shade plugin with a ManifestResourceTransformer. Compare the contents of both.
  2. Add JaCoCo to tally.java and a jacocoTestCoverageVerification rule of 80 %; make check depend on it.
  3. Turn on org.gradle.caching=true and the configuration cache, run gradle clean build twice, and count the FROM-CACHE tasks.
  4. Prove reproducibility: build money twice with each tool (with sleep 3 in between) and compare the JARs with cmp.
  5. Replace version = "1.1.0" with a -Pversion passed by CI, and add dependencyLocking to the consumers.

Test yourself

Gradle publishes money with implementation(commons-lang3). Which scope does Commons Lang get in the published POM?

  1. compile
  2. runtime
  3. provided
  4. test

Why does the Maven parent POM pin plugin versions and set project.build.outputTimestamp?

  1. Maven requires both
  2. So the build doesn't change with the installed Maven version, and the JARs are reproducible
  3. To enable offline mode
  4. To speed up downloads

Why is shared Gradle configuration in a build-logic convention plugin rather than allprojects {}?

  1. allprojects is removed in Gradle 9
  2. Each project applies the convention explicitly, keeping projects decoupled and configuration visible
  3. Convention plugins don't need compilation
  4. It makes Maven faster