CLOVER🍀

That was when it all began.

Mavenでの依存ライブラリの最新バージョンを表示、更新したりできるVersions Maven Plugin

JJUG CCC 2016 Springの「Spring Boot で Boot した後に作る Web アプリケーション基盤」というセッションで、Versions Maven Pluginというものがあることを知りました。

Spring Boot で Boot した後に作る Web アプリケーション基盤/spring-boot-application-infrastructure - Speaker Deck

該当のページ。
https://speakerdeck.com/sinsengumi/spring-boot-application-infrastructure?slide=75
https://speakerdeck.com/sinsengumi/spring-boot-application-infrastructure?slide=76

便利そうだな〜と思ったので、メモを兼ねてちょっと試してみることにします。

Versions Maven Plugin – Introduction

プラグインのゴールはこちら。

Versions Maven Plugin – Plugin Documentation

サンプルpom

Mavenプロジェクト(というかpom.xml)がないと話が始まらないので、適当に依存関係やプラグインを適用したpom.xmlを用意してみます。

    <dependencies>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.3.1</version>
        </dependency>
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>3.1.0</version>
        </dependency>
    </dependencies>

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

全体的に、最新版(2016/5/28時点)より少し古いバージョンを指定しています。

しれっと「versions-maven-plugin」も入れていますが、別に入れていなくてもかまいません。
※Versions Maven Pluginも、少し古いバージョンを指定しています

依存ライブラリの最新バージョンを表示する

依存ライブラリの最新バージョンを表示するには、「versions:display-dependency-updates」を指定します。

$ mvn versions:display-dependency-updates
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building versions-plugin-example 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- versions-maven-plugin:2.1:display-dependency-updates (default-cli) @ versions-plugin-example ---
[INFO] The following dependencies in Dependencies have newer versions:
[INFO]   com.squareup.okhttp3:okhttp ........................... 3.1.0 -> 3.3.0
[INFO]   org.apache.commons:commons-lang3 ........................ 3.3.1 -> 3.4
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.089 s
[INFO] Finished at: 2016-05-28T22:46:18+09:00
[INFO] Final Memory: 10M/201M
[INFO] ------------------------------------------------------------------------

ちなみに、指定されているバージョンがすでに最新版の場合は、結果に表示されなくなります。
※OkHttpのバージョンを上げました

[INFO] The following dependencies in Dependencies have newer versions:
[INFO]   org.apache.commons:commons-lang3 ........................ 3.3.1 -> 3.4

プラグインの最新バージョンを表示する

Mavenプラグインの最新バージョンを表示することもできるようです。「versions:display-plugin-updates」を指定します。

$ mvn versions:display-plugin-updates
[WARNING] The following plugins do not have their version specified:
[WARNING]   maven-clean-plugin ........................ (from super-pom) 3.0.0
[WARNING]   maven-deploy-plugin ....................... (from super-pom) 2.8.2
[WARNING]   maven-install-plugin ...................... (from super-pom) 2.5.2
[WARNING]   maven-jar-plugin .......................... (from super-pom) 3.0.0
[WARNING]   maven-resources-plugin .................... (from super-pom) 3.0.0
[WARNING]   maven-site-plugin ......................... (from super-pom) 3.5.1
[WARNING]   maven-surefire-plugin .................... (from super-pom) 2.19.1
[INFO] 
[WARNING] Project does not define minimum Maven version, default is: 2.0
[INFO] Plugins require minimum Maven version of: 3.0
[INFO] Note: the super-pom from Maven 3.3.9 defines some of the plugin
[INFO]       versions and may be influencing the plugins required minimum Maven
[INFO]       version.
[INFO] 
[ERROR] Project does not define required minimum version of Maven.
[ERROR] Update the pom.xml to contain
[ERROR]     <prerequisites>
[ERROR]       <maven>3.0</maven>
[ERROR]     </prerequisites>
[INFO] 
[INFO] Require Maven 2.0.2 to use the following plugin updates:
[INFO]   maven-site-plugin ........................................ 2.0-beta-7
[INFO] 
[INFO] Require Maven 2.0.6 to use the following plugin updates:
[INFO]   maven-clean-plugin .............................................. 2.5
[INFO]   maven-deploy-plugin ........................................... 2.8.1
[INFO]   maven-install-plugin .......................................... 2.5.1
[INFO]   maven-jar-plugin ................................................ 2.4
[INFO]   maven-resources-plugin .......................................... 2.6
[INFO]   maven-site-plugin ............................................. 2.0.1
[INFO]   maven-surefire-plugin ......................................... 2.4.3
[INFO] 
[INFO] Require Maven 2.0.9 to use the following plugin updates:
[INFO]   maven-compiler-plugin ........................................... 3.1
[INFO]   maven-surefire-plugin .......................................... 2.17
[INFO] 
[INFO] Require Maven 2.1.0 to use the following plugin updates:
[INFO]   maven-site-plugin ............................................. 2.1.1
[INFO] 
[INFO] Require Maven 2.2.0 to use the following plugin updates:
[INFO]   maven-site-plugin ............................................... 3.0
[INFO] 
[INFO] Require Maven 2.2.1 to use the following plugin updates:
[INFO]   maven-clean-plugin ............................................ 2.6.1
[INFO]   maven-compiler-plugin ........................................... 3.3
[INFO]   maven-deploy-plugin ........................................... 2.8.2
[INFO]   maven-install-plugin .......................................... 2.5.2
[INFO]   maven-jar-plugin ................................................ 2.6
[INFO]   maven-resources-plugin .......................................... 2.7
[INFO]   maven-site-plugin ............................................... 3.4
[INFO]   maven-surefire-plugin ........................................ 2.19.1
[INFO]   org.codehaus.mojo:versions-maven-plugin ......................... 2.2
[INFO] 
[INFO] Require Maven 3.0 to use the following plugin updates:
[INFO]   maven-clean-plugin ............................................ 3.0.0
[INFO]   maven-compiler-plugin ......................................... 3.5.1
[INFO]   maven-jar-plugin .............................................. 3.0.0
[INFO]   maven-resources-plugin ........................................ 3.0.0
[INFO]   maven-site-plugin ............................................. 3.5.1

プラグインの場合は、けっこういろいろ言われます…(なんかエラー出てるし)。

なお、「versions:display-plugin-updates」の場合は、単に使われるプラグインの最新バージョンが表示されるだけで、現在指定してあるのが最新バージョンかどうかは関係がなさそうです。

依存ライブラリをアップデートする

やりたいかどうかはさておき、pom.xmlに定義されている依存ライブラリを最新版にアップデートすることもできます。「versions:use-latest-releases」、「versions:use-latest-versions」を使用します。

「versions:use-latest-releases」の場合。

$ mvn versions:use-latest-releases
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building versions-plugin-example 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- versions-maven-plugin:2.1:use-latest-releases (default-cli) @ versions-plugin-example ---
[INFO] Major version changes allowed
Props: {project.groupId=com.example, project.artifactId=versions-plugin-example, project.version=0.0.1-SNAPSHOT}
[INFO] Updated org.apache.commons:commons-lang3:jar:3.3.1 to version 3.4
Props: {project.groupId=com.example, project.artifactId=versions-plugin-example, project.version=0.0.1-SNAPSHOT}
[INFO] Updated com.squareup.okhttp3:okhttp:jar:3.1.0 to version 3.3.0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.962 s
[INFO] Finished at: 2016-05-28T22:54:30+09:00
[INFO] Final Memory: 10M/201M
[INFO] ------------------------------------------------------------------------

「versions:use-latest-versions」の場合。

$ mvn versions:use-latest-versions
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building versions-plugin-example 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- versions-maven-plugin:2.1:use-latest-versions (default-cli) @ versions-plugin-example ---
[INFO] Major version changes allowed
Props: {project.groupId=com.example, project.artifactId=versions-plugin-example, project.version=0.0.1-SNAPSHOT}
[INFO] Updated org.apache.commons:commons-lang3:jar:3.3.1 to version 3.4
Props: {project.groupId=com.example, project.artifactId=versions-plugin-example, project.version=0.0.1-SNAPSHOT}
[INFO] Updated com.squareup.okhttp3:okhttp:jar:3.1.0 to version 3.3.0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.878 s
[INFO] Finished at: 2016-05-28T22:55:36+09:00
[INFO] Final Memory: 10M/201M
[INFO] ------------------------------------------------------------------------

これで、なんとpom.xmlの内容が書き換わります。

    <dependencies>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>3.3.0</version>
        </dependency>
    </dependencies>

「versions:use-latest-releases」と「versions:use-latest-versions」の違いは、Release版を選ぶかどうかのようですね。

ちなみに、この時pom.xmlのバックアップができていて

$ ll
合計 28
-rw-rw-r--   1 xxxxx xxxxx 1331  528 22:55 pom.xml
-rw-rw-r--   1 xxxxx xxxxx 1333  528 22:54 pom.xml.versionsBackup

「versions:revert」で戻すことができます。

$ mvn versions:revert 
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building versions-plugin-example 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- versions-maven-plugin:2.1:revert (default-cli) @ versions-plugin-example ---
[INFO] Restoring /path/to/pom.xml from /path/to/pom.xml.versionsBackup
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.205 s
[INFO] Finished at: 2016-05-28T23:00:41+09:00
[INFO] Final Memory: 9M/201M
[INFO] ------------------------------------------------------------------------

Revertすると、pom.xml.versionsBackupはなくなります。

反対に、確定する場合は「versions:commit」を利用します。

$ mvn versions:commit

「versions:commit」すると、pom.xml.versionsBackupは削除されます。

使ってみて

全部の機能を試してみたわけではありませんが、Versions Maven Pluginを試してみました。

更新する機能はあまり使わない気もしますが、知っておくとちょっと便利そうだなーと思いました。覚えておきましょう。