In gradle, the command gradle dependencyInsight --dependency <artifact-name> prints a reverse dependency graph for a specific artifact.
Is there something equivalent in Maven?
I'm aware of mvn dependency:tree. This is the equivalent of gradle dependencies. However this is not what I'm asking about.
One way is to use filtering with maven dependency plugin
mvn dependency:tree -Dincludes=[groupId]:[artifactId]:[type]:[version]
Check the link, there is an example. The output isn't actually in reverse but it shouldn't be too hard to read it in reverse by yourself.
Eclipse-IDE has some nice features which allows dependencies to tracked without using command line. Open pom.xml, open dependency hierarchy tab and there, dependencies can be filtered. Other IDEs might have similar features as well.
Related
Is there an easy way for me to trace a jar back to which BOM artifact it is from?
I need to upgrade org.dom4j:dom4j, but I need to figure out which of the artifact brings it in. Is there a way to print out all the transitive dependencies in Gradle? Thanks!
Depending on if you use gradle wrapper or not, gradlew dependencies or gradle dependencies should give you the dependency tree, but it doesnt show which BOM sets a specific version.
But it will show you if a dependency forces another dependency to chance its expected version.
I would recommend pipe it to a file to read easier (windows == gradle dependencies > dependencies.txt)
I need to sort the dependencies in the dependencyManagement section of a POM file that is used as a parent for all projects of my team's portfolio.
The motivation for this is similar to the one described here. In my case, I am just trying to harmonize the versions used throughout our portfolio, in order to avoid the recurring nightmare of version discrepancy: due to copy-paste, some projects use a version of a dependency while others use another version. Another motivation is to have only one place where to manage dependencies. What I am doing is essentially merging dependency specifications from all modules into a giant dependencyManagement section of a parent POM. (EDIT: In the process of looking for an answer to this need of mine, I learned that such a POM is what Maven calls a BOM or "bill of materials".)
However, this task requires that I define the version of each dependency in our parent POM. Doing so, I find myself putting the dependency specification somewhere in what is a growing list of dependencies. It gets more and more difficult to add a dependency and find out whether the dependency is already specified. But that would be much easier if I could sort the dependencies, for instance by group ID.
Is there a plugin that serves that purpose of reordering the dependencies? As a last resort, I will end up writing a small program that will read the XML file from the parent POM and output it sorted.
The recently released version 0.2.0 of BOM Helper Maven Plugin now has the sort goal that does exactly that.
You need to add the plugin to your pom:
<plugin>
<groupId>com.commsen.maven</groupId>
<artifactId>bom-helper-maven-plugin</artifactId>
<version>0.2.0</version>
</plugin>
You can configure it to run on every build, but I would rather run it manually only when I add/change a dependency. Something like:
mvn bom-helper:sort -Dbom-helper.inplace=true
should do the job. See the docs for more options.
I used sortpom. See more about parameters at https://github.com/Ekryd/sortpom/wiki/Parameters
mvn com.github.ekryd.sortpom:sortpom-maven-plugin:sort -Dsort.encoding=UTF-8 -Dsort.sortDependencies=scope,groupId,artifactId -Dsort.sortPlugins=groupId,artifactId -Dsort.sortProperties=true -Dsort.sortExecutions=true -Dsort.sortDependencyExclusions=groupId,artifactId -Dsort.lineSeparator="\n" -Dsort.ignoreLineSeparators="false" -Dsort.expandEmptyElements=false -Dsort.nrOfIndentSpace=2 -Dsort.indentSchemaLocation=true
The easiest way to sort your dependencies is to use the sortpom maven plugin.
It is very easy to use. Just go to your project directory, open terminal and run the following command:
mvn com.github.ekryd.sortpom:sortpom-maven-plugin:sort -Dsort.keepBlankLines -Dsort.predefinedSortOrder=custom_1
To find more about the project, check their github repo: https://github.com/Ekryd/sortpom
The command mvn -U forcing all project dependencies to get updated from remote repository.
Is it possible to force such update for specific selection of dependencies, by explicit selection / by some logic?
The reasoning:
I don't want to force checking all the dependencies we have because how time consuming it is. All I need, is to verify a few of them or even specify only one dependency. So, such solution is highly desired.
There are two maven plugins that may help you here.
The first, dependency, will simply download the given version of a dependency:
mvn dependency:get -Dartifact=groupId:artifactId:version
The second, versions, offers some behaviors which you may also find helpful.
By running mvn versions:use-latest-releases -Dincludes=groupId:artifactId your project's pom will be updated with the latest release version of the dependency specified by the '-Dincludes' flag. You could then run the first command to download the version now referenced by your pom.
Both of these behaviors can be heavily customized and automated to do some quite awesome things. To get more help on a plugin goal, run: mvn plugin:help -Ddetail=true -Dgoal=goal
Example: mvn versions:help -Ddetail=true -Dgoal=use-latest-releases
For further information:
versions, dependency, and plugins
With maven-3, it uses aether to resolve dependency.
Unfortunately, "mvn dependency:tree" use legacy (maven-2) resolution engine.
How do I find out the true dependency resolution for maven-3. I'm running into an issue where "exec:exec" creates different classpath then "dependency:tree".
In maven 3 - compatibility notes, it says I need to use "-X" and look at the log but there is no pointer what to look for.
Also, "assembly:assembly" brings in different 'jar' than when I print out classpath from "exec:exec".
dependency:tree is the correct way to get the project dependencies. Since version 2.5 of the plugin it now resolves the tree using aether.
exec:exec runs the maven exec plugin. The classpath it generates is based on the plugin dependencies, if specified. It is relevant only for the purpose of the plugin execution and not to be taken in the context of project.
The similar explanation holds good in case of assembly:assembly. The jars that it brings in entirely depends on the plugin and assembly-descriptor configuration.
Since version 2.5 of the Maven Dependency Plugin, dependency:tree works with Maven 3 (see the bug report, and the release notes)
I have a Maven project with a number of dependencies. I can run mvn dependency:tree to get a dump of all the artifacts that I depend on, plus their transitive dependencies, etc, turtles all the way down. However, I can sometimes run a non-default lifecycle goal like rpm:rpm or javadoc:javadoc and it will complain about missing an artifact that wasn't listed in dependency:tree. Is there a way to tell Maven "calculate dependencies as if you were going to run goal X:Y, then give me a dependency tree for that"? Am I missing something?
You're talking about running plugin goals, not lifecycle phases. Plugins have their own dependencies that are unrelated to the project dependencies. If you run Maven with verbose output (-X/--debug command line option), it will show you the dependency trees of all the plugins. This is the only way I've ever found to see a plugin's dependencies. The output is huge, and it will take you a while to orient yourself the first time through, but the trees are pretty obvious when you find them. Try searching for occurrences of the plugin's artifactId. That will get you where you want to be.