Get all 3rd Party dependencies module is using in Maven - java

We have a multi-module maven project. I have a use case where I want to know to get a list of all the dependencies which is being used in code with version:
If its declared in pom and being used.
Not declared in pom but still being used (transitively imported).
Bonus would be if the approach can exclude the deps which are being declared in pom but not being used in the code.
Please suggest the best way to achieve this. TIA.

Just use the mvn dependency:tree command.

There's the Maven Dependency Plugin:
The dependency plugin provides the capability to manipulate artifacts. It can copy and/or unpack artifacts from local or remote repositories to a specified location.
with its tree goal:
Displays the dependency tree for this project.
Regarding your bonus there's the analyze goal:
analyzes the dependencies of this project and determines which are: used and declared; used and undeclared; unused and declared.
and the analyze-only goal:
is the same as analyze, but is meant to be bound in a pom. It does not fork the build and execute test-compile.

Related

How does the library dependency is inherited/works in maven?

I have gradle scala project(Lets say "A") which uses the library(Lets say "X"). And i have used this Library-"X" in the build.gradle of Project-A as shown below and i'm using the Library-X classes to perform some operations.
build.gradle of Project-"A"
implementation "com.x.y.z.stone:central-dl-spark-common-integration-logging:0.4.0-SNAPSHOT"
Now Project-"A" is itself a library which is used in the Project-"B" and this Project-B is maven build. Now when i call the Project-A function which is acting as library in-turn uses Library-X function. But Project-B is complaining that class not found error as shown below. This is the Library-X class
java.lang.NoClassDefFoundError: com/x/y/z/integrationlogging/IntegrationLogging$
My question is do we even need to declare the Library-X in the pom.xml of Project-B as well?
No: if Project-B imports Project-A, and Project-A has an internal dependency on Library-X, you usually don't need to do anything and it will resolve automatically.
Some exceptions may include conflicts and libraries that exist in a private nexus. For the former, you need to resolve the conflict in the pom.xml. For the latter, you need to make sure your repo is accessible from wherever you're compiling.
No.
Dependencies are transitive "by default" unless the opposite is specificied (either with exclusions or scopes (a test scoped dependency won't be transitively propagated for instance)).
EDIT: you can use mvn dependency:tree to get a better understanding of your dependencies.
One possibility is that library X is pulled in different incompatible versions.
Thank you for the having look at my post. By adding the Library-X under tag in the Project-B pom.xml, worked.Something shown below
<artifactSet>
<include>x.y.stone:central-dl-spark-common-integration-logging</include>
</artifactSet>

Transfer a Maven dependency + all transitive + parent POM

My project stores its dependencies in a Maven repository. I would like to be able to move certain dependencies to another Maven repository. The move is the easy part. But it's what to move that is difficult for me to get right.
In order for build tools such as Maven or Gradle to be able to use the moved dependency in a build, I need to also transfer (1) transitive dependencies (recursively) and (2) the project's parent POM file, performing (1) again on the parent until all nodes in the dependency graph are exhausted.
This seems like a very common usecase and I'm hedging my bets on the fact it has been implemented many times over.
Question: Are there common libraries that implement this functionality out-of-the-box?
If not, I'll probably have to implement a custom POM parser. Are my assumptions above about what needs to move correct?
The copy-dependencies goal of the maven-dependency-plugin may help you on this task:
Goal that copies the project dependencies from the repository to a defined location.
It also provides an option, addParentPoms to also copy the parent poms required by the build (hence, the whole hierarchy). This option is not enabled by default though.
Moreover, via the different include/exclude options (by group Id, by artifact Id and so on) you may filter what you actually need to move.
Via its excludeTransitive option you may also check whether transitive dependencies are required or not: by default is set to false, hence transitive dependencies will be copied too.
Via its outputDirectory option you can specify where to copy dependencies, transitive dependencies and hierarchy of pom files, according to any specified filter.
You may also be interested in the combination of the purge-local-repository goal of the maven-dependency-plugin, to delete from your local repository whatever required by the project (including transitive dependencies, hierarchy of pom, plugin dependencies) and the go-offline goal to prepare the project for off-line mode, that is, to resolve (download) whatever required. Again, both goals provide include/exclude mechanisms and transitive dependencies management so that you may refine your strategy and outcome.
mvn dependency:list will give you list of all dependencies of your project, including transitive dependencies and dependencies specified in your parent pom.

Maven dependency grouping via poms

I have a multi-module maven project that uses some dependencies from netflix. The trouble is that those dependencies are built using gradle and use a runtime scope. When I build my project I then have only the direct dependency loaded but not it's own dependencies.
To fix that I added dependencyManagement entries to transform all the runtime scopes to compile scopes. They usually go in pack of 10 to 20 entries so what I want is to be able to make some pom that would only address this issue and be able to load it any time I need a dependency.
I know this can be done as i've read Maven pull dependencies via middle pom file and How to use POMs as a dependency in Maven? but I was wondering how I may carry those poms inside my current multi-module project.
I'd like to have those in my parent module and avoid needing to create one sub-module per pom. Like pom-dep1.xml, pom-dep2.xml... and have those bundled in my build. Is that possible ?
Thanks

Maven: How to find artifacts which depend on another artifact

Let us say, ArtifactA depends on ArtifactB and ArtifactC also depends on ArtifactB
I understand that "mvn dependency plugin" can help list the dependencies of a project/artifact.
But how about the reverse? If I want to find the list of projects/artifacts which depend on a given artifact?
From the above example, given ArtifactB, I would like to get ArtifactA and ArtifactC
How can I achieve this?
Maven can only operate on the current project, so it can only detect dependencies between from the current project (or sub-modules) to other projects (including sub-modules of the current project).
So what you can do is search for specific submodules depending on other submodules:
mycompany:parent
/ \
mycompany:child1 mycompany:child2
/ / \
mycompany:grandchild1 mycompany:grandchild2 mycompany:grandchild3
This is how you can find all subprojects that have dependencies to grandchild3:
mvn validate -pl child2/grandchild3 -amd
This will run the validate phase on all projects within the current project that depend on grandchild3.
It is not easy at all. One option is using m2eclipse which has a feature called Class search. In the maven repositories view, right click a repository and enable full index.
Then Navigate > Open type from maven - there you can look through all artifacts available based on java package convention.
Another option is to develop your own tool based on JarAnalyzer for instance, that will accept a JAR file and search through your local maven repository, looking for the most appropriate artifacts that satisfy the the imported packages/classes from the JAR.

flickrj dependencies

does anyone know the java packages that flickrj is dependent on? I'm going through the slow and painful compile - jarify - run - locate-library - repeat loop.
There's a Maven POM that references flickrj by name, you may find the listed dependencies useful. Note you'd also have to resolve the referenced project's transitive dependencies.
If you use the Maven dependency:copy-dependencies goal you can get all the transitive dependencies downloaded to a directory (you can also use the dependency:sources goal to get their sources)
You can also see the dependencies for flickr-api (a wrapper for flickrj) in its Maven pom.

Categories