mavengroovygmavenplus

Use compiled Groovy scripts with GMavenPlus plugin


I'm trying to use the GMavenPlus Plugin to run some Groovy scripts from Maven which have been compiled and packaged into a jar.

The script is something very simple:

package foo.bar.scripts

import groovy.transform.Field

@Field
private static final String JAVA_VERSION_PROPERTY_NAME = 'java.version'
@Field
private static final String MAVEN_COMPILER_RELEASE_PROPERTY_NAME = 'maven.compiler.release'

def javaVersion = project.properties[JAVA_VERSION_PROPERTY_NAME]?.trim()
if(javaVersion?.isInteger() && javaVersion.toInteger() >= 9) {
    project.properties[MAVEN_COMPILER_RELEASE_PROPERTY_NAME] = javaVersion
}

which then I invoke with:

<plugin>
                <groupId>org.codehaus.gmavenplus</groupId>
                <artifactId>gmavenplus-plugin</artifactId>
                <dependencies>
                    <dependency>
                        <groupId>${project.groupId}</groupId>
                        <artifactId>scripts</artifactId>
                        <version>${project.version}</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <id>adjust-compiler-settings</id>
                        <phase>initialize</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <scripts>
                                <script>foo.bar.scripts.AdjustCompilerSettings.main()</script>
                            </scripts>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

According to the documentation, the project variable should be by default available and it's indeed true if I define the script inline instead of picking it from the jar. Do I've a way to pass such object through?


Solution

  • I didn't find a solution for the problem, but I found (kind of) a workaround for it. That is to package the Groovy sources as jar, publish them and then make Maven download them (with the maven-dependency-plugin) and let the gmavenplus-plugin invoke such sources.

    Not the nicest solution, but it works :)