Find unresolved maven dependencies in multi-module maven project

I’d like maven to report unresolved dependencies in multi-module maven Java project which has below structure:

multi-module-java-app/ ├── app1 │   ├── pom.xml │   └── src ├── app2 │   ├── pom.xml │   └── src └── pom.xml 

poms are at the bottom.

Background:

Maven is used as a dependency management and build tool. Artifactory is repository manager. Artifacts may be built using maven locally on a developer’s environment or on Jenkins build server. Artifactory periodically moves artifacts to special archive repository which is part of all repository virtual.

Maven caches locally built artifacts in~/.m2 directory on the computer where Maven runs whether it’s developer environment or build server.

Problem

Several issues may arise:

Local builds on developers’ VMs may succeed, but fail in Jenkins.

Local builds, Jenkins builds may succeed, but fail on another developer’s VM.

Cause

Present/missing artifacts on a developer .m2 cache, missing artifacts in build server’s .m2 cache and/or archive Artifactory repository

Proposed solution

Run [path_to_maven]mvn dependency:list. [path_to_maven] has custom maven installation (with empty .m2 cache) in the root folder of the project. Custom maven is also configured using settings.xml to use special non-archived repository (all repository without the archive). The output is like below:

It reports unresolved dependencies as well as dependent artifacts which miss them. However this solution has 2 main drawbacks:

  1. .m2 missing slows down the detection significantly as all the dependencies have to be downloaded
  2. unresolved artifacts or modules which miss them are always snapshots.

Running mvn -B -q versions:set -DnewVersion=[some_version] solves the second. This command runs during release pipeline anyway.

However it’s not clear how to solve the first.

How to find unresolved maven dependencies while using .m2, so that unresolved dependencies may be detected quickly during Jenkins build after each push to feature branch?

The only idea that pops is that .m2 on build server will be synced with Artifactory.

.m2 on developers’ machines can be synced as well using some sort of custom plugin that uses rsync. Is there a known plugin that does this?

The ultimate goal is to remove archive repository and let the builds fail. But, first developers need to align the dependencies to the latest versions.

root pom.xml:

<?xml version="1.0" encoding="UTF-8"?>   <project 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>multi-module-java-app</artifactId>   <version>1.0-SNAPSHOT</version>   <packaging>pom</packaging>     <name>multi-module-java-app</name>   <!-- FIXME change it to the project's website -->   <url>http://www.example.com</url>     <pluginRepositories>           <pluginRepository>           <id>plugins</id>           <name>plugins</name>           <url>http://localhost:8081/artifactory/all</url>       </pluginRepository>   </pluginRepositories>     <repositories>       <repository>           <id>all</id>           <name>all</name>           <url>http://localhost:8081/artifactory/all</url>       </repository>   </repositories>     <dependencyManagement>     <dependencies>       <dependency>         <groupId>com.example</groupId>         <artifactId>spring-boot</artifactId>               </dependency>     </dependencies>   </dependencyManagement>     <modules>       <module>app1</module>       <module>app2</module>   </modules>   </project> 

app 1 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>   <project 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.mycompany.app</groupId>         <artifactId>multi-module-java-app</artifactId>         <version>1.0-SNAPSHOT</version>         <relativePath>../pom.xml</relativePath>   </parent>   <groupId>com.mycompany.app.app1</groupId>   <artifactId>app1</artifactId>   <version>1.0-SNAPSHOT</version>     <name>app1</name>     <build>         <plugins>             <plugin>                 <groupId>org.apache.maven.plugins</groupId>                 <artifactId>maven-jar-plugin</artifactId>                 <configuration>                     <finalName>${artifactId}</finalName>                 </configuration>             </plugin>         </plugins>   </build>   <pluginRepositories>           <pluginRepository>           <id>plugins</id>           <name>plugins</name>           <url>http://localhost:8081/artifactory/all</url>       </pluginRepository>   </pluginRepositories>     <repositories>       <repository>           <id>all</id>           <name>all</name>           <url>http://localhost:8081/artifactory/all</url>       </repository>   </repositories>     <dependencies>     <dependency>       <groupId>com.example</groupId>       <artifactId>spring-boot</artifactId>       <version>0.0.1-20200510.095344-1</version>     </dependency>   </dependencies>   </project> 

app2 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>   <project 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.mycompany.app</groupId>         <artifactId>multi-module-java-app</artifactId>         <version>1.0-SNAPSHOT</version>         <relativePath>../pom.xml</relativePath>   </parent>   <groupId>com.mycompany.app.app2</groupId>   <artifactId>app2</artifactId>   <version>1.0-SNAPSHOT</version>     <name>app2</name>   <build>       <plugins>           <plugin>               <groupId>org.apache.maven.plugins</groupId>               <artifactId>maven-jar-plugin</artifactId>               <configuration>                   <finalName>${artifactId}</finalName>               </configuration>           </plugin>       </plugins>   </build>     <pluginRepositories>           <pluginRepository>           <id>plugins</id>           <name>plugins</name>           <url>http://localhost:8081/artifactory/all</url>       </pluginRepository>   </pluginRepositories>     <repositories>       <repository>           <id>all</id>           <name>all</name>           <url>http://localhost:8081/artifactory/all</url>       </repository>   </repositories>     <dependencies>     <dependency>       <groupId>com.example</groupId>       <artifactId>spring-boot</artifactId>       <version>0.0.1-20200510.095344-1</version>     </dependency>   </dependencies>   </project> 
Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.