4

My pom.xml looks like this:

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

and script.groovy looks like this:

def value = project.properties['my.property']
log.info "my.property value = $value"

When i run mvn validate -Dmy.property=cmdValue script will write

[INFO] my.property value = defaultValue

It will write "defaultValue", but i definitively need overridden value "cmdValue".

bugs_
  • 3,544
  • 4
  • 34
  • 39
  • 1
    For those who find there way here, there is also an issue that was opened about this: https://github.com/groovy/GMavenPlus/issues/72. – Keegan Sep 01 '20 at 04:29

2 Answers2

4

I have solution, that works for me, but is little bit disappointing. Write script like this:

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') will return value defined in command line. Unfortunately it will return null, when it is not defined on command line. In this case I use value from project.properties['my.property'].

I wonder, if there is some better solution?

It is sad, that in examples from GMavenPlus plugin is project.properties['my.property'], but it doesn't work well :(

bugs_
  • 3,544
  • 4
  • 34
  • 39
  • I use `"${prop}"` works fine for me inline in my CDATA script but I never tried loading a script from a file. can't get it to work without `{}` – ycomp Aug 13 '17 at 15:54
  • 1
    `project.properties` is properties from the POM, `session.userProperties` is properties from the environment, GMavenPlus is just exposing the classes provided by Maven. – Keegan Sep 01 '20 at 03:48
2

How about use the properties to bind Maven's properties? Like this

<?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>

I tested like this

$ mvn org.codehaus.gmavenplus:gmavenplus-plugin:execute
>>defaultValue
$ mvn -DsomeProp=newValue org.codehaus.gmavenplus:gmavenplus-plugin:execute
>>newValue
Keegan
  • 11,345
  • 1
  • 25
  • 38
  • Thank you! It works :) - I see just two disadvantages. I have to manually define each property. And because of "bindPropertiesToSeparateVariables" i cannot use "log" and other variables in script. For now i will stick to solution with "getPropertyValue()" method in script. But now i have backup solution. Thank you again! – bugs_ Apr 21 '17 at 08:52
  • For the first issue, that's completely a matter of style, I edited the answer to use separate variables. For the second issue, yea, it's kinda cumbersome. – Keegan Apr 21 '17 at 12:57