My simple library

..of useful code



Chapters

Maven / Gradle

Maven Overview

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.

Key Features

  • Declarative build configuration with XML
  • Convention over configuration
  • Dependency management
  • Large ecosystem of plugins
  • Strict project structure

Maven Commands

Create a new project
mvn archetype:generate -DgroupId=com.mycompany.app \
    -DartifactId=my-app \
    -DarchetypeArtifactId=maven-archetype-quickstart \
    -DinteractiveMode=false
Common lifecycle commands
# 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
Dependency management
# Display dependency tree
mvn dependency:tree

# Download sources for dependencies
mvn dependency:sources

# Analyze dependency conflicts
mvn dependency:analyze
Add local .jar
mvn install:install-file  -Dfile=file.jar -DgroupId=com.Moshu -DartifactId=myResource -Dversion=1.0 -Dpackaging=jar -DgeneratePom=true

Maven POM Configuration

Basic POM.xml structure
<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>
Note: Maven Lifecycle

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 Overview

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.

Key Features

  • Flexible, programmatic build configuration
  • Incremental builds
  • Powerful dependency management
  • Build cache for faster builds
  • Supports multi-project builds
  • Can use Maven repositories

Gradle Commands

Create a new project
# Initialize a new Gradle project
gradle init --type java-application
Common tasks
# 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
Dependency management
# Display dependency tree
gradle dependencies

# Download sources for dependencies
gradle dependencies --refresh-dependencies

# Generate dependency insight report
gradle dependencyInsight --dependency <dependency-name>

Gradle Build Configuration

Basic build.gradle structure (Groovy DSL)
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()
}
Basic build.gradle.kts structure (Kotlin DSL)
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()
}
Note: Gradle Wrapper

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.

Maven vs Gradle Comparison

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
When to Choose Which?
  • Choose Maven if you need a simple, standardized build process with excellent IDE support and a large ecosystem of plugins.
  • Choose Gradle if you need flexibility, faster builds, custom build logic, or are working on a large multi-project build.