我的pom.xml看起来像这样:
<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>
<groupId>mygroup</groupId>
<artifactId>myartefact</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<my.property>defaultValue</my.property>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<scripts>
<script>file:///${basedir}/script.groovy</script>
</scripts>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.7</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
和script.groovy看起来像这样:
def value = project.properties['my.property']
log.info "my.property value = $value"
当我运行mvn validate -Dmy.property=cmdValue
脚本时会写
[INFO] my.property value = defaultValue
它会写&#34; defaultValue&#34;,但我明确需要覆盖值&#34; cmdValue&#34;。
答案 0 :(得分:2)
我有解决方案,对我有用,但有点令人失望。写这样的脚本:
def value = getPropertyValue('my.property')
log.info "my.property value = $value"
String getPropertyValue(String name) {
def value = session.userProperties[name]
if (value != null) return value //property was defined from command line e.g.: -DpropertyName=value
return project.properties[name]
}
session.userProperties('my.property')
将返回在命令行中定义的值。不幸的是,当它没有在命令行中定义时,它将返回null
。在这种情况下,我使用project.properties['my.property']
中的值。
我想知道,如果有更好的解决方案吗?
令人遗憾的是,GMavenPlus插件中的示例是project.properties['my.property']
,但它并不能很好地运行:(
答案 1 :(得分:2)
如何使用属性绑定Maven的属性?喜欢这个
<?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>
<groupId>com.github.groovy</groupId>
<artifactId>gmavenplus-test</artifactId>
<version>1.0-SNAPSHOT</version>
<name>GMavenPlus Test</name>
<properties>
<someProp>defaultValue</someProp>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.5</version>
<configuration>
<properties>
<someProp>${someProp}</someProp>
</properties>
<scripts>
<script><![CDATA[
println ">>${someProp}"
]]></script>
</scripts>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.10</version>
<classifier>indy</classifier>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
我像这样测试了
$ mvn org.codehaus.gmavenplus:gmavenplus-plugin:execute
>>defaultValue
$ mvn -DsomeProp=newValue org.codehaus.gmavenplus:gmavenplus-plugin:execute
>>newValue