maven 中有没有办法确保设置 属性
Is there a way in maven to assure that a property is set
我刚刚找到了一个由错误的 属性 值引起的困难的 Maven 问题。
属性 是指向备用 JVM 的路径,测试使用了 运行 次。
我想通过检测路径是否有效来让 Maven 尽早失败。
有什么方法可以做到这一点?
我打算先研究一下 ant运行 看看有没有办法让它 运行 以便它可以检查,但这似乎有点过分了。
问题:我怎样才能干净简单地做到这一点?
您可以使用 Enforcer Maven Plugin and its Require Property 规则,您可以在其中强制存在某个 属性,可选地使用某个值(匹配的正则表达式),否则构建失败。
This rule can enforce that a declared property is set and optionally evaluate it against a regular expression.
一个简单的片段是:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.4.1</version>
<executions>
<execution>
<id>enforce-property</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireProperty>
<property>basedir</property>
<message>You must set a basedir property!</message>
<regex>.*\d.*</regex>
<regexMessage>The basedir property must contain at least one digit.</regexMessage>
</requireProperty>
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
</plugin>
是的,您可以使用 maven-enforcer-plugin
for this task. This plugin is used to enforce rules during the build and it has a built-in requireFilesExist
规则:
This rule checks that the specified list of files exist.
以下配置将强制文件 ${project.build.outputDirectory}/foo.txt
存在,如果不存在,构建将失败。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.4.1</version>
<executions>
<execution>
<id>enforce-files-exist</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireFilesExist>
<files>
<file>${project.build.outputDirectory}/foo.txt</file>
</files>
</requireFilesExist>
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
</plugin>
使用 Maven Enforcer 插件的 Require Files Exist 规则。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.4.1</version>
<executions>
<execution>
<id>enforce-files-exist</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireFilesExist>
<files>
<file>${property.to.check}</file>
</files>
</requireFilesExist>
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
我刚刚找到了一个由错误的 属性 值引起的困难的 Maven 问题。
属性 是指向备用 JVM 的路径,测试使用了 运行 次。 我想通过检测路径是否有效来让 Maven 尽早失败。 有什么方法可以做到这一点?
我打算先研究一下 ant运行 看看有没有办法让它 运行 以便它可以检查,但这似乎有点过分了。
问题:我怎样才能干净简单地做到这一点?
您可以使用 Enforcer Maven Plugin and its Require Property 规则,您可以在其中强制存在某个 属性,可选地使用某个值(匹配的正则表达式),否则构建失败。
This rule can enforce that a declared property is set and optionally evaluate it against a regular expression.
一个简单的片段是:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.4.1</version>
<executions>
<execution>
<id>enforce-property</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireProperty>
<property>basedir</property>
<message>You must set a basedir property!</message>
<regex>.*\d.*</regex>
<regexMessage>The basedir property must contain at least one digit.</regexMessage>
</requireProperty>
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
</plugin>
是的,您可以使用 maven-enforcer-plugin
for this task. This plugin is used to enforce rules during the build and it has a built-in requireFilesExist
规则:
This rule checks that the specified list of files exist.
以下配置将强制文件 ${project.build.outputDirectory}/foo.txt
存在,如果不存在,构建将失败。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.4.1</version>
<executions>
<execution>
<id>enforce-files-exist</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireFilesExist>
<files>
<file>${project.build.outputDirectory}/foo.txt</file>
</files>
</requireFilesExist>
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
</plugin>
使用 Maven Enforcer 插件的 Require Files Exist 规则。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.4.1</version>
<executions>
<execution>
<id>enforce-files-exist</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireFilesExist>
<files>
<file>${property.to.check}</file>
</files>
</requireFilesExist>
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>