| name | java-maven-helper |
| description | Maven build system, dependency management, and Java project configuration assistance. |
Java Maven Dependency and Build Helper Skill
Maven build system, dependency management, and Java project configuration assistance.
Instructions
You are a Maven and Java ecosystem expert. When invoked:
-
Maven Project Management:
- Initialize and configure Maven projects
- Manage pom.xml configuration
- Handle project structure and organization
- Configure multi-module projects
- Use Maven archetypes
-
Dependency Management:
- Add, update, and remove dependencies
- Manage dependency scopes
- Handle version conflicts
- Use dependency management sections
- Work with BOMs (Bill of Materials)
-
Build Configuration:
- Configure plugins and goals
- Set up build profiles
- Manage build lifecycle
- Configure properties and resources
- Handle filtering and resource processing
-
Troubleshooting:
- Fix dependency resolution errors
- Debug build failures
- Resolve plugin conflicts
- Clean corrupted repositories
- Handle version conflicts
-
Best Practices: Provide guidance on Maven project organization, dependency management, and build optimization
Maven Basics
Project Initialization
mvn archetype:generate
mvn archetype:generate \
-DgroupId=com.example \
-DartifactId=my-app \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DarchetypeVersion=1.4 \
-DinteractiveMode=false
mvn archetype:generate \
-DgroupId=com.example \
-DartifactId=my-webapp \
-DarchetypeArtifactId=maven-archetype-webapp \
-DinteractiveMode=false
mvn archetype:generate \
-DgroupId=com.example \
-DartifactId=my-spring-app \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DinteractiveMode=false
Basic Commands
mvn clean
mvn compile
mvn test
mvn package
mvn install
mvn deploy
mvn package -DskipTests
mvn test -Dtest=MyTest
mvn dependency:tree
mvn help:effective-pom
mvn exec:java -Dexec.mainClass="com.example.Main"
Usage Examples
@java-maven-helper
@java-maven-helper --init-project
@java-maven-helper --add-dependency
@java-maven-helper --fix-dependencies
@java-maven-helper --multi-module
@java-maven-helper --troubleshoot
pom.xml Configuration
Complete Example
<?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>my-application</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>My Application</name>
<description>A sample Maven project</description>
<url>https://github.com/user/my-application</url>
<inceptionYear>2024</inceptionYear>
<organization>
<name>Example Corp</name>
<url>https://example.com</url>
</organization>
<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0</url>
<distribution>repo</distribution>
</license>
</licenses>
<developers>
<developer>
<id>dev1</id>
<name>Developer Name</name>
<email>dev@example.com</email>
<organization>Example Corp</organization>
<roles>
<role>developer</role>
</roles>
</developer>
</developers>
<scm>
<connection>scm:git:git://github.com/user/my-application.git</connection>
<developerConnection>scm:git:ssh://github.com/user/my-application.git</developerConnection>
<url>https://github.com/user/my-application</url>
<tag>HEAD</tag>
</scm>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.boot.version>3.2.0</spring.boot.version>
<junit.version>5.10.1</junit.version>
<lombok.version>1.18.30</lombok.version>
<slf4j.version>2.0.9</slf4j.version>
<maven.compiler.plugin.version>3.11.0</maven.compiler.plugin.version>
<maven.surefire.plugin.version>3.2.2</maven.surefire.plugin.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.plugin.version}</version>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>development</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<spring.profiles.active>dev</spring.profiles.active>
</properties>
</profile>
<profile>
<id>production</id>
<properties>
<spring.profiles.active>prod</spring.profiles.active>
</properties>
</profile>
</profiles>
<repositories>
<repository>
<id>central</id>
<name>Maven Central</name>
<url>https://repo.maven.apache.org/maven2</url>
</repository>
</repositories>
</project>
Dependency Scopes
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.14.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>custom-lib</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/custom-lib.jar</systemPath>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.2.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
Project Structure
Standard Maven Layout
my-app/
├── pom.xml
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ └── com/
│ │ │ └── example/
│ │ │ ├── Main.java
│ │ │ ├── model/
│ │ │ ├── service/
│ │ │ └── repository/
│ │ ├── resources/
│ │ │ ├── application.properties
│ │ │ ├── logback.xml
│ │ │ └── db/
│ │ │ └── migration/
│ │ └── webapp/ (for web apps)
│ │ ├── WEB-INF/
│ │ │ └── web.xml
│ │ └── index.html
│ └── test/
│ ├── java/
│ │ └── com/
│ │ └── example/
│ │ └── MainTest.java
│ └── resources/
│ └── test.properties
├── target/ (generated, git-ignored)
└── .gitignore
Multi-Module Project
parent-project/
├── pom.xml (parent)
├── module-core/
│ ├── pom.xml
│ └── src/
├── module-api/
│ ├── pom.xml
│ └── src/
├── module-web/
│ ├── pom.xml
│ └── src/
└── module-cli/
├── pom.xml
└── src/
<?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>parent-project</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>module-core</module>
<module>module-api</module>
<module>module-web</module>
<module>module-cli</module>
</modules>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.2.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
<?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>
<parent>
<groupId>com.example</groupId>
<artifactId>parent-project</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>module-core</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
Dependency Management
Adding Dependencies
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>32.1.3-jre</version>
</dependency>
Dependency Analysis
mvn dependency:tree
mvn dependency:tree -Dincludes=com.google.guava:guava
mvn dependency:analyze
mvn dependency:list
mvn versions:display-dependency-updates
mvn versions:display-plugin-updates
mvn dependency:purge-local-repository
Version Management
<properties>
<spring.version>5.3.31</spring.version>
<junit.version>5.10.1</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.2.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
Excluding Transitive Dependencies
<dependency>
<groupId>com.example</groupId>
<artifactId>some-library</artifactId>
<version>1.0.0</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
Plugin Configuration
Common Plugins
Compiler Plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>17</source>
<target>17</target>
<encoding>UTF-8</encoding>
<compilerArgs>
<arg>-parameters</arg>
<arg>-Xlint:unchecked</arg>
</compilerArgs>
</configuration>
</plugin>
Surefire Plugin (Unit Tests)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<includes>
<include>**/*Test.java</include>
<include>**/*Tests.java</include>
</includes>
<argLine>-Xmx1024m</argLine>
</configuration>
</plugin>
Failsafe Plugin (Integration Tests)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.2.2</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
JAR Plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.example.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
Assembly Plugin (Fat JAR)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.example.Main</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
Shade Plugin (Uber JAR)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.5.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.example.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
Exec Plugin
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<mainClass>com.example.Main</mainClass>
</configuration>
</plugin>
mvn exec:java
mvn exec:java -Dexec.mainClass="com.example.Main"
mvn exec:java -Dexec.args="arg1 arg2"
Build Profiles
Profile Configuration
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<env>development</env>
<database.url>jdbc:h2:mem:testdb</database.url>
</properties>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
</profile>
<profile>
<id>prod</id>
<properties>
<env>production</env>
<database.url>jdbc:postgresql://prod-db:5432/app</database.url>
</properties>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<debug>false</debug>
<optimize>true</optimize>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>test</id>
<properties>
<env>test</env>
</properties>
</profile>
</profiles>
mvn clean install -Pprod
mvn clean install -Pdev,integration-tests
mvn help:active-profiles
mvn help:effective-pom -Pprod
Common Issues & Solutions
Issue: Dependency Not Found
mvn dependency:tree
mvn -U clean install
rm -rf ~/.m2/repository/com/example/problematic-artifact
mvn clean install
<settings>
<proxies>
<proxy>
<id>example-proxy</id>
<active>true</active>
<protocol>http</protocol>
<host>proxy.example.com</host>
<port>8080</port>
</proxy>
</proxies>
</settings>
Issue: Version Conflicts
mvn dependency:tree -Dverbose
mvn dependency:analyze-duplicate
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>32.1.3-jre</version>
</dependency>
</dependencies>
</dependencyManagement>
Issue: Corrupted Repository
mvn dependency:purge-local-repository
rm -rf ~/.m2/repository
mvn clean install
Issue: Out of Memory
export MAVEN_OPTS="-Xmx2048m -XX:MaxPermSize=512m"
echo "MAVEN_OPTS='-Xmx2048m'" > ~/.mavenrc
set MAVEN_OPTS=-Xmx2048m -XX:MaxPermSize=512m
Issue: Slow Builds
mvn -T 4 clean install
mvn -T 1C clean install
mvn clean install -DskipTests
mvn -o clean install
mvn clean install -amd
settings.xml Configuration
Local Repository and Mirrors
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>${user.home}/.m2/repository</localRepository>
<mirrors>
<mirror>
<id>nexus</id>
<name>Nexus Repository</name>
<url>https://nexus.example.com/repository/maven-public/</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>
<servers>
<server>
<id>nexus-releases</id>
<username>deployment</username>
<password>password123</password>
</server>
</servers>
<profiles>
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>central</id>
<url>https://nexus.example.com/repository/maven-public/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>
Testing Configuration
JUnit 5
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.24.2</version>
<scope>test</scope>
</dependency>
</dependencies>
Code Coverage (JaCoCo)
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.11</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>PACKAGE</element>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.80</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
mvn clean test jacoco:report
CI/CD Configuration
GitHub Actions
name: Maven Build
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
cache: 'maven'
- name: Build with Maven
run: mvn clean install -B
- name: Run tests
run: mvn test
- name: Generate coverage report
run: mvn jacoco:report
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
files: ./target/site/jacoco/jacoco.xml
GitLab CI
image: maven:3.9-eclipse-temurin-17
variables:
MAVEN_OPTS: "-Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository"
cache:
paths:
- .m2/repository
stages:
- build
- test
- package
build:
stage: build
script:
- mvn compile
test:
stage: test
script:
- mvn test
- mvn jacoco:report
artifacts:
reports:
junit:
- target/surefire-reports/TEST-*.xml
paths:
- target/site/jacoco
package:
stage: package
script:
- mvn package -DskipTests
artifacts:
paths:
- target/*.jar
Best Practices Summary
Project Organization
- Follow standard Maven directory layout
- Use multi-module structure for large projects
- Keep pom.xml clean and organized
- Use properties for version management
- Document custom configuration
Dependency Management
- Use dependencyManagement for version control
- Prefer BOMs for framework dependencies
- Minimize dependencies
- Regularly update dependencies
- Use appropriate scopes
- Exclude unnecessary transitive dependencies
Build Configuration
- Use build profiles for different environments
- Configure plugins properly
- Use appropriate packaging type
- Set Java version explicitly
- Enable parallel builds when possible
Testing
- Separate unit and integration tests
- Use JaCoCo for code coverage
- Set coverage thresholds
- Run tests in CI/CD pipeline
- Use appropriate test frameworks
Performance
- Use parallel builds (-T)
- Enable offline mode when possible
- Use Maven repository manager (Nexus/Artifactory)
- Configure appropriate memory settings
- Cache dependencies in CI/CD
Quick Reference Commands
mvn clean
mvn compile
mvn test
mvn package
mvn install
mvn deploy
mvn dependency:tree
mvn dependency:analyze
mvn versions:display-dependency-updates
mvn exec:java
mvn spring-boot:run
mvn clean install -Pprod
mvn clean install -DskipTests
mvn clean install -T 4
mvn clean install -U
mvn clean install -o
mvn help:effective-pom
mvn help:active-profiles
Notes
- Always use dependencyManagement for multi-module projects
- Keep Maven version up to date
- Use Maven Wrapper (mvnw) for consistent builds
- Configure settings.xml for organization-wide settings
- Use profiles for environment-specific configuration
- Enable parallel builds for faster compilation
- Set up repository manager for better performance
- Use BOMs to manage framework versions
- Document custom plugin configurations
- Regularly audit and update dependencies