Search code examples
mavengroovymaven-invoker-plugin

How to use ScriptVariables in Maven-Invoker-Plugin with PostBuildHookScript in Groovy


I am using Maven-Inovker-Plugin to run integration tests. I have a post-build-hook-script called verify.groovy to verify the results of the integration tests. In the groovy script, I need to access some properties from the original POM. How do I do that?

So far, I have put this in the configuration block for maven-invoker-plugin :

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-invoker-plugin</artifactId>
                <version>1.7</version>
                <configuration>
                    <debug>true</debug>
                    <cloneProjectsTo>${project.build.directory}/it</cloneProjectsTo>
                    <projectsDirectory>src/test/resources/invoker-tests</projectsDirectory>
                    <pomIncludes>
                        <pomInclude>*/pom.xml</pomInclude>
                    </pomIncludes>
                    <postBuildHookScript>verify</postBuildHookScript>
                    <scriptVariables>
                        <param>${skip.var1}</param>
                        <param>${skip.var2}</param>
                    </scriptVariables>
                    <localRepositoryPath>${project.build.directory}/local-repo</localRepositoryPath>
                    <goals>
                        <goal>clean</goal>
                        <goal>package</goal>
                    </goals>
                </configuration>
                <executions>
                    <execution>
                        <id>integration-test</id>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

I have put values in scriptVariables from documentation here. I am pretty sure I have done it wrong, what is the correct way?

And then how do I access variable skip.var1 and skip.var2 in my groovy script? I am unable to find any documentation or examples on this. So far I have tried the following, but it does not work.

def var1 = skip.var1

But I get error :

groovy.lang.MissingPropertyException: No such property: skip for class: Script1

How do I correctly access the variable?


Solution

  • Had to change the version to 1.9 or higher, as mentioned here : http://maven.apache.org/plugins/maven-invoker-plugin/integration-test-mojo.html#scriptVariables

    Also had to have below configuration for passing the variables:

                        <scriptVariables>
                            <var1>${skp.var1}</var1>
                        </scriptVariables>
    

    And in the groovy script, I can directly use var1.

    So this is what worked:

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-invoker-plugin</artifactId>
                    <version>2.0.0</version>
                    <configuration>
                        <debug>true</debug>
                        <cloneProjectsTo>${project.build.directory}/it</cloneProjectsTo>
                        <projectsDirectory>src/test/resources/invoker-tests</projectsDirectory>
                        <pomIncludes>
                            <pomInclude>*/pom.xml</pomInclude>
                        </pomIncludes>
                        <postBuildHookScript>verify</postBuildHookScript>
                        <scriptVariables>
                            <var1>${skip.var1}</var1>
                        </scriptVariables>
                        <localRepositoryPath>${project.build.directory}/local-repo</localRepositoryPath>
                        <goals>
                            <goal>clean</goal>
                            <goal>package</goal>
                        </goals>
                    </configuration>
                    <executions>
                        <execution>
                            <id>integration-test</id>
                            <goals>
                                <goal>integration-test</goal>
                                <goal>verify</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>