How to update maven repository in Eclipse? - java

Assuming you're already using the m2eclipse plugin, what can you do when it doesn't update the dependencies to the latest in your repo?
For example, on the command line, you can just add the -U flag as in
mvn clean install -U
…to force the dependencies to be updated. Is there something like this within Eclipse? (It doesn't always seem to pick up the latest updates.)

You can right-click on your project then Maven > Update Project..., then select Force Update of Snapshots/Releases checkbox then click OK.

In newer versions of Eclipse that use the M2E plugin it is:
Right-click on your project(s) --> Maven --> Update Project...
In the following dialog is a checkbox for forcing the update ("Force Update of Snapshots/Releases")

Right-click on your project and choose Maven > Update Snapshots. In addition to that you can set "update Maven projects on startup" in Window > Preferences > Maven
UPDATE: In latest versions of Eclipse: Maven > Update Project.
Make sure "Force Update of Snapshots/Releases" is
checked.

Sometimes the dependencies don't update even with Maven->Update Project->Force Update option checked using m2eclipse plugin.
In case it doesn't work for anyone else, this method worked for me:
mvn eclipse:eclipse
This will update your .classpath file with the new dependencies while preserving your .project settings and other eclipse config files.
If you want to clear your old settings for whatever reason, you can run:
mvn eclipse:clean
mvn eclipse:eclipse
mvn eclipse:clean will erase your old settings, then mvn eclipse:eclipse will create new .project, .classpath and other eclipse config files.

If Maven update snapshot doesn't work and if you have Spring Tooling, one interesting way is to remove
Right-click on your project then Maven > Disable Maven Nature
Right-click on your project then Spring Tools > Update Maven Dependencies
After "BUILD SUCCESS", Right-click on your project then Configure > Convert Maven Project
Note: Maven update snapshot sometimes stops working if you use anything else i.e. eclipse:eclipse or Spring Tooling

Related

M2E turn off resolve workspace dependencies

I have eclipse oxygen with M2E installed. I usually update my dependencies via Right Click -> Spring Tools -> Update Maven Dependencies. This uses Resolve Workspace Dependencies by default which I don't want.
How can I turn that option off by default?
For now I have to use command line with the command mvn eclipse:eclipse which one should not be using anymore
Note: My projects are not maven projects within eclipse
If you right click and go to Maven -> Update Maven Project you should be brought to a window. At this window there will be an option to select the code bases to update as well as not forcing update of snapshots or releases. This should help you get around this.
EDIT: The question has been edited to point out the projects are not maven projects in eclipse. This answer is less relevant

Eclipse maven build does not install dependencies

I'm working in eclipse on a java / javascript project, we're using maven to manage our project dependencies, I ran into a issue with maven build (clean install) does not install the dependencies that I specified in the POM.xml file, I tried everything I can find on the internet the whole day today, still no luck, I'll be really appreciate if anyone could kindly take a look at, thank you :)
I got a reuse lib project (lib) and a working project (project), the project should be installing the lib during the maven build, So, here's more detail information on what exactly I did:
I ran maven clean on the lib, then ran maven package, the lib-1.0.0-SNAPSHOT.jar file was generated successfully.
then in the working project POM.xml file, I added the dependency declaration.
I right clicked the working project, maven -> update maven project, where I checked the 'force update of snapshots / releases' checkbox, then -> ok
I right clicked the working project, run as -> maven build (with clean install, and also I checked on the 'Update Snapshots' checkbox) -> apply -> run
In the console, I saw the reuse lib was downloading and then downloaded, but it's never got installed (there should be a line says installing reuse lib...), as the result of it, the reuse lib will not be loaded after I ran my working project, it drives me insane -_-!..
Only your maven projects build output can be installed (in the local repository with mvn install, resp. mvn clean install). During the build it will resolve the dependencies (and the transitive dependencies) to be downloaded and packed to your delivery. Those dependencies of your project will implicitly also be "installed" in your local repository since you will see them in your local repository after the download happened - Maven will however not see that as a install in the meaning of install of the default lifecycle.
To install your "reuse lib"-Maven project you will have to run mvn install or mvn clean install on that project's pom rather than on a project which "just uses it as dependency".

What do I do with the pom.xml file?

I have just downloaded a project from github, it has a pom.xml and is missing many dependencies.
I installed m2e Maven integration in Eclipse plugin but now what do I do? It's installed and seems to be working but how do I download the dependencies now?
You checkout the git files to a location different from your Eclipse workspace.
Start Eclipse on a clean workspace and install m2e. (I usually install m2e-wtp to get web projects)
Use File -> Import -> Existing Maven projects.
Point to the pom.xml you want to use in the files checked out above.
Wait while Eclipse downloads half the Internet.
Ready.
Right click on your project, run as -> maven install.
If you can't see these, you either incorrectly installed m2e, or you need to import the project as a maven project.
In that case, delete the project from your workspace (do not check the "delete contents on disk"). Right click in the project explorer and click on import... From that dialog, you can select existing maven projects, where you need to navigate to the highest directory containing a pom.xml file.
Also, if you have maven installed on your computer (not m2e in eclipse), you can open the directory that contains the pom.xml and just type mvn clean install. This will build your project.
If you get a "Build success" message, you'll know that the dependencies have been fetched correctly.
In eclipse, You have to convert your project to a maven project. right click on your project -> maven (or configure) -> convert to maven project (or enable dependencies management).
This should update your dependencies automatically, if it is not the case then right click your project -> maven -> update project (or update dependencies, it depends on the verion of m2e you are using).
It should be enough to build the project. The dependencies should be downloaded during the build.
Pom.xml files are used for Maven projects.
Depending on your Eclipse version, maybe it's not compatible with Maven out of the box. If not, you can install a Maven plugin (like m2e) so that the project can be viewed as a Maven project.
Maven will do the dependency management (if pom.xml file is configured properly)
When you right click on your project->Run As-> Maven install it will download all the dependencies that it does not have already in your local repository.

IntelliJ IDEA not recognizing classes specified in Maven dependencies

I have a project in IntelliJ IDEA which was created with Maven. I then specified a set of dependencies and external repositories in the Pom.xml file.
The project builds fine on command line if I do mvn install. When I open any of the code files in the IDE though it says all the classes handled by Maven dependencies aren't recognized - as it would for a normal project if I never added the required JARs to the build path.
I know in my Eclipse Maven projects (rather than IntelliJ) it usually shows an extra directory on the left which says "Maven Dependencies" and lists the JARs pulled in via maven. I don't see that here. What am I doing wrong?
Here's what my screen looks like:
Right click on the pom.xml -> Add as Maven project -> Reimport
For some reason re-import did not do the trick. After looking at this:
http://www.jetbrains.com/idea/webhelp/maven-importing.html
I set auto-import and then the problem went away though; hopefully it helps someone else. Thanks for the ideas everyone :).
After installing IntelliJ IDEA on a new computer I found myself with the same issue.
I had to update the remote maven repository. (Settings > Maven > Repositories)
Both local and remote repos needed to be updated. The remote one wasn't updated ever before this. After a restart everything worked fine. You might have to reimport your project.
You could go to:
File > Settings > Build, Execution, Deployment > Build Tools > Maven
and check if your pom.xml is not in the "Ignored Files" list.
I was running into similar issues. For me it ended up being that I was importing the project incorrectly. I had been doing
import project
<navigate to existing project and click top level directory>
follow the wizard
What solved my maven problems was to do
import project
<navigate to existing project and click the pom.xml
follow the wizard
For me File>>Invalidate Cache/Restart>>Invalidate and Restart worked in IntelliJ
Idea cannot download all dependent jar packages using maven,try the following operations:
mvn -U idea:idea
then all the dependent jar packages are download from the maven repository
A simple reimport and/or update of the repositories via Intellij did not do the trick for me.
Instead I had to delete the complete ~/.m2/repository directory and let maven sort everything out by itself. Afterwards Maven -> Reimport finished it off.
I've encountered a similar issue after refactoring my maven project into different modules. Re-importing on it's own usually doesn't work, but I've found that deleting the .iml files and then re-importing usually does the trick.
Ran into the "same" issue some days ago. It might not be related as my issue was more specific to Spring boot but as I was struggling with it and tried every solution on this post I'm going to share my experience.
What I was trying to do is to add one of my spring boot app into another project as a maven dependency. The dependency was resolved but I couldn't access my classes.
When no packaging is declared, Maven assumes the default packaging is JAR.
The JAR generated by the Spring Boot Maven Plugin overrides the default one generated by Maven.
The solution was:
The solution that we found is to generate another JAR which will be used as a dependency to be imported from other projects.
The full article which helped me solve my issue.
Hope it helps someone.
For reasons I don't understand, in my case, I needed turn on setting "Always update snapshots" in Build, Executions, Deployment > Build Tools > Maven.
That made IntelliJ redownload dependencies and make it work.
In my case the problem was that the project was in maven2 while intellj was configured for maven3. Switching to maven2 in settings solved the problem
Might be useful to others that were still stuck like me.
None of the suggested fix worked. Actually, not before I fixed my main problem which was the installation location of maven.
In my case, I did not use the standard location. Changing that location in the maven settings (Settings/Maven/Maven home repository) did the trick.
My 2 cents.
Cache is causing problems! Make sure to do the following:
In your terminal, go to project/module:
mvn clean install
In your IntelliJ:
File > Invalidate Caches > Invalidate
Right click on project/module > Maven > Reimport
For my case I should have checked the work offline
Go to File>Settings >Build, Execution, Deployment >Build tools>Maven
Then check Work Offline
Worked for me:
mvn -U idea:idea
Since mvn -U updates the dependencies, check what mvn -U does: https://stackoverflow.com/a/26224957/6150881
Before this I have tried following steps but these have not helped:-
Deleted .idea and .iml file
Invalidate cache and restart
Maven -> Reimport .
This happened to me when I had mistakenly set my IntelliJ to power saving mode. Power Saving mode is displayed by battery icon with half empty charge. Disabling that fixed the problem.
This also happened to me after upgrading Intellij to 1.4.15. I tried to re-import the whole project with same result, but enabling Maven Auto Import did the tricks.
Looks like there are several, valid reasons why intelliJ would ignore a pom file.
None of the previous answers worked in my case, so here's what did work, in case someone else runs into this issue:
In this example, module3 was being completely ignored by IntelliJ. The pom.xml in that directory wasn't even being treated as a maven pom.
My project structure is like this:
myProject
module1
module2
module3
BUT, my (simplified) pom structure is like this:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>devs</groupId>
<artifactId>myProject</artifactId>
<version>0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>myProject</name>
<modules>
<module>module1</module>
<module>module2</module>
<modules>
<profiles>
<profile>
<id>CompleteBuildProfile</id>
<modules>
<module>module3</module>
</modules>
</profile>
</profiles>
</project>
To fix this, I modified the root <modules> element to add in module3 temporarily.
<modules>
<module>module1</module>
<module>module2</module>
<module>module3</module>
<modules>
Next re-import the root pom, and IntelliJ will add the module.
When that's done, revert the pom. IntelliJ will ask if you also want to remove module3 from the project structure. Click No.
Bam! Done. Module3 works and I can run my Integration tests from IntelliJ again :D
The problem was caused for me by selecting the project directory to be Imported when first starting IntelliJ rather than the pom.xml file for the project.
Closing the problem project and then following the Import process again but choosing the pom.xml resulted in a fully working project in the IDE.
For me the problem seems to be a conflict with the maven helper plugin (https://plugins.jetbrains.com/plugin/7179?pr=idea).
I disable it, and it works again :)
Go to
File > Settings > Build, Execution, Deployment > Build Tools > Maven
and check the Maven home directory. This should be the same maven installation used for command line
For me, what did the trick was to add the dependencies in File > Project Settings > Modules > Dependencies.
Just delete your project's .idea folder and re-import your project in IntelliJ.
If you have any dependencies in pom.xml specific to your organisation than you need to update path of setting.xml for your project which is by default set to your user directory in Ubuntu : /home/user/.m2/settings.xml -> (change it to your apache-maven conf path)
Go to -> intellij settings -> build,Execution, Deployement -> Build Tools -> Maven -> User settings file
Restart, Invalid caches, outside building, none worked for me.
Only Reimport worked finally. For others sake, putting it as answer:
Right click the project > Maven > Reimport
While importing a New project :
1.To identify all the modules in a project as maven modules:
File --->New Project Settings -->Build Execution deployment -->build tools --> maven ---> importing ---> enable "search for projects recursively"
Option1: Right-click on the main project folder => Add Framework Support => Check Maven option
Option2: right-click on the pom.xml file and click on "Add as a maven project"
This happened when I was upgrading from Java from 8 to 11 and Spring version. All the dependencies in the maven section disappeared as if no pom file existed. Was able to find the issue by doing
mvn clean
It showed me that one of the dependencies was missing version tag and it needed one.
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
</dependency>
After adding version to the above dependency it started showing up all the dependencies under maven section.
In my case the my maven home path was pointing to Bundled Maven 3 instead of where my .m2 folder was located, fixed it by going to File > Settings > Build, Execution and Deployment > Maven > Maven home path and adding C:/Program Files/apache-maven-3.5.4

How do I manually add a project to the build path of another project?

I have a maven project which won't compile due to an unresolvable reference from another project's artifact.
When I run eclipse:eclipse and open it up it shows an auto fix suggestion of "Add project 'project2' to build path of 'project1'". If I click this everything works. So project1 can clearly see my project2 reference, but doesn't quite use it as expected.
However, once I delete all the files generated for eclipse the error resumes because whatever reference eclipse created has been removed. How can I get this project2 in the project1 build path manually. I already have it listed in the pom as below:
<dependency>
<groupId>group.id</groupId>
<artifactId>project2</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
In "Project properties > Maven" menu you should check "Resolve dependencies from Workspace projects".
When I run eclipse:eclipse[...]
This sounds like you are using the Maven Eclipse plugin. You should probably rather use m2e, which is the recommended way to integrate with Maven now. This will also obviate the need to run mvn eclipse:eclipse. Instead, just import your Maven project into Eclipse, and it will pick up everything automatically.
After you set "Resolve dependencies from Workspace projects" as described in polypiel's answer, things should just work (TM).
If you have maven nature to both your projects, you should do mvn clean install to both the projects, so they will be installed in your local repository and will be available for other projects to use as dependencies. Or just right click to both the projects in eclipse and choose Run As --> Maven install

Categories

Resources