77

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> 
Betlista
  • 10,327
  • 13
  • 69
  • 110
javamonkey79
  • 17,443
  • 36
  • 114
  • 172
  • 2
    I'd be perfectly happy if a plugin would simply check for updates and inform me about them. The same checking for new versions of plugins would also be neat. – Huxi Jun 10 '09 at 00:42
  • 1
    I've been thinking that might be one of the goals of the Mojo that I will build. – javamonkey79 Jun 10 '09 at 04:39

6 Answers6

76

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

Jens Piegsa
  • 7,399
  • 5
  • 58
  • 106
Dominic Mitchell
  • 11,861
  • 4
  • 29
  • 30
  • 8
    This does what the poster requested, although I think use-latest-releases is better in most cases. But this is definitely the plugin to use. – Zac Thompson Jun 10 '09 at 19:13
  • 7
    Dang, now that I already built it! I guess as the saying goes, "don't reinvent wheels unless you want to learn alot about wheels". :) Thanks! – javamonkey79 Jun 11 '09 at 00:54
  • Yup — I missed use-latest-releases. Thanks, Zac. – Dominic Mitchell Jun 11 '09 at 10:42
  • 2
    If you need to get the latest version only of your company's libraries you can achieve this with the following Maven property: -Dincludes=com.mycompany:* – frandevel Sep 27 '11 at 11:23
34

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.

piepera
  • 2,033
  • 1
  • 20
  • 21
  • 1
    Works for me! For some reason the link always takes me to the mojo homepage, I'll paste the link here: http://www.mojohaus.org/versions-maven-plugin/display-dependency-updates-mojo.html – Liuting Jul 03 '15 at 15:57
  • Thanks, I've fixed the link with Liuting's new link. – piepera Jul 14 '15 at 18:40
6

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

rperez
  • 8,430
  • 11
  • 36
  • 44
  • 1
    this is much preferred to having some tool update stuff in ways that you may or may not control. You also guarantee that all children will be using the version specified. – Mike Pone Jun 10 '09 at 14:56
  • 2
    @MikePone depends on the use case - there are valid ones, just like having a deployment pom that needs to always use latest releases of your own artifacts, which is a common need. In general, of course, you're correct. – eis Aug 25 '16 at 13:45
3

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

Scott Izu
  • 2,229
  • 25
  • 12
0

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.

L_37
  • 1
  • 1
0

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!
Robert Munteanu
  • 67,031
  • 36
  • 206
  • 278