ChatGPT解决这个技术问题 Extra ChatGPT

Maven: Command to update repository after adding dependency to POM

I've added a new dependency to my POM.

Is there a simple command I can run to download this dependency to my repository?

For those looking on how to do this in a Spring Boot project: use mvnw to call the wrapper layer. The commands on this page work with it.

A
Aaron Digulla

If you want to only download dependencies without doing anything else, then it's:

mvn dependency:resolve

Or to download a single dependency:

mvn dependency:get -Dartifact=groupId:artifactId:version

If you need to download from a specific repository, you can specify that with -DrepoUrl=...


I get this error when I run that command: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-dependency-plugin:2.1:get (default-cli) on project standalone-pom: The parameters 'repositoryUrl' for goal org.apache.maven.plugins:maven-dependency-plugin:2.1:get are missing or invalid -> [Help 1]. Specifying -DrepositoryUrl=... doesn't work.
I think I've found the solution. The parameter should be "repoUrl" and not "repositoryUrl".
Doesn't solve the issue for me: the I run mvn package -o right after — I get error that plugins can not be downloaded. Running mvn dependency:resolve-plugins doesn't fully solve the issue either.
Your repository URLs might be missing/wrong. In any case, I don't think this answer is the answer to the problem you're having. This answer covers the case when Maven can find all the artefacts it needs in the configured repositories.
I don't care if you are a believer or not, a christian or not... God bless you this awesome simple answer.
A
Aaron Digulla

mvn install (or mvn package) will always work.

You can use mvn compile to download compile time dependencies or mvn test for compile time and test dependencies but I prefer something that always works.


Thanks, I also discovered that adding it to the pom in STS will automatically download it for you.
@Andrew Spencer's reply is more accurate - mvn dependency:xxx deal with dependencies only and don't do any additional stuff - and that what the question was about.
At times, 'mvn package' may not update dependencies. Has happened to me more than once.One needs to run 'mvn dependency:resolve' in such cases
@BinitaBharati, you can add a -U to the Maven command line to force dependency downloads. This is useful if Maven doesn't download an updated dependency because of a cache timeout.
@Kishan Ask a new question. Show the layout of your project (especially where the import happens) and whether you use a multi-module build.
C
Community

I know it is an old question now, but for users who are using Maven plugin with Eclipse under Windows, you have two options:

If you got Maven installed as a standalone application: You can use the following command in the CMD under your project path: mvn eclipse:eclipse It will update your repository with all the missing jars, according to your dependencies in your pom.xml file. If you haven't got Maven installed as a standalone application you can follow these steps on your eclipse: Right click on the project ->Run As -- >Run configurations. Then select mavenBuild. Then click new button to create a configuration of the selected type .Click on Browse workspace then select your project and in goals specify eclipse:eclipse

You can refer to how to run the command mvn eclipse:eclipse for further details.


Running eclipse:eclipse after dependency:resolve helped me see downloaded jars in eclipse, thanks!
While this answer will help the poor folk stuck with Eclipse, I strongly recommend that anyone using Eclipse find a better alternative. Especially if you're going to be using Maven. Netbeans and IntelliJ are light years ahead.
@64BitBob Assuming that Netbeans and IntelliJ are better than eclipse, we should always give a solution for those who use it. :)
I see that the plugin is not available anymore in marketplace but yes it works in Eclipse 2020 without downloading anything. I wonder if mvn eclipse:eclipse is the command sent by eclipse itself when we rightclick->Maven->Update Project...
@Paolo In my opinion they have automatically added the plugin in new versions of Eclipse, and yes I think it's the same command behind the "Update project" option.
N
NullPointer

Pay attention to your dependency scope I was having the issue where when I invoke clean compile via Intellij, the pom would get downloaded, but the jar would not. There was a xxx.jar.lastUpdated file created. Then realized that the dependency scope was test, but I was triggering the compile. I deleted the repos, and triggered the mvn test, and issue was resolved.


A
Ashish Pandey

Right, click on the project. Go to Maven -> Update Project.

The dependencies will automatically be installed.


This is how to do it in Eclipse.
sometimes, and it's eventual, not away this works, some commands like mvn package, install, dependency:resolve can solve your issue