一个好的解决方案是利用Maven Enforcer插件。
更新到1.4.2
从1.4.2版本开始(尚未发布,请参见增强请求MENFORCER-204),有一个新
requireSnapshotVersion规则,该规则强制所构建的项目具有快照版本。
<plugin> <artifactId>maven-enforcer-plugin</artifactId> <version>1.4.2</version> <executions> <execution> <id>enforce-snapshot</id> <goals> <goal>enforce</goal> </goals> <configuration> <rules> <requireSnapshotVersion/> </rules> <fail>${fail.if.release}</fail> </configuration> </execution> </executions></plugin>编写自定义规则
直到版本1.4.1,如果当前项目是SNAPSHOT版本,没有内置规则会失败,但是我们仍然可以使用该
evaluateBeanshell规则。
这样做的目的是使构建失败,因为默认情况下该版本不是快照版本。当当前项目在发行版中时,请禁用该规则。
为此,您可以在POM中包含以下内容:
<plugin> <artifactId>maven-enforcer-plugin</artifactId> <version>1.4.1</version> <executions> <execution> <id>enforce-beanshell</id> <goals> <goal>enforce</goal> </goals> <configuration> <rules> <evaluateBeanshell> <condition>"${project.version}".endsWith("-SNAPSHOT")</condition> </evaluateBeanshell> </rules> <fail>${fail.if.release}</fail> </configuration> </execution> </executions></plugin>这是执行一个BeanShell脚本来评估项目的版本。如果以结尾结束,
-SNAPSHOT则规则通过,否则,规则失败,构建结束。确定版本是否为快照。(快照版本的严格规则更为复杂,但这应该涵盖所有用例)。因此,此规则将验证正在构建的项目具有SNAPSHOT版本。
上面的两种配置都将Maven属性声明为
<property> <fail.if.release>true</fail.if.release></property>
当
mvn deploy在SNAPSHOT版本上运行时,它们会使您的构建失败,并确保没有将SNAPSHOT意外部署到发行版本库中。
然后,执行发布时需要禁用该规则。为此,我们可以定义一个
release配置文件以禁用所定义的规则:
<profile> <id>release</id> <properties> <fail.if.release>false</fail.if.release> </properties></profile>
并在发布时激活该配置文件
mvn release:prepare release:perform -Darguments="-Prelease"



