Maven transitive dependencies - maven-3

I have 3 maven projects: first - "A" with packaging jar, second - "B" packaging jar, third - "C" packaging jar. "C" depends on "B" as compile time dependency, "B" depends on "A" also as compile time dependency. Project "C" also uses classes from "A". So I have transitive dependency C -> B -> A. But when I try to build these projects maven failed with compilation error while compiling project C: it can't find classes from project "A" that is used in "C".
I thought that maven resolves transitive dependency. Why I get such an error?

Maven resolves transitive dependencies, but will not (usually) automatically build them for you. You can get maven to do what you want by supplying the --also-make/-am flag along with the project list:
mvn -am -pl C clean install
This assumes that A, B, C are all modules of a shared parent, and you would be running the command above in the parent's directory.

The failure of transitive dependency in Maven occurs due to many reason. You need to enable debugging option for that.
I faced similar issue. I am using eclipse. To enable debug option for maven in eclipse: Windows->Preference->Maven->Tick mark Debug Output
Error in my project was:
[WARNING] The POM for mil-pop2:java-json:jar:1.0 is invalid, transitive dependencies (if any) will not be available: 4 problems were encountered while building the effective model for mil-pop2:java-json:1.0
[ERROR] 'dependencies.dependency.systemPath' for java-json.jar:java-json.jar:jar must specify an absolute path but is ${project.basedir}/src/main/resources/lib/java-json.jar #
The reason for failure in transitive dependency is the project in which we are dependent inherits the jar from custom path rather than local repository. Remove the custom path if you have provided.
Before :
<dependency>
<groupId>java-json</groupId>
<artifactId>java-json.jar</artifactId>
<version>1</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/java-json.jar</systemPath>
</dependency>
After:
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>
This solved my error.

Related

Failed build - path to project is a directory

The path to my project is /project/
My build file structure is src/main/package/subpackage/Class.java
My test file structure is src/test/package/subpackage/Test.java
I would like my compiled code to be in bin/main/package/subpackage/Class.class
Compiled test code in bin/test/package/subpackage/Test.class
My pom.xml has the entry
<build>
<sourceDirectory>${project.basedir}/src/main</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test</testSourceDirectory>
<outputDirectory>${project.basedir}/bin/main</outputDirectory>
<testOutputDirectory>${project.basedir}/bin/test</testOutputDirectory>
<finalName>${project.artifactId}-${project.version}</finalName>
</build>
Running mvn clean install causes the following.
Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:2.6:resources (default-resources) on project Hello-Maven: Error loading property file '/project/': /project (Is a directory) -> [Help 1]
...
[Help 1] https://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
Now I've tried the link, but it suggest that it's an issue with the plugins.
However, commenting this block out, and running mvn clean install again returns an almost empty jar file inside /project/target/Hello-Maven-1.0-SNAPSHOT.jar, containing only the pom and the manifest. Additionally, there aren't any plugins, only dependencies: junit and javafx.
EDIT: I realize that plugins specifically for running maven but finding information over why the error happens for "install" is difficult at best.

mvn dependency:tree fails on multi-module project due to range version confict

I have a multi-module Maven project. The parent pom contains two modules:
moduleA (POM)
moduleB (POM)
moduleA.pom contains three modules:
moduleC (JAR)
moduleD (JAR)
moduleE (WAR)
moduleB depends on C, D and E.
mvn clean package -DskipTests and mvn clean install -DskipTests runs fine but when I ran mvn dependency:tree after these commands, I faced version conflict error:
[ERROR] Failed to execute goal on project moduleB: Could not resolve dependencies for project com.org:moduleB:pom:1.0.0-SNAPSHOT: Failed to collect dependencies for com.org:moduleB:pom:1.0.0-SNAPSHOT: Could not resolve version conflict among [com.org:moduleC:jar:1.0.0-SNAPSHOT -> com.org:test:jar:[1.0.0,1.1.0), com.org:moduleD:jar:1.0.0-SNAPSHOT -> com.org:test:jar:[1.0.0,2.0.0), com.org:moduleE:jar:1.0.0-SNAPSHOT -> com.org:test:jar:[1.1.0,2.0.0)] -> [Help 1]
module C, D and E are not under my control to change above versioning.
I tried some solutions as mentioned here without any success:
mvn dependency:tree fails on trivial project

Jenkins Build Failure, Cobertura Error

I tried to build a job and it returns following error:
> Failed to execute goal
> org.codehaus.mojo:cobertura-maven-plugin:2.7:instrument (default-cli)
> on project addressbook: Execution default-cli of goal
> org.codehaus.mojo:cobertura-maven-plugin:2.7:instrument failed: Plugin
> org.codehaus.mojo:cobertura-maven-plugin:2.7 or one of its
> dependencies could not be resolved: Could not find artifact
> com.sun:tools:jar:0 at specified path
> /var/lib/jenkins/tools/hudson.model.JDK/myjava/../lib/tools.jar ->
> [Help 1]
Any idea how to resolve it?
I guess the jdk in question was above 8 which is still not supported by Cobertura: https://github.com/mojohaus/cobertura-maven-plugin/issues/30
[ERROR] Failed to execute goal
org.codehaus.mojo:cobertura-maven-plugin:2.7:instrument (verification)
on project generex: Execution verification of goal
org.codehaus.mojo:cobertura-maven-plugin:2.7:instrument failed: Plugin
org.codehaus.mojo:cobertura-maven-plugin:2.7 or one of its
dependencies could not be resolved: Could not find artifact
com.sun:tools:jar:0 at specified path
/usr/local/lib/jvm/openjdk11/../lib/tools.jar
I had to switch to https://www.jacoco.org/jacoco/ instead.
Seems like you are missing the tools jar in your JDK/JRE library you can confirm it by searching for the JAR in the lib folder if it's not present then you can add the following in your pom.xml
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.6.0</version>
<scope>system</scope>
<systemPath>${env.JAVA_HOME}/lib/tools.jar</systemPath>
</dependency>
Where ${env.JAVA_HOME} points to the JAVA you set in environment variables if you are using another JRE apart from the one set then specify the path like so /var/lib/jenkins/tools/hudson.model.JDK/myjava/JDK8/lib/lib
Hope it helps :)

Selecting optional dependencies of OSGi-bundles with Ivy

I use Ivy to resolve OSGi bundles, like org.eclipse.jdt:
<dependencies>
<dependency org="bundle" name="org.eclipse.jdt" rev="x.y.z"/>
</dependencies>
It works fine and gives me all mandatory dependencies.
My question is, how can I select some (not all) optional dependencies of transitive bundles?
I can:
Select optional dependencies of org.eclipse.jdt by activating use_xxx configuration
Select all optional dependencies using transitive-optional configuration
What I actually need is a possibility to globally activate a configuration use_yyy. Globally means (applying to all transitive dependencies):
If a module doesn't have this configuration, do nothing
If a module does have this configuration, activate it

Non-resolvable parent POM using Maven 3.0.3 and relativePath notation

After migrating to Mavent 3.0.3 Parent poms in several Projects cannot be resolved anymore.
The Projects are structured in a default manner, so I set
parent.relativePath to "../pom.xml"
superpom (located in repository)
|-rootpom (located locally: no error)
|-|-parentpom (located locally: error resolving parent)
|-|-|-module1 (located locally: error resolving parent)
|-|-|-module2 (located locally: error resolving parent)
|-|-|-module3 (located locally: error resolving parent)
|-|-|-module4 (located locally: error resolving parent)
The Error...
Non-resolvable parent POM for myGroup:myArtifactId:1.0: Failure to
find myGroup:myParentArtifactId:1.0 in
http://myRepo.net/archiva/repository/maven2 was cached in the local
repository, resollution will not be reattempted until the update
interval of maven2 has elapsed or updates are forced and
'parent.relativePath' points at wrong local POM #
myGroup:myParentArtifactId:1.0, C:\myProjectDir\parent\pom.xml, line
x, column y -> [Help 2]
... seems to indicate that the pom was searched for in the repository, so I wonder why the pom was not found locally before looked elsewhere.
I have read that maven3 might get confused when several repositories are defined in the settings.xml, but that was always when searching for a pom inside repositories and not locally.
update
Up until now we did execute the maven build on the parent-project level (parentpom) - a fact which I did not know was important, since maven2 completed successfully until now.
When using maven3 this seems to be of importance.
When executing maven3 on the root-project level (rootpom) the build finishes successfully.
So my immediate problem is solved.
Since I don't necessarily want to answer my own question maybe
someone can explain why maven3 behaves this way now or why the old
approach was wrong.
Make sure you Double-Check that the version you refer to in the child-pom is the same as that in the parent-pom. For me, I'd bumped version in the parent and had it as 3.1.0.0-RELEASE, but in the child-pom, I was still referring to the previous version via relativePath, and had it defined as 2.0.0.0-SNAPSHOT. It did not make any difference if I included just the parent directory, or had the "pom.xml" appended to the directory:
<parent>
<artifactId>eric-project-parent</artifactId>
<groupId>com.eric.common</groupId>
<!-- Should be 3.1.0.0-RELEASE -->
<version>2.0.0.0-SNAPSHOT</version>
<relativePath>
../../EricParentAsset/projects/eric-project-parent</relativePath>
</parent>
Here is answer to your question.
By default maven looks in ../pom.xml for relativePath. Use empty <relativePath/> tag instead.
'parent.relativePath' points at wrong local POM #
myGroup:myParentArtifactId:1.0, C:\myProjectDir\parent\pom.xml
This indicates that maven did search locally for the parent pom, but found that it was not the correct pom.
Does pom.xml of parentpom correctly define the parent pom as the pom.xml of rootpom?
Does rootpom folder contain pom.xml as well as the paretpom folder?
I had the same problem. My project layout looked like
\---super
\---thirdparty
+---mod1-root
| +---mod1-linux32
| \---mod1-win32
\---mod2-root
+---mod2-linux32
\---mod2-win32
In my case, I had a mistake in my pom.xmls at the modX-root-level. I had copied the mod1-root tree and named it mod2-root. I incorrectly thought I had updated all the pom.xmls appropriately; but in fact, mod2-root/pom.xml had the same group and artifact ids as mod1-root/pom.xml. After correcting mod2-root's pom.xml to have mod2-root specific maven coordinates my issue was resolved.
You need to check your relative path, based on depth of your modules from parent if module is just below parent then in module put relative path as:
../pom.xml
if its 2 level down then
../../pom.xml
Please check in logs if you have http.HttpWagon$__sisu1:Cannot find 'basicAuthScope' this error or warning also, if so you need to use maven 3.2.5 version, which will resolve error.
<parent>
<groupId>com.test.vaquar.khan</groupId>
<artifactId>vk-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../projectname/pom.xml</relativePath>
</parent>
Add following line in parent
<relativePath>../projectname/pom.xml</relativePath>
You need relative path if you are building from local parent pom not available in nexsus, add pom in nexus then no need this path
For me, it works when I double checked the parent´s "group ID" and "artifact ID" that in my case were the wrong ones and that was the problem.

Resources