How to update package version in external libraries - java

I want to update this package's version, but I didn't find this package in my pom file under root directory
How can I update this package's version? Do I need to do it directly in the pom file under the Maven package?
This is my dependency tree, and I want to upgrade to 1.31

If you don’t use it directly, then it is coming from one of your dependencies. You can check which one using
mvn dependency:tree
With IntelliJ IDEA, you can also open the Maven view, then right-click the project and select “Analyze Dependencies…” to get the same information.
Ideally, you should keep it as a transitive dependency, otherwise you will have to take care of its upgrade every time you upgrade the library that actually depends on it. Moreover, there can be issues if you upgrade only the transitive dependency and not the intermediate one (e.g. for Spring).
The best solution would thus be to upgrade that intermediate dependency, assuming that they have released a new version of it (SnakeYAML 1.29 being affected by CVE-2022-25857, there are good chances).
Only if you can’t do that, you should add the dependency in the <dependencyManagement> section of your pom.xml, but don’t forget tot maintain it from now on (and remove it once the intermediate dependency is upgraded).

If you can't find it in your pom then it means it's a transitive dependency pulled in by one of your other dependencies. You can just redefine this as a normal dependency in your pom and it will override the version to be whatever you like.

Related

Maven dependency tree / hierarchy not showing the truth

I was going to migrate an old Ant project existing of multiple single java projects to a multi-module maven project. All the libraries for have been stored in one local folder.
For building up the dependency management I wanted to go the way to add all dependencies to the parent pom.xml (dependency management section) and also do my best by adding the correct ones to the children (Java Maven projects) until compilation is working.
I then want to streamline the pom.xmls by using "mvn dependency:tree -X" to see if I have added some transitive dependencies to the single Maven projects which aren't not needed to be explicitly added to the pom.xmls
Now when comparing the dependency hierarchy for a Maven project shown in Eclipse with that shown by using "mvn dependency:tree -X", there are some differences:
Maven will not show that "jetty-http" is actually a transitive dependency of jetty-server - mostly because I already added it as direct dependency in the pom.xml before.
While Eclipse does show that relationship and this is the correct result (also checked it manually using Maven central dependency list).
So in the end when using Maven I would have left "jetty-http" as direct dependency in my pom.xml, although I don't have to. This is kind of useless.
Does anyone know why the Maven dependency tree is so limited? I want to understand what is going on here. Is there any alternative using Maven commands?
Or is there even a better way to check for/identify transitive dependencies added to the pom.xml by mistake?
Best practise is to add all dependencies to the POM that are used directly in your source code (and at runtime).
So if X is used in your source code, but is already present as transitive dependency of Y, you should nevertheless add X as direct dependency.
You can check this with mvn dependency:analyze
https://maven.apache.org/plugins/maven-dependency-plugin/analyze-mojo.html

How to ensure I am not including duplicate transitive dependencies through two maven dependencies?

I am looking for a way to test whether or not any of my explicit dependencies in my pom.xml reference/include any of the same transitive dependencies. For example, if dependency-A includes junit.jupiter and dependency-B also includes junit.jupiter, I want a way to see this so that I can exclude it from one of them to prevent conflicts.
I saw through this link that you can use mvn dependency:tree to essentially show all dependencies and their transitive dependencies, but it prints in a fairly unreadable format and it isn't clear through that output what the source of each transitive dependency is.
Note that if dependency-A uses junit.jupiter and dependency-B uses junit.jupiter, then only one of these dependencies will be included. Maven will not include the same dependency twice.
What can be tricky, though, is the resulting version. Maven takes the "nearest" element, not the highest version. If you want to notice if you have conflicting versions, I recommend the "dependency convergence" rule of the maven enforcer rules.
If you want to choose one version for junit.jupiter, add an entry to <dependencyManagement> in your POM.

Adding maven module with sources as another module

I have a java maven project that has another module as a dependency.
That dependency has "-sources.jar" in the repository (and I can download the sources with mvn dependency:sources)
I now need to do some little one-line changes in the module I am using as a dependency. The ideal way would be to somehow copy this dependency as another module, with all its current sub-dependencies. Then when I build the whole project, I want to build my newly copied dependency and use it instead of the current dependency. (I hope it's clear what I want to do.)
However, I don't know at all how to do that, and if it's even possible.
edit:
To be clearer.
I have my own project. I am also using let's say com.example.dependency from a repository. There are however small bugs in the com.example.dependency module, so I want to download the source to me and fix it locally and use it as a submodule.
edit2:
I will add that the dependency is not any public project on github, etc. It is a module in a private maven repository that only I have an access to. I cannot clone the source control project. I can only use maven, and its dependency tracking, and the "dependency:sources" thing.
what you may to to do is to checkout your dependency source as new project and do the changes. Then modify your project version and do the
mvn install
to install it in your local repository, then you this version in your project as one of the dependency, you will add this dependency in usual way you have done earlier, but change the version to new version you have installed.

How can I prevent code from using wrong version of some dependency?

I have a project which depends on a JAR file. The version of this JAR changes often and we are having a hard time trying to ensure we are using the correct one. It is also causing problems when investigating bugs: which version of JAR contains the bug. Some programmers may forget to update corresponding dependencies and include 2 versions of this JAR inside the project, so that an old version may be found by the classloader.
A question is how to account for this issue.
I have a following plan:
1) When an error occurs log the JAR name I am working with to ensure it is a correct version. I plan to use something like
this.getClass().getResource(someResourceINeedFromThatJar).getFile()
2) I can write a test to account for this. But I don't know how I can run a test AFTER the package phase of my Maven build
3) Maybe you can suggest something else for this?
Well, We have same scenario and solved issue by using maven dependencyManagement
It does two things.
Set a default version for dependencies in submodules/child projects
override the version of transitive dependencies
it does override a specified value in a transitive dependency.
The enforcer plugin does not ignore the dependencyManagement. But is unable to recognize the discrepancy since the transitive dependency's version was altered before it went to work.
Here is a nice article : You can go through it:
http://andydennie.com/2012/08/02/maven-enforcer-plugin-vs-dependencymanagement/
And another source: http://maven.apache.org/enforcer/maven-enforcer-plugin/

Maven release plugin and globally defined dependency versions

I'm not sure whether the title makes a whole lot of sense and whether this post already answers my question, but here it is:
We have a multi-module project. As you would expect this projects has a combination of internal and third party dependencies. For the third party dependencies we define these in the dependency management section of our parent POM so that we can manage the versions of these dependencies in a single common place.
As for inter-project (internal) dependencies, so far we've just entered the versions within each modules POM where a dependency is required. Then when doing a prepare with the release plugin, these versions are updated appropriately - all very nice.
What we want, like with the third party dependencies, is to be able to specify the internal dependency versions in the parent POM and therefore have a single common place. I see three potential approaches.
We do this by creating a property in the parent POM.
We do it via the dependency management section in the parent POM.
We use the project version property as the dependency version.
The preference would be to use one of the first two approaches, though there isn't really a strong reason for this. This leads me onto the main concern and question: If we use either of the first two approaches, will the release plugin still update the dependency version during the prepare stage?
All thoughts/feedback appreciated.
In the end we used the project.version property to help manage this. However, from what I understand, I believe using the dependency management section in the parent POM would also work.

Categories

Resources