How to figure out in what pom a dependency from effective pom is defined in IntelliJ Idea? - maven

I have a big project that has parent pom, this one has another parent; in the project's pom file another project with bom file is included as a dependency, etc.
I click on pom and generate effective pom. Inside I see a dependency, for example this one
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
Is there an easy way in IntelliJ to find the pom where this dependency is defined?

In Ultimate version it's possible to generate a diagram for all maven dependencies:
https://www.jetbrains.com/help/idea/work-with-maven-dependencies.html#maven_dependency_diagram
Or you can execute mvn dependency:tree to build full dependency tree.
https://maven.apache.org/plugins/maven-dependency-plugin/examples/resolving-conflicts-using-the-dependency-tree.html
https://www.mkyong.com/maven/maven-display-project-dependency/

Related

How does maven resolve the dependencies of the main dependencies on which our application is build?

I am trying to understand maven a little more. How is maven able to download the dependencies of the main dependency of the application? For example assuming my application has main dependency like this:
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.7.0</version>
<scope>provided</scope>
</dependency>
Now, when maven downloads this jar , it downloads the dependencies for this jar as well. For example, see the screen shot below:
As can be seen, maven has not only downloaded the hadoop-hdfs-2.7.0.jar but also all it dependencies.
Now, my questions is how maven knows what are the dependencies for the "top-level" dependency, that is in this case the "top-level" dependency is hadoop-hdfs, so what all jars it has to download for this?
I see this as well in the .m2/respository for hadoop-hdfs:
I opened the .pom file, the contents are (partly):
<project>
....
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.7.0</version>
<description>Apache Hadoop HDFS</description>
<name>Apache Hadoop HDFS</name>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-annotations</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-auth</artifactId>
<scope>provided</scope>
</dependency>
<dependencies>
...
</project>
What is this hadoop-hdfs-2.7.0.pom ? Does this file give information to maven what are the dependencies to be downloaded for hadoop-hdfs-2.7.0.jar?
Can anyone help me clear these things?
First of all you are right, the hadoop-hdfs-2.7.0.pom tells Maven
about the libraries that hadoop depends upon. But, when using hadoop
as a dependency in your project, maven uses the below strategies to
finalize the list of dependencies in addition to using the
hadoop-hdfs-2.7.0.pom.
If a dependency is specified with groupid, artifactid and version in the current project under the dependencies tag, it takes the first
precedence. This is how hadoop-hdfs got added in your project.
Dependency Management takes the next precedence. When a dependency is specified only with group and artifact id's under dependencies tag
but at the same time, the dependency is defined under
dependencyManagement tag with version and transitively inside hadoops pom.xml also,
the one under the dependencyManagement tag will be given preference.
Dependency Mediation takes the last precedence. Dependencies are resolved using dependency mediation. Meaning, in your case the
dependencies mentioned inside hadoop-hdfs-2.7.0.pom are the transitive
dependencies (indirectly depends on these dependencies since your
dependency "hadoop-hdfs" requires it) of your project and this process continues
recursively until all child dependencies are resolved.
Note: There are other features such as excluding dependencies, marking
one optional and importing a list of dependencies. But they are used
sparsely. More information with examples can be found in the below URL
[https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Management][1]

Excluding transitive dependency not working

Project A references Project B. Project B has included a local dependency. This local dependency unfortunately has a dependency to net.java.dev.designgridlayout in version 1.5.
We want to use net.java.dev.designgridlayout in version 1.11 in Project A but we are not able to "overwrite" the dependency. Eclipse always uses the dependency from Project B.
We already tried to exclude the 1.5 version from the local dependency, but it doesn't work.
The strange thing is, that Eclipse successfully resolves a class that has been added with version 1.11. For an already existing class, however, eclipse resolves it from the transitive dependency from de.someCompany.
Project B:
<dependencies>
<dependency>
<groupId>de.someCompany</groupId>
<artifactId>fs-client</artifactId>
<version>5.1.209</version>
<exclusions>
<exclusion>
<groupId>net.java.dev.designgridlayout</groupId>
<artifactId>designgridlayout</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>net.java.dev.designgridlayout</groupId>
<artifactId>designgridlayout</artifactId>
<version>1.11</version>
</dependency>
</dependencies>
Project A:
<dependencies>
<dependency>
<groupId>Project-B</groupId>
<artifactId>Project-B</artifactId>
<version>1503.01</version>
</dependency>
</dependencies>
I also tried to include the 1.11 dependency in Project A.
We even tried to install the DesignGridLayout V. 1.11 in the local dependency and to change the groupID and artifactId to something different, but it cannot even be found by Eclipse for some reason. If it would be possible to include the DesignGridLayout with another groupId and artifactId, I think it would work.
mvn install:install-file -Dfile=lib\designgridlayout.jar -DgroupId=com.company.designgridlayout -DartifactId=design-grid-layout -Dversion=1.11 -DgeneratePom=true -Dpackaging=jar -DlocalRepositoryPath="%USERPROFILE%\.m2\repository"
Not sure - but:
Your project A has a dependency to itself? Shouldn't it use project-b?
Its not a good idea to change group or artifact id's as maven can no longer detect its the same artifact. If you do a custom version the version number should be enough.
If you add the dependency in your own pom then you don't need to exclude the artifact, since the groupId and artifactId are the same. The version in your own pom will win in project-b. If project a defines that dependency again itself that version will win.
I would do a mvn dependency:tree on project-a pom to see where the dependencies come from.
For eclipse: it indexes the local repository. In the maven settings there is a re-index button. So if you manually copy jars in there that may help eclipse to find the artifact. But that workaround would need to be done on every machine. I would not count that as solution. In the maven world artifact-resolution is an infrastructure issue and should not be handled per project. The way this is done should be transparent through the settings.xml

Does Maven need to explicitly specify the dependency that Spring/Hibernate dependented?

I'm new to Maven, I try to use Maven with Spring, Hibernate in my project. After go though the Spring and Hibernate reference, I found that "there is no need to explicitly specify the dependent liberaries in POM.xml file for such Apache commons liberaries".
My questions is that : If my other parts of project refer to Apache commons liberary, such as commons-io, SHOULD I explicit specify this dependency in POM.xml file?
You should define those dependencies in Maven which your project is using. For example, even though some library depends on commons-io but if your code needs this then you should directly define commons-io in your pom.xml
You should not worry about the dependencies of the libraries you have defined in your pom.xml. Maven will do that for you.
Maven is used to avoid the issue of having to run down JAR files that are dependent on other JAR files. Of course you do not HAVE to use maven to do this, but you should. Maven will automatically download the dependent JAR files of the JAR file you require. THe hibernate-entity manager JAR file, for example, has over 100 dependencies and maven does the work for you.
Anyway,even if you do add the commons-io file to the build path/classpath of the maven project,and then update the project configuration, maven will kick it out.
You can provide a lib name on a site like mvnrepository.com to see what it depends on (e.g. take a look at a section called "This artifact depends on ..." in case of spring-webmvc library). Those dependencies (which your artifact depends on) are called transitive dependencies. You don't have to specify these in your pom.xml as maven will resolve them for you.
For the sake of readability you should only state those dependencies in your module that you rely on directly. You want JUnit to test your software, only declare JUnit; you need hibernate to use ORM, declare hibernate, and so on. Leave the rest to Maven.
And most of the time you should state what you intend to use in the very module you want to use it in. So if you want to use a dependency in more than one module, consider moving it into a dependencyManagement block in a parent pom and referencing it from there in the module you want it in.
parent pom.xml
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
child pom.xml
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
This guarantees you version-stability and still allows you to find out what a module uses by only looking in it's pom (and not all over the place).

Maven is picking up the oldest snapshot in Archiva - why?

I have a projectA-parent with the following structure:
projectA-parent|
----------------|projectA
----------------|projectA-core
----------------|projectA-api
The module projectA simply defines dependencies to core and api [1], so I can just define a dependency to projectA in other projects that need both the core and the api. I deploy the project to Archiva (1.4-M2), to a SNAPSHOT repository.
I have another project X where I define a dependency to projectA, and this afternoon the goal "mvn compile" started failing with unresolved compilation issues. Upon investigation with "mvn dependency:tree" on that project I discovered that, under projectA, the dependency to projectA-core was declared as "runtime". While this morning it was like this, I performed several mvn installs since where the scope is "compile" (see [1], where the most recent pom definition is listed).
Looking at archiva, I see the following files for this project (abbreviated):
projectA-0.0.7-20120712.084920-61-tests.jar
projectA-0.0.7-20120712.084920-61-tests.jar.md5
projectA-0.0.7-20120712.084920-61-tests.jar.sha1
projectA-0.0.7-20120712.084920-61.jar
projectA-0.0.7-20120712.084920-61.jar.md5
projectA-0.0.7-20120712.084920-61.jar.sha1
projectA-0.0.7-20120712.084920-61.pom
projectA-0.0.7-20120712.084920-61.pom.md5
projectA-0.0.7-20120712.084920-61.pom.sha1
projectA-0.0.7-20120712.172412-87-tests.jar
projectA-0.0.7-20120712.172412-87-tests.jar.md5
projectA-0.0.7-20120712.172412-87-tests.jar.sha1
projectA-0.0.7-20120712.172412-87.pom
projectA-0.0.7-20120712.172412-87.pom.md5
projectA-0.0.7-20120712.172412-87.pom.sha1
projectA-0.0.7-20120712.180733-90.pom
projectA-0.0.7-20120712.180733-90.pom.md5
projectA-0.0.7-20120712.180733-90.pom.sha1
Here's the interesting part: if I delete projectA from my local .m2/repository, even if I "mvn compile -U" I will get th 08:49 version, not the 18:07 one! This means either maven or archiva are resolving the 1st sNAPSHOT of the day and not the most recent one.
Why is that, and how can one solve this?
[1]:
<dependencies>
<dependency>
<groupId>com.projectA</groupId>
<artifactId>projectA-api</artifactId>
<version>${project.version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.projectA</groupId>
<artifactId>projectA-core</artifactId>
<version>${project.version}</version>
<type>jar</type>
<scope>runtime</scope>
</dependency>
</dependencies>
What is the packaging of your projectA ('jar' or 'pom'). It only exists a 'jar' artefact for 08:49.
I suppose you changed the packaging to 'pom'. Thus if you declare the library as dependency in other projects you have to set <type>pom</type>.

Adding POM type dependency using m2eclipse, unable to resolve

I am trying to add Hector dependencies to my POM. My IDE is Eclipse, and I am also using m2eclipse. Adding dependencies of JAR type is not a problem, but this dependency is of type POM. I have tried almost everything usual including cleaning, building, and using import scope but nothing seem to have helped. When I try to add import me.prettyprint.hector.api.Serializer;
I get the error "The import cannot be resolved".
Is there anything else I need to do to use POM type dependencies or is there a better way of using dependencies of POM types in the project?
I believe his question is not as obvious as simply including the necessary dependency. I have experienced this problem too and am looking for a solution. The problem can be clearer stated as the following:
Let's say I have two maven projects (project A and project B). Project A is a simple web-app which wants to include dependencies as stated in project B. However, project B packaging type is "pom". This should allow all of project B's dependencies to be included into project A. Here is an example:
Project A (packaging is "war"):
<dependencies>
<dependency>
<groupId>com.foo</groupId>
<artifactId>B</artifactId>
<version>1.0</version>
<type>pom</type>
</dependency>
</dependencies>
Project B (packaging is "pom")
<dependencies>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
What we'd like to see in Eclipse is when you run maven eclipse:eclipse on Project A, that you can see the commons-lang-2.4.jar file as a dependency under project A such that you can resolve it in your code when imported. This is not happening and I'm still looking for such a solution.
The error indicates that the relevant class is missing in your classpath. A search of this class indicates, it is available in hector-core
This discussion indicates how this dependency can be imported, viz. adding the following entry to your project pom (or choosing this appropriately in m2eclipse).
<dependency>
<groupId>me.prettyprint</groupId>
<artifactId>hector-core</artifactId>
<version>0.7.0-29</version>
</dependency>

Resources