Maven update dependencies in POM

JavaMaven 2PluginsCommand

Java Problem Overview


Are there any preexisting Maven plugins or commands to update the dependencies in the POM? Example: (if this was in my POM)

<dependency>
    <groupId>commons-lang</groupId>
    <artifactId>commons-lang</artifactId>
    <version>2.3</version>
</dependency> 

Is there a command or plugin I can run to get it to update the dependency to:

<dependency>
    <groupId>commons-lang</groupId>
    <artifactId>commons-lang</artifactId>
    <version>2.4</version>
</dependency> 

Java Solutions


Solution 1 - Java

Try the maven-versions-plugin, in particular, the versions:use-latest-versions goal.

Solution 2 - Java

I prefer using mvn versions:display-dependency-updates; this generates a report of which dependencies can be upgraded, but lets you modify the POMs yourself. There's also a display-plugin-updates command for plugins.

Solution 3 - Java

you can use dependencyManagement in your parent pom:

<dependencyManagement>
      <dependencies>
          <dependency>
              <groupId>commons-lang</groupId>
              <artifactId>commons-lang</artifactId>
              <version>2.4</version>
          </dependency>
      </dependencies>
</dependencyManagement>

this way, you need to change the version only once in the parent POM

Solution 4 - Java

Personally, I think there should be an additional parameter in maven that would allow you to add to the pom.xml.

See post at http://maven.40175.n5.nabble.com/Is-there-any-maven-plugin-to-add-dependency-to-existing-pom-xml-td2839092.html#a5772853

Here, you can add the following to your pom.xml file:

<project>
  ...
  <build>
    ...
    <plugins>
      ...
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>versions-maven-plugin</artifactId>
        <version>2.1</version>
      </plugin>
      ...
    </plugins>
    ...
  </build>
  ...
</project>

...

Then backup your pom.xml file via version set command:

mvn versions:set -DnewVersion=9.9.9

Run latest versions:

mvn versions:use-latest-versions

and diff the pom.xml files, pom.xml and pom.xml.versionsBackup

Solution 5 - Java

No there is isn't. And be happy there is not. How would such a tool know how to upgrade your dependencies?

With breakages possibly happening between minor versions, it would be a disaster waiting to happen.


But you can always write your own Mojo for that.

  • get latest version of dependency from Maven repository
  • compare with version from pom.xml
  • rewrite pom.xml
  • run mvn test
  • ?
  • Profit!

Solution 6 - Java

I had the same kind of problem and finally solved it by writing a bash script.

GitHub repository - Update POM Shell

This is a shell script that allows you to update a dependency on different modules directly from the command line.

It is particularly useful when you need to update one or more dependencies on different modules at once.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionjavamonkey79View Question on Stackoverflow
Solution 1 - JavaDominic MitchellView Answer on Stackoverflow
Solution 2 - JavapieperaView Answer on Stackoverflow
Solution 3 - JavarperezView Answer on Stackoverflow
Solution 4 - JavaScott IzuView Answer on Stackoverflow
Solution 5 - JavaRobert MunteanuView Answer on Stackoverflow
Solution 6 - JavaL_37View Answer on Stackoverflow