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

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.

Related

How to get a list of the jar included in the uberjar build by maven shade plugin?

I have a (long) list of what should not be included in my uber-jar, and I would like the list of what is included, so that I can work on the configuration and remove duplicates.
I looked at the output printed by mvn package that lists a lot of
[INFO] Including aaa.bbb:ccc.ddd:jar:x.y.z in the shaded jar
but I am pretty sure some of those were not included when I used the minimizeJar option. And looking into the output jar only allows to know the included classes (very difficult to track w.r.t the dependencies)
Question: Which ever configuration I use, how can I get a list of the dependencies that are actually merged into the uberjar?
I'm not familiar with the maven-shade-plugin, but have you tried out mvn dependency:tree? It should list all dependencies and its subdependencies of your project.
Edit: I re-read your question and it seems that you need a full dependency tree of your final uber-jar. A quick look at the maven-shade-plugin page told me that any dependency will be packed into the uber-jar. This should be what mvn dependency:tree outputs, but you can list the jar's contents aswell with jar tf <uber-jar>.jar. You may have to filter out parent dirs (e. g. com/) and the manifest file.
This is not the answer I am looking for, but for the sake of information sharing, here is what I did to find which dependency to remove from the shaded-jar:
use mvn dependency:list > dep.list
then manually remove lines that are not dependencies
and sort it
visually make a diff with the list of jar present on the deployment cluster
use mvn dependency:tree and my head to find the parent most dependencies that can be removed
mark them as <scope>provided<scope/>
use <keepDependenciesWithProvidedScope>false</keepDependenciesWithProvidedScope> in the shade plugin configuration
Tedious...

list maven modules dependent on another maven module

How to list all maven modules dependent on another maven module (either directly or indirectly?
For example, mvn -pl :moduleX -amd install actually installs moduleX and all other modules that are dependent on it. What I need is to only list the other modules without any further processing.
mvn dependency:tree (as tunaki said)
mvn dependency:tree -Dverbose
If you want to display conflicting dependencies that were omitted from the resolved dependency tree.
mvn dependency:tree -Dverbose -Dincludes=commons-collections
If you want to specifically see the dependency tree parts involving Commons Collections (for example).

Add list of directories to Maven

I have an environment variable called $MYCLASSPATH that contains a set of directories that contain JARS. An example of my environment variable could be the following:
/project1/jars/:/project2/jars:/project3/jars
I also have a maven project that contains some external dependencies that are defined in the pom.xml. However, I want to include all the directories listed in the above environment variable in Maven since some JARS are required for the compilation.
Without Maven I could do this:
javac -cp "$MYCLASSPATH" path/to/my/java_file
How can I add all these directories to Maven?
As an addition to crowne's answer, once you know the coordinates the external dependencies would have to be added to your local maven repository using mvn install:install-file
For example
mvn install:install-file -Dfile=path_to_your_jar -DgroupId=X -DartifactId=X -Dversion=X -Dpackaging=jar
Your POM can then contain these dependencies in the same way normal dependencies are declared eg.
<dependency>
<groupId>X</groupId>
<artifactId>X</artifactId>
<version>X</version>
</dependency>
Each jar should be identified by a specific set of maven coordinates
https://maven.apache.org/pom.html#Maven_Coordinates
This lets us know which version of the jar the project is dependent on and where to get it from in the maven repository.
It also allows the project to be built correctly from another computer with a different set of environment variables.
So you should define your dependencies in your pom using the correct maven coordinates.

mvn dependency:tree does not list dependencies of a dependency

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

Ivy loading unnecessary dependencies?

I'm migrating a maven project to ant + ivy (it's an arbitrary decision, and it has to be done ).
The thing is that comparing both WAR files, the ant and the maven one, the former has a lot more 3rd party jars than the first one...
At first I thought that they were transitive dependencies but when I run mvn dependency:tree they don't appear there.
Any ideas?
EDIT:
Just for the record, I'm aware of the scope attribute of maven. I'm excluding all the provided and test dependencies in the ivy.xml file
You can use the ivy:report Ant task to generate a report of the dependencies resolved by Ivy. In this report, you can see where the extra dependencies are comming from.
Maarten

Categories