mvn dependency:tree does not list dependencies of a dependency - java

I have a project that has a dependency, say X.jar, which has its own dependency Y.jar. When I include X.jar as a dependency in my project I expect that all the dependencies of X.jar be available to me in my project.
Indeed this is the case with other dependencies. When I run mvn dependency:tree it lists dependencies and their dependencies as expected but it fails to list any dependencies for X.jar.
X.jar is a custom jar from a project I wrote (I have indeed added X.jar to the local repo) so perhaps the problem lies there. When I run mvn dependency:tree on the X project it lists all dependencies correctly.
I have no idea how to debug this and any help would be appreciated.

The problem is probably that the pom.xml of your X.jar is not properly installed in your repository.
When you install a jar "by hand" in your repo :use the option -DpomFile="<path_to_your_pom>" .
If you don't do that : maven will create (and install !!!) an ultra-simple pom.xml for you (and of course it don't contains any data about dependencies).
More about installing a custom jar here

Related

Why is this jar on my classpath in a maven project?

I have a maven project with some dependencies, and the resulting artifact contains the dependencies as .jar files inside.
I could check the effective pom, but that does not show the actual .jar filenames / classes for the dependencies.
Is there any way to print dependency .jar names?
Is the name always ${artifactId}-${version}.jar?
You can view the dependencies used in a Maven build with the Maven Dependency Plugin. To view the dependencies as a list, use:
mvn dependency:list
A better representation might be the dependency tree, though, as it clearly shows relationships between imported dependency POMs and transitive dependencies:
mvn dependency:tree
If you want to see a raw list of the JARs bundled together in your build, you can also use the dependency plugin's copy-dependencies goal:
$ mvn dependency:copy-dependencies -DoutputDirectory="\${project.builddir}/bundled-jars"
$ ls -1 target/bundled-jars
Note that the destination is in quotes with the $ escaped so that it can use the Maven property without invoking shell substitution. You could also just write -DoutputDirectory=target/bundled-jars.

IntelliJ - Replace dependency to a module with dependency to the Maven artefact

When you delete a module from a Maven project in IntelliJ, all references to that module turn into compilation errors. Is there a way to exchange references to the module with references to the artefact in the Maven repo, so that everything still compiles?
Now I can get it to work after a restart of IDEA. Don't know why it didn't work at first. I delete a module, and "make project" gives me compile errors, but Maven > Reimport adds dependencies to Maven repo to replace dependencies to the deleted module. Works fine, and I can live with it being a two-step procedure :)
(My dependencies are already in pom.xml, Harmelodic. Modules are created from Maven pom.xml files.)

Add artifact to local repository from project

Is there a way add artifact to local maven repository from my eclipse project?
currently i have a project that contain many jars, and i have started using maven. what i need is to add all these jars to the local repository in an automated way without redownload them or adding them one by one and specifying their coordinates.
Make a new Maven project in Eclipse, and add all your code to the src/main directory. Now you will have lots of compile errors, because of missing dependencies.
Now start auto adding the dependencies. In Intellj you can add something using alt-enter, which also has the option to "add maven dependency". This adds that dependency from the maven repository to the pom. I do not know eclipse well enough, but it probably also has this feature.
Now, in a normal project, you will find most of your required dependencies somewhere in Maven Central. If you miss any, you can add them using manual installation to your local repository, as suggested by Manas Mukherjee
mvn install:install-file -Dfile={jar_file_name_path}.jar -DgroupId={groupId}
-DartifactId={artifactId} -Dversion={version} -Dpackaging=jar
you can write a script using mvn install command.
mvn install:install-file -Dfile={jar_file_name_path}.jar -DgroupId={groupId}
-DartifactId={artifactId} -Dversion={version} -Dpackaging=jar
You can add all dependencies in pom file as well
Thanks

Download dependencies to local repository but not to "target" one

I have a project, that has pom.xml and depends on lots of dependencies from outside (located far-far in internet..).
So, I want to download all those dependencies which I depend on to my "local repository".
This is my try (I do not need do compilation, so I use "validate" here. So I'm do not expect to have "target" folder in the end):
mvn validate -Dmaven.repo.local=C:\my\.m2\repository dependency:copy-dependencies
In the end - yes I have many dependencies been downloaded to "C:\my\.m2\repository", but some of them went to: C:\projects\myProject\java\trunk\target\dependency, like these ones:
junit-4.8.1.jar
log4j-1.2.8.jar
mockito-all-1.8.2.jar
Question is: how to make those to be downloaded to "C:\my\.m2\repository" but not to "target" of my project?
For now, because of that, another projects that depend on that are failing while building, because they are expecting to find "junit-4.8.1.jar" in local repo.
Another try:
mvn validate -Dmaven.repo.local=C:\my\.m2\repository dependency:resolve
Then those dependencies are not resolvable at all.
Could not resolve dependencies for project bla-bla-SNAPSHOT: The
following artifacts could not be resolved:
commons-lang:commons-lang:jar:2.4, log4j:log4j:jar:1.2.8,
junit:junit:jar:4.8.1: Could not find artifact
commons-lang:commons-lang:jar:2.4 -> [Help 1]
Maven did that because you invoked the goal dependency:copy-dependencies. It will copy the dependencies of the current module to
${project.build.directory}/dependency
See http://maven.apache.org/plugins/maven-dependency-plugin/copy-dependencies-mojo.html
I really can't imagine these dependencies did not get into your local repo. Fiddling around with a script may not be the solution to your problem. Try
mvn process-resources -U -Dmaven.repo.local=C:\my\.m2\repository
The -U option forces a download of all dependencies. I suggest using process-resources, although as of my understanding validate should be fine, too.

Maven war contains extra dependencies if not built standalone

I've got a war which I'm trying to reduce the dependencies contained in it's WEB-INF/lib as they're provided by the ear and if they're only in the ear it resolves some classloader issues I've had.
If I run mvn clean install in the module for the war it builds in only the dependencies I want. If I go up a level and run mvn install on the project (the war's defined as a module in the pom.xml at this level) then the war is repackaged with a lot more dependencies.
Why would the behaviour be different and how can I stop all these extra dependencies getting in?
I've tried following the advice here but saw no difference in the behaviour: http://maven.apache.org/plugins/maven-war-plugin/examples/skinny-wars.html
Maven 2.0.6, maven-ear-plugin 2.4.1, maven-war-plugin 2.1-beta-1 (I can't change the versions, I'm on a closed network). Thanks in advance.
ETA: A mvn clean install at the project level gives me the results I want. An mvn clean and then an mvn install adds in the extra jars that I don't want.
Run mvn dependency:tree to see where the (probably transitive) dependencies are coming in from.
You'll want to read "Maven the Definitive Guide" (free PDF by Sonatype) in order to get an in depth understanding of how Maven deals with Dependency management.
And please do find a way to upgrade to maven 2.1.0 (preferably 2.2.1), and the latest versions of the plugins, earlier versions are notoriously buggy.

Categories