Apache Maven is a build automation tool used primarily for Java projects. It uses a Project Object Model (POM) file to manage project build, reporting and documentation.
mvn archetype:generate -DgroupId=com.mycompany.app \
-DartifactId=my-app \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DinteractiveMode=false
# Compile source code
mvn compile
# Run tests
mvn test
# Create package (JAR, WAR, etc.)
mvn package
# Install artifact in local repository
mvn install
# Clean build artifacts
mvn clean
# Run all phases up to install
mvn clean install
# Display dependency tree
mvn dependency:tree
# Download sources for dependencies
mvn dependency:sources
# Analyze dependency conflicts
mvn dependency:analyze
mvn install:install-file -Dfile=file.jar -DgroupId=com.Moshu -DartifactId=myResource -Dversion=1.0 -Dpackaging=jar -DgeneratePom=true
<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.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Maven has three built-in build lifecycles: default (handles project deployment), clean (handles project cleaning), and site (handles site documentation). Each lifecycle consists of phases that are executed sequentially.
Gradle is a build automation tool that combines the power and flexibility of Ant with the dependency management and conventions of Maven. It uses a Groovy or Kotlin DSL for build scripts.
# Initialize a new Gradle project
gradle init --type java-application
# List available tasks
gradle tasks
# Build the project
gradle build
# Run tests
gradle test
# Clean build artifacts
gradle clean
# Run a specific task
gradle <task-name>
# Build with parallel execution
gradle build --parallel
# Build with continuous mode (watch for changes)
gradle build --continuous
# Display dependency tree
gradle dependencies
# Download sources for dependencies
gradle dependencies --refresh-dependencies
# Generate dependency insight report
gradle dependencyInsight --dependency <dependency-name>
plugins {
id 'java'
id 'application'
}
group 'com.mycompany.app'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
implementation 'com.google.guava:guava:30.1.1-jre'
testImplementation 'junit:junit:4.13.2'
}
application {
mainClass = 'com.mycompany.app.App'
}
tasks.named('test') {
useJUnitPlatform()
}
plugins {
java
application
}
group = "com.mycompany.app"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
implementation("com.google.guava:guava:30.1.1-jre")
testImplementation("junit:junit:4.13.2")
}
application {
mainClass.set("com.mycompany.app.App")
}
tasks.test {
useJUnitPlatform()
}
The Gradle Wrapper is the preferred way to execute Gradle builds. It ensures that the right Gradle version is used for the build. To generate the wrapper:
gradle wrapper --gradle-version 7.4.1
Then use the wrapper scripts (gradlew or gradlew.bat) instead of the global gradle command.
| Feature | Maven | Gradle |
|---|---|---|
| Build Script Language | XML (declarative) | Groovy/Kotlin DSL (programmatic) |
| Performance | Slower due to rigid lifecycle | Faster with incremental builds and build cache |
| Dependency Management | Excellent, with transitive dependency resolution | More flexible, with rich dependency configurations |
| Customization | Limited, through plugins | Highly customizable with custom tasks |
| Learning Curve | Easier for simple projects | Steeper but more powerful |
| IDE Support | Excellent in all major IDEs | Good, but sometimes slower with large builds |
| Multi-project Builds | Supported but can be complex | Excellent support with flexible configurations |
My simple library
..of useful code