I have a complex project with a lot of Maven libraries. From time to time I am facing the problem of a library version conflict. I can open the class which is conflicting and see all libraries a class is packed in. But unfortunately I don't find an easy way to check where these libraries are referenced in the Maven dependencies. Do you have an idea how to find a library in the Maven tree?
You need Maven Helper plugin
https://plugins.jetbrains.com/plugin/7179-maven-helper
It has a dependencies viewer and lets you resolve conflicts right there. Open your pom file after installing the plugin and you will see another tab at the bottom of the editor window.
Right click on any dependency to bring up the context menu and you will see an option to exclude it. It will add exclude to your pom file. That obviously won't work for uberjars.
Going more low-tech, you can let maven tell you the answer with the command
mvn dependency:tree -Dincludes=groupId:artifactId
You can also invoke this command simply from an Intellij terminal window.
mvn dependency:tree will print the entire tree, the includes addition will let you filter on a specific artifact making it easier to search (although I've been known to just copy the entire tree into a scratch file and just do searches on it there).
If you don't know the groupId or the artifactId is already unique enough, you can filter like this:
mvn dependency:tree -Dincludes=*:artifactId
Related
Is there a way to easily link a specific Java class to a jar that is listed by mvn dependency:tree? I tried -X to and -Dverbose to add verbosity but it didn't do it.
I have an import statement that I suspect is pulled from one jar in one version of my pom and from another when I change some dependency declarations in the pom because the API becomes different. But I know of no easy way to trace the imported class to a jar in the dependency tree.
To locate the maven dependency for a specific import, I usually do a Go to -> Declaration (⌘B on OSX) followed by clicking the Scroll from Source button in the Project tab like so:
I noticed that the first time I did this, the source for junit-3.8.1 was not present in my local maven cache due to which, Intellij IDEA decompiled the Testcase class. The Scroll from Source did not work for this de-compiled source. However after clicking the Download Sources and retrying the Scroll from Source worked fine as above.
This approach has stood me well for checking the maven dependency which is the source of that one off import.
I know this is trivial, but I bet I am not the only developer being annoyed by this issue:
I have a maven project building a Spring Hibernate frameowrk for a webapp.
I am building using Eclipse.
I use Eclipse shortcuts to import dependencies and often do so almost subconsciously.
Somewhere in my project dependencies I have a jar that includes the following package.
edu.emory.mathcs.backport.java.util
I often accidentally import this package instead of java.util and get compile or runtime errors.
QUESTION: How can I find out which dependency includes this package so I can (hopefully) exclude it using the Maven enforcer plugin.
This does not directly answer your question but may help you prevent the accidental import in the first place:
In Eclipse you can add a type filter in Preferences → Java → Appearance → Type Filters.
From the description there:
All types whose fully qualified name matches the selected filter strings will not be shown in the 'Open Type' dialog. They will also be ignored in content assist or quick fix proposals and when organizing imports. For example 'java.awt.*' will hide all types from the awt packages.
You can run mvn dependency:tree from the command line. It will show a tree of your dependencies and their transitive dependencies.
Within Eclipse you can open your pom.xml using the Maven POM Editor (default editor for POM files) and click on Dependency Hierarchy.
Within this view you can use a filter or select the dependency on the right side and the dependency tree for this dependency will be shown on the left side.
I recently encountered this issue with a Maven project in IntelliJ IDEA.
You can view transitive dependencies using the Maven command mvn dependency:tree to figure out where this package is creeping in.
However, it's worthwhile to mention that this problem can occur for Gradle or Maven projects. If you are using IntelliJ, then another workaround that will work for all of your projects - regardless of build tool - is to exclude the package altogether from the IDE's editor settings.
The steps to exclude the edu.emory.mathcs.backport.java.util package when using auto-import for all of your projects opened in IntelliJ are as follows (this is valid for IntelliJ IDEA 2022.2):
Ctrl + Alt + S (or click on File, then Settings)
Editor > General > Auto Import
Under the exclusion section, add the following exclusion with an IDE scope: edu.emory.mathcs.backport.java.util.*
Reference Screenshot
We do not need to specify the versions and other parameters of Maven dependencies and plugins we use. We can do it only once in parent POM or may be in some other way and inherit that properties in our children POMs.
Unfortunately for many times I have met the projects where I cannot find the real parameters of dependencies and plugins that easily. I.e. the versions of dependencies are empty, but in parent POM there is no information about this.
So my questions are:
What are all the possible ways to specify the parameters of dependency and plugin in Maven? I.e. where can I look for version number if it is not present neither in plugin in child POM nor in plugin management in parent pom?
Is there any Maven command to find out where Maven takes these parameters from? For example, I may use the dependency:tree command. It will print a list of dependencies which will help me to understand the version dependency I am stuck about. But this is a hack, and it cannot help me with plugins. May be there is something better?
Before executing any command, Maven builds the "effective POM", see this stackoverflow answer for more details. You may view the effective POM for any project by running mvn help:effective-pom. Typically when I run this command I redirect the output into a file so I can view and search it in a text editor.
You asked about specific parts of the POM, dependencies and plugins. You're on the right track, the maven-dependency-plugin helps. The goals I use most often are dependency:tree, dependency:resolve, and dependency:resolve-plugins. The first two assist with project dependencies, the last one with plugins. Add the -U option to force Maven to update dependencies regardless of the update policies/repository metadata values.
It is also helpful when troubleshooting to start with an empty local repository.
Last but certainly not least, Maven will give warnings when a build uses a plugin without a specific version. ALWAYS specify an explicit version to fix the warning and avoid issues like this one.
I had a situation in my project where two dependencies relied on a different version of a jar. Just showing the dependency tree only showed the newer version and where it was used, so I ended up going through the POM files (I had an idea where it might be) to find the source.
My question: is it possible to get a dependency tree that includes these dependencies that were removed (showing why they were removed). I seem to recall the m2eclipse plugin can do it, but I'd prefer a command line tool (since I'm not using eclipse).
edit:
Specifically, I already tried the dependency tree, including running with full debug output (-X). As far as I could tell, it doesn't show when it's masking these dependencies.
Just try the maven-dependency-plugin use the tree goal to look at the tree of dependencies. But it might be a good idea to use Eclipse via m2e plugin for such problems.
I want to use VIM for Java programing, but I found it is very cumbersome to find and set a Maven dependence at command line.
Is there a plugin to search dependence like "apt-cache search"? and adding a dependence to pom.xml like "apt-get install"?
This should be a basic function, Why there is no such plugin ? Thank you :)
There is no plugin, and I can't answer why.
However in the IDE I use (IntelliJ) and probably others, there are help pop-ups that ask you if you want to add a dependency to a POM automatically (most likely only for things in your local repository). Also, when editing a POM, anything already in your local repository will autocomplete as you type out the dependency (and if there are multiple versions, the list of versions to choose from is displayed). Just letting you know in case switching to an IDE would help you.
I think the best you can do for search is use a browser off to the side with MVNRepository loaded up. You can search for libraries and when you see the one you want, you can copy-paste the example dependency into your POM. Note a small fraction of dependencies listed there are not in the most common public repositories and require you to customize your repository list.
maven dependency plugin could help you a little bit on listing/analyzing dependencies. But it cannot add a dependency for you.
in fact, if you prefer VI to edit files, why not just open pom.xml in your vim, and edit the file directly?
If you prefer working with Vim, you can still do it in Eclipse: try Vrapper on the free side. On the non-free side, you can try Viable or viPlugin.
Eclipse then has Sonatype's m2e plugin, which lets you add dependencies and plugins with a sleek search wizard (not to say a whole bunch of other helping features).