0

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?

cdprete
  • 53
  • 1
  • 9
  • If I see correct that task can be simply done via profiles without groovy compiling etc... – khmarbaise Apr 01 '22 at 08:42
  • That's not the only script I've. Also, the `java.version` property can be changed by the user while using profiles would use the JDK version used to run Maven and it's not the same. – cdprete Apr 01 '22 at 09:03
  • And what will changing the java.version property help? What kind of problem are you trying to solve? If you have scripts I would suppose there are problems in your build.... – khmarbaise Apr 01 '22 at 09:05
  • Make a separation between runtime of Maven and the used JDK for compiling/testing you can use toolchain....The question is which JDK you need to use? – khmarbaise Apr 01 '22 at 09:05
  • As I already said, I've other scripts as well, so don't concentrate exactly on it :) The point is that I need to access this `project` property and now what the script does :) – cdprete Apr 01 '22 at 09:21
  • That does not help because not getting the full picture will result in wrong recommendations... so it's better to have the full picture what kind of problems you are trying to solve... Using scripts is usually the wrong way... in edge cases a plugin is usually a better way... – khmarbaise Apr 01 '22 at 09:27
  • A plugin is more complex to develop and maintain than some small scripts sometimes (like in this case). In this specific case, creating a plugin just to set a property accordingly is an overkill. – cdprete Apr 01 '22 at 10:33
  • Small scripts can not really being tested..a maven plugin can... furthermore setting this property via script and compiling groovy code...is an overkill... – khmarbaise Apr 01 '22 at 10:40
  • I would say it's a question of tastes :) – cdprete Apr 01 '22 at 11:20

2 Answers2

0

Based on the given thing:

   <profile>
      <id>javac-release</id>
      <activation>
        <jdk>[9,)</jdk>
      </activation>
      <properties>
        <maven.compiler.release>8</maven.compiler.release>
      </properties>
    </profile>
khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • This will take into account the JDK Maven uses which could not be the same of the one needed by the project. For example, I run with Java 11 but I want to build against Java 8. – cdprete Apr 01 '22 at 11:21
  • If you build with JDK11 you can set release to 8 and that's it... Or you use as already mentioned toolchain if you really need to use a different JDK for compiling/testing vs. the one which is used to run maven... – khmarbaise Apr 01 '22 at 11:31
  • as I've already written in the comments under the question, please don't focus on this specific script. I've other scripts as well as part of the same build that need to have access to the `project` object. – cdprete Apr 03 '22 at 16:36
0

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

cdprete
  • 53
  • 1
  • 9