Maven: How to stop using old version of dependency? - java

In my Java Maven Project I can see the following 2 versions of the one dependency (apache httpcomponents):
How can I ensure that only the newest version (4.5.2) is used in my project?
I cannot even find 4.2.1 declared anywhere within my pom.xml files to remove it.

There should never be two versions of the same dependency for a given project.
So either your IDE shows something wrong here or you did not mvn clean before the build so that old and new dependencies mixed.
Maven will always take only one version. Unfortunately, you cannot tell Maven to take the latest one, but it will always take the nearest one in the dependency tree.
If you want to set the version for a specific dependency, use <dependencyManagement>.

Based on your screenshot you're using IntelliJ. And I have to agree that this view is kind of confusing. In the Project-tab you'll External Libraries which contain all dependencies of all projects in this tab. For that reason dependencies might appear with different versions.
In the Maven-tab (right hand side) you can see the dependency-tree per project. First you'll see the direct dependencies, which you can expand to see the next level of dependencies. AFAIK there's no complete dependency list available in IntelliJ, as one would see with Eclipse.

That's the view of IDE, it shows all the using or used library in your project. Once you point your pom.xml to the new version and rebuild your project, the new version will be used.
In Intellj, you can select: Menu -> File -> Invalidate Caches/Restart to update it. If that won't help, start new project from your existing pom.xml file

Related

IntelliJ still using old dependency

I am currently using Intellij Idea 2020.2.4.
I want to replace a dependency with a newer version of itself.
I replaced
<dependency>
<groupId>com.company.digital.fmk</groupId>
<artifactId>dgt-security</artifactId>
<version>2.6.24</version>
</dependency>
with
<dependency>
<groupId>com.company.digital.fmk</groupId>
<artifactId>dgt-security</artifactId>
<version>3.9.0</version>
</dependency>
I've checked that in the effective pom, the version has changed to the new value. However, after running
mvn -U clean install, when I click on a class from the dependency, it still shows the old version.
When I try to delete the old version from the project structure, it says that it is still used in a module, even though I can't find the old version number anywhere in the project.
I can't manually delete the old library from my local folder, because it is used in another project.
Other answers have suggested using Maven -> Reimport, but I didn't find this in my intellij
What can I do to make intellij use the new dependency version in my project ?
EDIT
When I right-click on a project and select Maven, here are my options:
Reload project (already tried, did not work)
Generate sources and update folders
Ignore projects
Unlink maven projects
Open 'settings.xml'
Create 'profiles.xml'
Download sources
Download documentation
Download sources and documentation
Show effective pom
Show diagram
Show diagram popup
You can go to your Maven repository:
.m2/com/company/digital/fmk
Delete jar files
Rerun command, it will download dependencies again.
Right click on project -> maven -> sync shall do the trick
previous versions did this by default on changes in pom.xml
now it seems to be not available anymore

Intellij Modules are forgotten on rebuild

I have added all the jar modules I need for my project in a folder called 'libs'. I right click 'Add as library' and the module is implemented and will work for the duration of that build.
My problem is that whenever I rebuild the project in anyway it 'forgets' about these modules and I have to re add them over and over.
What am I doing wrong?
EDIT: I forgot to mention that I am using gradle
Any IDEA version from about 12 up (current version 15 is superb) should handle maven projects correctly.
Make yourself a pom.xml and put at the root of your project.
The <dependencies> section defines your libraries (they must have their own pom.xml).
IntelliJ will automatically sync the libraries in your project whenever you make changes to the pom.xml. Thus you avoid having to add or remove modules manually any more.
You can either run your java code as normally, or run a maven goal such as install from the maven pane on the top right.
Hope this helps! Once you start using maven (or an equivalent tool like gradle) your life with multiple dependencies will get much easier.

Maven m2eclipse detect changes in dependecy in workspace and automatically install

I found an annoying issue working with m2eclipse in Eclipse.
My workspace contains 2 projects, an application A and a library B. The application A POM has B as a dependency and everything works correctly. (The project dependecy is found and used when I build the application)
But if I change some code in project B and I forgot to mvn:install it, when I build the application it uses the last built version of the library and it loses my last changes.
Is there a way to force Maven / M2Eclipse to check if the source code of the dependecy is newer than the last version built, and to install it when installing/ deploying the main application?
Or maybe my approach is wrong or is something obvious that I'm missing?
You can tell m2eclipse to use resolve dependencies from the workspace rather than through the normal mechanisms. In your project properties (NOT workspace properties), select Maven->Resolve dependencies from Workspace projects.
This will mean that when you change B and subsequently build A, the changes should be picked up automatically.
If, however, you build outside Eclipse, you'll have to do the normal mvn install to get the correct dependencies.
I don't know of a way to tell maven to build the library first, then build your project. You could put two maven commands into a script and run the script.
Alternatively, you could put both projects inside a maven parent project, and then build the parent. This causes all child project to be built too (so in your case the library, and the application).
I think you have couple of options here
If you can change the maven project set up, I would suggest you to use maven multimodule
Option two might solve your problem but still involves a manual stop when you change your dependent project B, Do this on for your dependent project in eclipse
Select Library B ==> Properties ==> Maven ==> in the input box under Goals to invoke after project clean: ==> enter : install
To deploy any change that you made to Project B in eclipse to maven local/remote repo, You have to run Clean build in eclipse, This will deploy the latest Library B to the maven repository
Also make sure your Library B version is a SnapShot during the development
If you are looking to run the latest snapshots on your local machine you should try to set things up to launch directly from Eclipse.
Apart from avoiding the problem you originally posted about, it also has other advantages.
It will save you a lot of time by not having to perform intermediate build steps.
You can start and stop servers without having to leave your IDE.
The Eclipse console has extra features that you don't get from the system console.
You don't have to modify your start scripts to attach a debugger.

Adding custom maven project as dependency

I'm using eclipse with the m2eclipse plugin now I just want to resolve a - imho - easy problem: I've got two maven projects, I want to add project A as dependency to project B.
Well how do I achieve this in a manner way? If I add the project A to the build path of project B eclipse recognizes the classes but this project isn't resolved by eclipse on build time.
I got it working by installing project A to my local repo and adding this as dependency to my pom. This works but is cumbersome because I always have to install a new version of project A when something changed.
Shouldn't the plugin handle such a situation for me?
Providing that you have both maven projects open in your workbench then make sure you have the "Enable Workspace Resolution" option enabled in the Maven context menu.
Yes Eclipse handle this situations.
You can add both the Projects A and B in the same work-space.
I could help you creating a simple work-space from the scratch.
I am assuming that you have already installed the MAVEN plugin M2Eclipse for Eclipse.
Start a new Eclipse in a blank Work-space
Right click on Project Explorer --> Go to Import dialog and add a Maven Module.
Locate the POM directory and add that directory.
It will list all the projects in all the sub folders.
Add as many as Maven Based modules in a single work-space.
By doing this you dont need to install the dependencies. Any change will be reflected on the derived module.
Hope that will help you.
The way you did it is correct, because it assumes that project B will be using the dependency of project A that will be in the artifactory, so you can develop both independently.
And anyway, for the project A, if you are using maven, don't you use maven clean install for compiling and deploying? That way you are sure you always have the latest version
The other option is, in case both of the are more dependent of each other, you should consider make one of them as a module of the other, or maybe make a project C that contains both modules, but that would mean both of them are part of the same project (like an ear containing two jars), depends on the situation

In IDEA, after adding a dependency in maven, my artifact always show's the library needs to be 'fixed'

So every time I add a dependency to a library using maven in my pom.xml, it doesn't get deployed until I go to module settings and then click on 'fix'.
This happened when I added freemarker.
Why doesn't IDEA do this for me? Am I missing a step?
Not sure about previous versions but with IntelliJ 9, IntelliJ shows me a Import Changes link when I modify a pom.xml and also offers to Enable Auto-Import:
alt text http://www.imagebanana.com/img/emcut3ku/screenshot_008.png
The auto-import option is also available from the Maven Integration dialog: File > Settings > Maven > Importing > Import Maven projects automatically.
IDEA stores all of your module's dependencies in either a .iml or .classpath file (based on your preference).
When you change the pom.xml file, it needs to resolve the new set of dependencies (Re-import) and update the file accordingly. I can only imagine that they don't do this automatically for you because they can never really know when you are done working with the file, and causing resolution of dependencies to start while you're still typing would make working with pom.xml from within idea very irritating.
AFAIK, you're doing it right. In my version of IDEA, I see a "re-import" button that appears in the pom file editor (which saves a couple of clicks)... or you can go to the "Maven Projects" window and click the "Re-Import Maven Projects" button, but these should do the same thing as your "Fix" button in the project settings.
The original question referred to a library that wasn't deployed -
IntelliJ uses artifacts to deploy build output and required libraries to application/web servers, and currently those can include a constant list of dependencies. Clicking the "fix" simply adds module dependencies to the artifact, if they weren't already there.
Since in most use-cases this is 1:1, i.e. artifacts contain all module dependencies, I have opened an feature issue for it:
http://youtrack.jetbrains.com/issue/IDEA-99458

Categories

Resources