Maven cannot resolve dependency for module in same multi-module project - java

When running commands such as
mvn dependency:build-classpath
or
mvn exec:java
Maven is unable to resolve a dependency of one of my modules on another.
[ERROR] Failed to execute goal on project parser-app: Could not resolve dependencies for project project_group:A:jar:0.1-SNAPSHOT: Could not find artifact project_group:B:jar:0.1-SNAPSHOT
The project structure is as follows:
/pom.xml
/A/pom.xml
/B/pom.xml
The parent pom is as follows:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>project_group</groupId>
<artifactId>parent</artifactId>
<packaging>pom</packaging>
<version>0.1-SNAPSHOT</version>
<name>parent</name>
<modules>
<module>A</module>
<module>B</module>
</modules>
The first child module (the one failing to resolve the dependency):
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>parent_group</groupId>
<artifactId>parent</artifactId>
<version>0.1-SNAPSHOT</version>
</parent>
<artifactId>A</artifactId>
<packaging>jar</packaging>
<name>A</name>
<dependencies>
<dependency>
<groupId>parent_group</groupId>
<artifactId>B</artifactId>
<version>0.1-SNAPSHOT</version>
</dependency>
The second child module (the dependency):
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>parent_group</groupId>
<artifactId>parent</artifactId>
<version>0.1-SNAPSHOT</version>
</parent>
<artifactId>B</artifactId>
<packaging>jar</packaging>
<name>B</name>

Have you run mvn clean install at least once on the project to install the dependencies within your local repository?

The Maven reactor is weird that way, it keeps modules around only for certain tasks. When running a build target that only does something for one subproject, then even if Maven builds dependencies first, it does not keep them around in the reactor (sometimes).
Installing to the local repository is a workaround, but it is horrible and should be avoided when possible, because you can easily end up with outdated build results.
A slightly less ugly workaround is to combine two build targets, where the second build target does something harmless, but triggers addition to reactor in all subprojects.
As an example you can combine the task you want with the 'compile' or 'package' tasks.
Also see highest voted answer at
Maven doesn't recognize sibling modules when running mvn dependency:tree

This error might also be caused by Maven being in offline mode.
Sometimes I seem to accidentally enable offline mode in IntelliJ IDEA. To disable it, toggle the Toggle Offline Mode toggle in the Maven Toolbar
or uncheck the Work Offline checkbox in the settings under Build, Execution, Deployment > Build Tools > Maven.

Configuring test-jar in the jar plugin resolved the issue for me:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Afterwards, running mvn clean install works.

Related

Probleme with Spring application, maven multi module and intelliJ Comunity

I'm working on a project with a parent maven module that contains no sources and 4 child modules.
It's supposed to be a Spring Application, the class having SpringApplication.run(XXX.class, args); is in a child module.
As I work with intelliJ community I don't have the spring plugin that could help me launch easely the application.
If I clean install the project everything builds correctly.
To start the application i try to right click on the class booting Spring and then click on run.
As intelliJ tries to build the app i get the following message :
Failed to execute goal on project XXX : Could not resolve dependencies
for project XXX:XXX:jar:0.0.28: Failed to collect dependencies at
XXX:XXX:jar:0.0.28: Failed to read artifact descriptor for
XXX:jar:0.0.28: Could not transfer artifact XXX:YYY:pom:${ZZZ_version_module} from/to
releases (http://nexus...): Authorization failed for
http://nexus.../.../.../$%257BZZZ_version_module%257D/...-$%257BZZZ_version_module%257D.pom
403 Forbidden
From what i understand the problem is that when building, intelliJ only build the child module, so it cannot turn the variablized version in my pom.xml ${ZZZ_version_module} into the right version using properties from the parent pom.
Because of my work, I cannot use any plugin in intelliJ or any other dependency in my pom that could help me.
Do any one have any solution to run the SpringApplication ?
Parent pom.xml
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.3</version>
</parent>
<groupId>ABC</groupId>
<artifactId>XYZ</artifactId>
<version>${ZZZ_version_module}</version>
<packaging>pom</packaging>
<name>...</name>
<description>...</description>
<properties>
<ZZZ_version_module>0.0.28</ZZZ_version_module>
...
</properties>
</project>
Child pom.xml :
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>ABC</groupId>
<artifactId>XYZ</artifactId>
<version>${ZZZ_version_module}</version>
</parent>
<artifactId>...</artifactId>
<name>...</name>
<description>...</description>
</project>
(Sorry for the broken english)

Maven (eclipse) use shaded jar instead of original jar

I have a project, where the packages will be relocated (shaded jar)
pom.xml:
<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>
<artifactId>artifact-child</artifactId>
<name>artifact-child</name>
<parent>
<groupId>group</groupId>
<artifactId>artifact</artifactId>
<version>${revision}</version>
</parent>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<!-- <shadedArtifactAttached>true</shadedArtifactAttached> -->
<relocations>
<reloaction>
<pattern>example</pattern>
<shadedPattern>${super-package}.example</shadedPattern>
</reloaction>
</relocations>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
And I have a second project, which need the shaded jar at runtime.
pom.xml:
<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>testversion</groupId>
<artifactId>testv</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>testv</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>group</groupId>
<artifactId>artifact-child</artifactId>
<version>0.0.1</version>
<classifier>shaded</classifier>
</dependency>
</dependencies>
</project>
My question:
Eclipse find the other project in his workspace and use directly the source code.
For example: I must write import example.myclass instead of import org.example.myclass.
In some cases, this can be a problem. Is it possible to say, what maven or eclipse should use the "shaded jar" instead of the original source code?
Must I create a online repository, so maven can only download the shaded jar?
I found two other stackoverflow posts (with no result):
Maven Eclipse multi-module shaded dependency
How to install shaded jar instead of the original jar
SOlVED:
My Mistakes:
1. version of the parent must declare directly not via properties
2. Forgot to run "Maven Install"
Solution:
Maven run without errors, but Eclipse use the open project and not the shaded jar.
Found the solution here: How can I download a single raw file from a private github repo using the command line? .
Open the properties of the Project. Under the Tap Maven, remove the check from "Resolve dependencies from Workspace projects"
If the shaded jar is your dependendy, you just reference the classes/packages as they are defined in the shaded jar.
If Eclipse shows an error, Eclipse is wrong (which is often the case). First of all, try to build your project with something like clean verify. Then you see whether you really have errors (or if Eclipse made them up).
If Eclipse errors bother you, try ALT+F5.

Maven issue to build one module using revision property

Recently we tried to deploy files using jenkins providing ${revision} property for the build ( Maven 3.3.9 ).
It worked OK on json creating 0.0.1-SNAPSHOT for dev builds and 0.0.1-RC for releases, but we have an issue using maven on developer machines.
We have multi-module maven project with version defined in parent and some modules uses others as dependencies. Both build it from jenkins and use maven locally, to integrate with IDEs and run tests before pushing it into repository.
When we try to build a single module, maven does not recognize ${revision}, although it works fine when we mention all modules e.g. mvn clean install -pl appserver, we see
Failed to execute goal on project appserver: Could not resolve dependencies for project com.org:appserver:war:0.0.1-local: Failed to collect dependencies at com.org:my-kinesis-events:jar:0.0.1-local: Failed to read artifact descriptor for com.org:my-kinesis-events:jar:0.0.1-local: Could not transfer artifact com.org:my-parent:pom:${revision} from/to central: Failed to transfer .../repository/maven-central/com/org/my-parent/${revision}/my-parent-${revision}.pom. Error code 500, Server Error -> [Help 1]
We tried to use:
-Drevision=0.0.1-local
<profile>
<id>default-version</id>
<activation>
<property>
<name>!revision</name>
</property>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<revision>0.0.1-local</revision>
<project.version>0.0.1-local</project.version>
</properties>
</profile>
but the only thing that works is a build for parent that that builds the module and all modules it depends on:
mvn clean install -pl appserver,my-kinesis-events,module1,module2,...
Although the above works it requires from the team to define custom run configuration in IDE, each time they need something.
Did somebody also experienced this issue and found the solution?
Parent pom snippet:
<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.org</groupId>
<artifactId>my-parent</artifactId>
<version>${revision}</version>
<packaging>pom</packaging>
<name>My Parent module</name>
<modules>
<module>../my-tools</module>
<module>my-kinesis-events</module>
....
</modules>
.....
</project>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>my.org</groupId>
<artifactId>my-kinesis-events<</artifactId>
<version>${project.version}</version>
<dependency>
<dependencies>
</dependencyManagement>
Module pom snippet:
<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>
<artifactId>appServer</artifactId>
<parent>
<groupId>com.org</groupId>
<artifactId>my-dsp</artifactId>
<version>${revision}</version>
<relativePath>..</relativePath>
</parent>
<packaging>war</packaging>
<name>AppServerAPI</name>
<description>Application Server</description>
<dependencies>
<dependency>
<groupId>com.org</groupId>
<artifactId>my-openrtb</artifactId>
</dependency>
.....
</dependencies>
....
</project>
These issues have been fixed since Maven 3.5, you need however to use the maven flatten plugin to ensure that all variables are removed from your POM before publishing. Otherwise you end up having POM containing ${revision} has version.
This is neatly explained in the official maven documentation:
https://maven.apache.org/maven-ci-friendly.html#install-deploy

maven - advice on usage of multi-modules (jar, war, …) project

I have the following Maven organization:
- ./pom.xml (top-level project, which defines the 4 modules below)
- ./a/pom.xml (library jar)
- ./b/pom.xml (library jar)
- ./c/pom.xml (war)
- ./d/pom.xml (executable jar)
For building and deploying the c project, I do:
mvn verify tomcat7:redeploy -pl c -am
For building and executing the d project, I do:
mvn verify exec:java -pl d -am
For d there is a problem. Maven reports:
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.4.0:java (default-cli)
on project parent: The parameters 'mainClass' for goal
org.codehaus.mojo:exec-maven-plugin:1.4.0:java are missing or invalid -> [Help 1]
This is indeed correct, the parent pom.xml has no configuration for exec. Only the d project has a configuration for exec.
This question is a little variation of question maven - advice on usage of multi-modules (jar, war, ...) project, so I decided to dedicate a separate question for it.
Why does Maven try to exec on the parent project?
update
I abstracted the same problem in following three pom files:
The parent pom:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>parent</name>
<modules>
<module>a</module>
<module>b</module>
</modules>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
The a pom:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>test</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>a</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>a</name>
</project>
The b pom:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>test</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>b</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>b</name>
<dependencies>
<dependency>
<groupId>test</groupId>
<artifactId>a</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<mainClass>MyMainClass</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
The command I am executing is:
mvn verify exec:java -pl b -am
This can also be found at https://github.com/jeperjaperjieper/issue-33197130.
I can't cure your problem, but I can give you a recipe. You may have hit a maven issue.
You don't have to use -pl and build from the top every time.
I would expect it to work if you just run 'mvn' (no -pl) at the top; it probably doesn't take very long to build the first pieces.
If you don't want to do that, then you need to run 'mvn install' for the pieces that come first, and then you'll be able to cd into the last piece and build it.
I recommend opening up a JIRA issue for Maven on this; it seems as if you've exposed a problem with -pl, but the best way to find out is to post an issue with a test case and poke the Maven user mailing list.
Try mvn exec:java -Dexec.mainClass="com.example.Main" to run your d project.

MavenError: Failed to execute goal on project: Could not resolve dependencies In Maven Multimodule project

I am trying to create a maven multi-module project. the project is created successfully but when I am trying to use one module as a dependency of another module, it throws an exception. When I create a module using eclipse, I was selecting packaging as a jar, but when the module is created, the packaging tag was not mention in child pom.xml and I manually insert the packaging tag as a jar.
following is my parent pom.xml:
<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.netsol</groupId>
<artifactId>empirecl</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
-------------------------
<modules>
<module>empirecl-web</module>
<module>empirecl-dao</module>
<module>empirecl-service</module>
<module>empirecl-api</module>
</modules>
Dao Child Module:
<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.netsol</groupId>
<artifactId>empirecl</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>empirecl-dao</artifactId>
<packaging>jar</packaging>
<name>empirecl-dao</name>
------------------------
Service Child Module:
<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.netsol</groupId>
<artifactId>empirecl</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>empirecl-service</artifactId>
<packaging>jar</packaging>
<name>empirecl-service</name>
<dependencies>
<dependency>
<groupId>com.netsol</groupId>
<artifactId>empirecl-dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>jar</type>
</dependency>
------------------------------------------
The Dao module maven clean and install successfully, but when i trying to use service module, it will generate an following exception:
[ERROR] Failed to execute goal on project empirecl-service: Could not resolve dependencies for project com.netsol:empirecl-service:jar:0.0.1-SNAPSHOT: Failed to collect dependencies at com.netsol:empirecl-dao:jar:0.0.1-SNAPSHOT: Failed to read artifact descriptor for com.netsol:empirecl-dao:jar:0.0.1-SNAPSHOT: Could not find artifact com.netsol:empirecl:pom:0.0.1-SNAPSHOT -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal on project empirecl-service: Could not resolve dependencies for project com.netsol:empirecl-service:jar:0.0.1-SNAPSHOT: Failed to collect dependencies at com.netsol:empirecl-dao:jar:0.0.1-SNAPSHOT
I am trying the find to solution from web, but still the solution is not found. In eclipse when i open the Dependency Hierarchy of service module, it shown the DAO module as a folder not jar. below is the screen shot of Dependency Hierarchy of service module.
In case anybody comes back to this, I think the problem here was failing to install the parent pom first, which all these submodules depend on, so the Maven Reactor can't collect the necessary dependencies to build the submodule.
So from the root directory (here D:\luna_workspace\empire_club\empirecl) it probably just needs a:
mvn clean install
(Aside: <relativePath>../pom.xml</relativePath> is not really necessary as it's the default value).
In my case I forgot it was packaging conflict jar vs pom. I forgot to write
<packaging>pom</packaging>
In every child pom.xml file
My solution was to insert <packaging>pom</packaging> between artifactId and version
<groupId>com.onlinechat</groupId>
<artifactId>chat-online</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>server</module>
<module>client</module>
<module>network</module>
</modules>
This what worked for me -
Go to Java build path --> Order and Export(check JRE system libraries and maven dependencies) in my case these 2 were unchecked.
My solution:
remove all projects in current workspace
import all again
maven update project (Alt + F5) -> Select All and check Force Update of Snapshots/Releases
maven build (Ctrl + B) until there is nothing to build
It worked for me after the second try.
For me, adding the following block of code under <dependency management><dependencies> solved the problem.
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.el</artifactId>
<version>3.0.1-b06</version>
</dependency>
Check with your VPN connection, if you are working in home.
Delete or close all other projects, and restart netbeans IDE, and try again.

Categories