question about maven dependency - maven

i loaded the maven project in eclipse then found sth wrong with pom.xml file, when i clicked the 'overview' tab(m2eclipse), it said:
Failed to read artifact descriptor for commons-logging:commons-logging:jar:1.1.2-SNAPSHOT
when i clicked the dependency hierarchy tab, it showed 'Project read error', however i have no problem to run 'mvn dependency:tree' from command line and can see there is a dependency on commons-logging:
commons-logging:commons-logging:jar:1.1.1:compile
just don't understand where the commons-logging 1.1.2-SNAPSHOT comes from. any idea ? Thanks.

In order to determine where the dependency commons-logging comes from (even without the .pom editor and its dependency editor) open the console and execute the following command:
mvn dependency:tree -Dverbose -Dincludes=commons-logging
This will show all dependencies of commons-logging.
I looked through the dependency tree, but did not find any reference to 1.1.2-SNAPSHOT.
Finally, adding the following dependency to my pom.xml solved the problem:
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.2-SNAPSHOT</version>
</dependency>

eclipse by default does not use the same maven installation as your command line. Depending on your m2eclipse version it might use an embedded maven snaphsot (with strange behaviour). Check
eclipse -> Window -> Preferences -> Maven -> Installations
and add your external maven installation (prefer current version 3.0.3) to the list and use this as default.

It's most likely coming into your project transitively. You can check the "Dependency Hierarchy" section of your pom editor in eclipse and see where it's coming from (search for commons-logging in the right top box). Also, I cannot see a 1.1.2-SNAPSHOT version of commons-logging on central so most likely someone has made a mistake in a dependency pom.

Related

How do I figure out where a version is managed?

I've got a maven project A. This project depends on project B, which again depends on mysql-connector-java.
Project B depends on mysql-connector-java:8.0.27. However, no matter what I do project A insist on using mysql-connector-java:5.1.39.
Maven tells me :
[INFO] | +- mysql:mysql-connector-java:jar:5.1.39:compile (version managed from 8.0.27)
Neither project A nor project B manage the version in any particular way, apart from the regular dependency inclusion which B does :
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
How do I figure out where mysql-connector-java is managed? I've tried to search across the projects I manage - unable to find this specific version to be mentioned anywhere.
One way to analyze the dependencies a package has is by running the following command:
mvn dependency:tree
This will print a complete tree showing which dependency (and version) has been pulled into the project. This is a great way to see where the connector package with version 5.1.39 is coming from.
Once you find it, the best way to address it would be to uptick the package which is using the older version of the connector. Another (not the best) option would be to use an exclusion in package A's pom file.
Besides mvn dependency:tree -Dverbose, calling mvn help:effective-pom might also help to determine where it comes from.

IntelliJ How to force downgrade dependency version?

I have a persistent problem with maven dependencies version changes in IntelliJ. Whenever I try to use a previous version of a library and change the dependency version in my pom.xml nothing happens. Maven continues to use the newer version of the library.
For example I want to use:
<dependency>
<groupId>org.springframework.security.oauth.boot</groupId>
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
But Maven repo has version 2.0.2 saved :
<dependency>
<groupId>org.springframework.security.oauth.boot</groupId>
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
So for my projects version 2.0.2.RELEASE is used.
I tried reimporting the project first. Then I tried "reimpor all maven projects". Then I checked Settings > Maven > Always update snapshots. I also tried opening the project settings and deleting the dependency from there, but on reimport the 2.0.2 version will be imported in the project. For now the only thing that works is deleting manually the folder from the ".m2" folder.
Shouldn't library versions be strictly followed and shouldn't version 2.0.1 v be used for my project?
The moment you change the version of the artifacts, maven will use the same version. It will never use neither new version nor the older version. Since you are using intellij, you can check which are the jar files along with their version used. See below the screenshot.
You can expand the External libraries as shown below and you can check the dependencies used in pom.xml.
Besides, you can also check in command prompt. Go to command prompt and point to the project directory and type the following command.
mvn install dependency:copy-dependencies
You can see all the required dependencies along with version information in target folder.
I suggest you not to delete the .m2 directory as you may have to download all the dependencies once again.
If you want to enforce the use of a particular dependency version you can use:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.security.oauth.boot</groupId>
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>
What this will do is exclude the dependency unless it actually gets used, and then if it does gets used it only uses the version you have specified.
Not clear what is the issue.
Repo can contain everything, no matter if dependency is present locally.
Also, Idea does not resolve dependency itself, we use maven api to resolve them.
By default, maven takes dependency which is nearest to root (see https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html)
Specifiying explicit dependency in root pom should force using this version.
Could you please provide mvn dependency:tree output and corresponding IDEA maven dependency diagram (if you have IU)?
If Idea resolve another dependency version than maven, please fill an issue at https://youtrack.jetbrains.com/issues

Does maven automatically download artifact dependencies?

I've been working with Maven for a little while now and I had a question about the information shown on the Maven Repository site. I was looking at the tags to paste into my pom for spring-web-mvc 3.2.8.RELEASE and noticed the table with the header "this artifact depends on" and saw the host of artifacts listed below.
My question is simple: Am I supposed to include the all of the dependencies listed in that table in my pom?
To answer your question, no you do not need to include all of the dependencies listed in the artifact dependencies section. It is my understanding that when you include a dependency in your pom file, maven will automatically download any needed jars. I am inferring this due to the fact that I personally don't add any of the artifact's dependencies other than what I need to my pom.
For example if I wanted spring-core I would do the following:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
And maven will automatically take care of the dependencies for me.
A good way to test this out is to open a new maven project in eclipse and specify a dependency such as this, update the project, and then check in the Maven dependencies folder.
For fun, I experimented with this and it is indeed true, Maven will download any necessary dependencies when you update your project. After putting only the above dependency in my pom.xml file I got the following:
No need to download all those.
Maven will take care of all the artifact's dependencies for the specified dependency mentioned in pom file.

Adding external JAR to Maven project in NetBeans

When I right click on my Maven project and choose the Add dependency option and I type my external jar's name in query, it is not found. How to add external jar to a Maven project?
From the NetBeans forum:
Open the Projects tab.
Right-click on Dependencies.
Select Add dependency.
Set groupId to: group.id (can be anything)
Set artifactId to: artifact.id (can be anything)
Set version to: 1.0 (can be anything)
Click Add to continue.
Dependency is added to pom.xml and appears under the Libraries node of Maven project. Continue:
Expand Dependencies.
Right-click on library (e.g., group.id).
Select Manually install artifact.
Set Artifact to install with the Java Archive (.jar) file path.
Click Install locally.
Library is installed locally with dependency attributes (coordinates) entered in steps 4 - 6.
I found those instructions helpful when going through the NetBeans GUI. Basically when right clicking to add a dependency, the group id, version, and name must be populated with anything. Then that "dependency" will be listed in the dependency drop down. Right click on that newly created dependency and right click to install locally and navigate to the appropriate jar file.
You can follow this tutorial:
http://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html
Example:
Install the jar to your local maven repository:
mvn install:install-file -Dfile=cxf-2.7.3.jar -DgroupId=org.apache.cxf -DartifactId=cxf-bundle -Dversion=2.7.3 -Dpackaging=jar
Edit the pom.xml file in your project to include the newly added dependency:
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-bundle</artifactId>
<version>2.7.3</version>
</dependency>
This should work regardless of the IDE you are using.
In Netbeans, the approach to adding dependencies that are not in repository is reversed. First come up with maven coordinates in the Add Dependency dialog. Then right click on the new dependency node and trigger "Manually install Artifact" action.
This answer is for jars that are in the maven repo
Let's say I want to add log4j-1.2.17.jar to my project, all I have to do is find it in maven repository
Step 2 is to copy that and place it inside the dependencies tag of your pom.xml` file:
<dependencies>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.4</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
....
....
<dependencies>
Step 3 Build and clean your project. The jar file will be in your dependencies folder afterwards
one trick is in the netbeans main menu select: profile->options->java->maven put in the global execution options the parameters example: -Dfile=C:\Users\anonym\Desktop\commons-pool-1.6.jar -DgroupId=commons-pool -DartifactId=commons-pool -Dversion=1.6 -Dpackaging=jar
where de parameter -Dfile is the location of the jar file
-Dfile=routeToJar
after that select your project. then rigth clic on the select project. and then select Run Maven->Goal. when the wizard appear type in as Goals install:install-file .. and then clic OK buttom

A conflicting jar is being added to Maven dependencies - how to track down?

I have a dependency that I have added to my project:
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-server</artifactId>
<version>7.0.5</version>
</dependency>
When I compile and run I get an error indicating a mismatch of signatures. Looking at my Maven Dependencies in my Java Build Path (Eclipse) I see a jar being added by Maven for Vaadin version 6.8.8. I have scoured my pom.xml and do not see that I have added that. I assume that this dependency is being added by another dependency.
I definitely want to use Vaadin version 7.0.5. As long as version 6.8.8 keeps getting included it will be an issue. How can I resolve this?
mvn dependency:tree
Once you have its output you can add a suitable exclusion.

Resources