目的
maven plugin の versions-maven-plugin でベータ版を除外する方法をすぐに忘れてしまうので備忘のメモ
versions-maven-pluginとは
pom.xmlに書いた依存ライブラリのバージョンが最新かをチェックできるプラグイン。
使い方
pom.xml の build タグに下記のように書く。
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<dependencies>
<dependency>
...
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</build>
</project>
$ mvn versions:display-dependency-updates とすると、新しいバージョンがあると教えてくれる
The following dependencies in Dependencies have newer versions:
org.springframework:spring-context .... 4.2.5.RELEASE -> 4.2.6.RELEASE
org.springframework:spring-core ....... 4.2.5.RELEASE -> 4.2.6.RELEASE
org.springframework:spring-test ....... 4.2.5.RELEASE -> 4.2.6.RELEASE
他のコマンドは使ったことない。
困る点
ベータ版も最新として表示されてしまう。
The following dependencies in Dependencies have newer versions:
org.mockito:mockito-all ........................ 1.10.19 -> 2.0.2-beta
対応
versions-maven-plugin のオプションで rulesUri が指定できるので、除外ルール用のXMLファイルを用意して、指定してやる。
除外用のルール
下記のようなファイルを作成して、gist にでも置いておく。正規表現でベータ版っぽい名前のやつを無視する。はず。
exclude-beta-rule.xml
<?xml version="1.0" encoding="utf-8"?>
<ruleset comparisonMethod="maven"
xmlns="http://mojo.codehaus.org/versions-maven-plugin/rule/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://mojo.codehaus.org/versions-maven-plugin/rule/2.0.0
http://mojo.codehaus.org/versions-maven-plugin/xsd/rule-2.0.0.xsd">
<ignoreVersions>
<ignoreVersion type="regex">.*-beta\d*</ignoreVersion>
<ignoreVersion type="regex">.*-b\d+</ignoreVersion>
<ignoreVersion type="regex">.*\.Alpha\d*</ignoreVersion>
</ignoreVersions>
<rules>
<rule groupId="*.maven" comparisonMethod="mercury"/>
</rules>
</ruleset>
versions-maven-plugin 用のプロファイルを用意する
除外用のルールファイルの場所を pom.xml からプロパティで参照したいだけ。
m2/settings.xml
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd">
<profiles>
<profile>
<id>versions-plugin-rule</id>
<properties>
<versions.plugin.ruleUri>
(作成したgistのURLなど適当に)
</versions.plugin.ruleUri>
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>versions-plugin-rule</activeProfile>
</activeProfiles>
</settings>
各プロジェクトの pom.xmlを直す
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<dependencies>
<dependency>
...
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<rulesUri>${versions.plugin.ruleUri}</rulesUri>
</configuration>
</plugin>
</plugins>
</build>
</project>
これで -beta のようなバージョンのライブラリは除外される。