Build Tools and Ecosystem
Maven, Gradle, project structure, and dependency management
Introduction
In this lesson, you'll learn about build tools and ecosystem in Java. Coming from JavaScript, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.
In JavaScript, you're familiar with maven, gradle, project structure, and dependency management.
Java has its own approach to maven, gradle, project structure, and dependency management, which we'll explore step by step.
The Java Way
Let's see how Java handles this concept. Here's a typical example:
<!-- pom.xml -->
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>my-app</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<!-- Runtime dependency -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>32.1.3-jre</version>
</dependency>
<!-- Test dependency -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
<!-- mvn install — download deps -->
<!-- mvn compile — compile -->
<!-- mvn test — run tests -->
<!-- mvn package — create JAR -->
<!-- mvn spring-boot:run — run Spring app -->Comparing to JavaScript
Here's how you might have written similar code in JavaScript:
// package.json
{
"name": "my-app",
"version": "1.0.0",
"scripts": {
"start": "node src/index.js",
"test": "jest",
"build": "tsc"
},
"dependencies": {
"express": "^4.18.0"
},
"devDependencies": {
"jest": "^29.0.0"
}
}
// Install deps
// npm install
// npm install express
// npm install --save-dev jest
// Run
// npm start
// npm testYou may be used to different syntax or behavior.
Maven uses pom.xml (XML); Gradle uses build.gradle (Groovy/Kotlin DSL)
You may be used to different syntax or behavior.
groupId:artifactId:version (GAV) identifies a dependency — like npm's package@version
You may be used to different syntax or behavior.
Dependencies downloaded from Maven Central (like npm registry)
You may be used to different syntax or behavior.
mvn package creates a JAR file — the deployment unit for Java apps
You may be used to different syntax or behavior.
Java project structure is standardized: src/main/java, src/test/java, src/main/resources
Step-by-Step Breakdown
1. Maven Coordinates (GAV)
Every Java library is identified by groupId (org), artifactId (name), and version. Find libraries at mvnrepository.com.
"express": "^4.18.0" // in package.json<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>3.2.0</version>
</dependency>2. Maven Lifecycle Commands
Maven has lifecycle phases. Each phase runs all previous phases first: compile → test → package → install → deploy.
npm install
npm test
npm run buildmvn install # download deps
mvn compile # compile .java → .class
mvn test # run JUnit tests
mvn package # create .jar in target/3. Gradle Alternative
Gradle is Maven's modern alternative — faster (incremental builds, daemon) with a Kotlin/Groovy DSL instead of XML.
// npm scripts in package.json// build.gradle.kts
plugins { kotlin("jvm") version "1.9.0" }
dependencies {
implementation("com.google.guava:guava:32.1.3-jre")
testImplementation("org.junit.jupiter:junit-jupiter:5.10.0")
}4. Project Structure
Maven enforces a standard directory layout. src/main/java for production code, src/test/java for tests.
src/
index.js
test/
index.test.js
package.jsonsrc/
main/
java/com/example/App.java
resources/application.properties
test/
java/com/example/AppTest.java
pom.xmlCommon Mistakes
When coming from JavaScript, developers often make these mistakes:
- Maven uses pom.xml (XML); Gradle uses build.gradle (Groovy/Kotlin DSL)
- groupId:artifactId:version (GAV) identifies a dependency — like npm's package@version
- Dependencies downloaded from Maven Central (like npm registry)
Key Takeaways
- pom.xml defines project metadata, dependencies (GAV coordinates), and build lifecycle
- mvn compile/test/package are the main commands; mvn install puts JAR in local repo
- Gradle is faster with a DSL syntax; both are widely used
- Standard layout: src/main/java for code, src/test/java for tests, src/main/resources for configs