我设立一个Maven构建,和目的地服务器需要进行命令行作为属性(其随后被用于选择适当的配置文件)上指定,例如我可以强制Maven 2要求在命令行上指定属性吗?
mvn -Denv=test
我想如果该属性没有设置,构建失败 - 这是可能的吗?
是的,我是Maven新手。
编辑:我见过this link,这似乎暗示它是不可能的,但我不知道它是如何最新的。
我设立一个Maven构建,和目的地服务器需要进行命令行作为属性(其随后被用于选择适当的配置文件)上指定,例如我可以强制Maven 2要求在命令行上指定属性吗?
mvn -Denv=test
我想如果该属性没有设置,构建失败 - 这是可能的吗?
是的,我是Maven新手。
编辑:我见过this link,这似乎暗示它是不可能的,但我不知道它是如何最新的。
每个人都很近,但有一个在执法专门为一个属性检查规则,不需要蚂蚁或时髦的轮廓:http://maven.apache.org/enforcer/enforcer-rules/requireProperty.html
规则也可以检查属性的值。
我的第一个想法是创建一个配置文件,该配置文件在env属性未设置时处于活动状态,并以某种方式失败。也许你可以编写一个测试该属性的Maven插件,如果它不存在则失败?
或者,你可以用一个非常小的ant-build脚本来测试它。
为了详细说明edbrannin的替代解决方案:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.yourcompany</groupId>
<artifactId>yourproject</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>checkParam</id>
<phase>initialize</phase>
<goals><goal>run</goal></goals>
<configuration>
<tasks>
<fail message="'env' property must be set" unless="env"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
会给你以下的输出:
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] An Ant BuildException has occured: 'env' property must be set
恕我直言,最简单的方法来做到这一点(一个我会去亲自) 。
你可以使用包含<or>
和<equals>
标签嵌套<condition>
甚至控制一组允许值(见Ant手册:http://ant.apache.org/manual/Tasks/conditions.html)
这可以通过执行插件直接完成。 – 2009-04-10 23:47:56
也许你可以使用这样的解决办法:在Maven中,你可以激活,如果配置文件部分房产未设置:
<project>
...
<profiles>
<profile>
<id>failure_profile</id>
<activation>
<property>
<name>!env</name>
</property>
</activation>
</profile>
</profiles>
</project>
然后您应该强制该配置文件总是失败,例如使用Maven的实施者 - 插件:然后
<profile>
<id>failure_profile</id>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<executions>
<execution>
<id>enforce</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<AlwaysFail/>
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
如果不提供-Denv构建就会失败:
[INFO] [enforcer:enforce {execution: enforce}]
[WARNING] Rule 0: org.apache.maven.plugins.enforcer.AlwaysFail failed with message:
Always fails!
[INFO] ---------------------------------------------------------
[ERROR] BUILD ERROR
嗯,这是更详细的则Ant,但纯粹的Maven :)
优秀;非常感谢。正如你所说,它更加冗长,所以我会坚持Olivier的回答作为我接受的答案。让我说我想要一个纯粹的Maven回答 - 道歉。我希望我能接受两个答案。 – Hobo 2009-01-12 11:43:25
更整洁 - 正是我所期待的。谢谢! – Hobo 2009-04-11 15:03:43