GMavenPlus - 如何在 gmavenplus-plugin 执行的脚本中获取 Maven 属性 值

GMavenPlus - How to get Maven property value in script executed by gmavenplus-plugin

我的 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

它将写入 "defaultValue",但我确实需要覆盖值 "cmdValue"。

我有解决方案,对我有用,但有点令人失望。像这样写脚本:

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') 将在命令行中定义 return 值。不幸的是,当它没有在命令行上定义时,它将 return null。在这种情况下,我使用 project.properties['my.property'].

中的值

不知有没有更好的解决办法?

令人难过的是,在 GMavenPlus 插件的示例中是 project.properties['my.property'],但效果不佳:(

使用属性绑定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