问题
tl;dr: How can I resolve the "No such property: project
" error when running a Groovy script from within Maven using GMaven? I'm using GMaven 1.5, Groovy 2.2.1. and Java 1.7, and Maven 3.1.0 .
I have a Groovy script -DeltaCodeGen
(it has no package)- that generates Java code (for a Java project, this is the only Groovy in the project). It used to be that we would have the script generate the .java
files and those would get checked in, but now we are trying to have them built during the Maven build during the generate-sources phase.
I can run the script successfully from my IDE, but when I try to run it as part of my build I get
Caused by: org.codehaus.groovy.runtime.metaclass.MissingPropertyExceptionNoStack: No such property: project for class: DeltaCodeGen
Despite other answers, my script does not have a variable called project
, nor does contain the string "project" at all. It does not extend any other script. The only library it uses is Reflections (the need for the Servlet API is still an open question).
Running build, I noticed in stderr
(by running mvn -X 2>mvn_err.log
) the following error:
java.lang.NoSuchMethodError: org.codehaus.groovy.runtime.InvokerHelper.getVersion()Ljava/lang/String;
at org.codehaus.gmaven.runtime.v1_6.ProviderImpl.name(ProviderImpl.java:70)
at org.codehaus.gmaven.feature.support.ProviderSupport.asString(ProviderSupport.java:175)
at org.codehaus.gmaven.feature.support.ProviderSupport.toString(ProviderSupport.java:68)
at org.slf4j.helpers.MessageFormatter.safeObjectAppend(MessageFormatter.java:304)
at org.slf4j.helpers.MessageFormatter.deeplyAppendParameter(MessageFormatter.java:276)
at org.slf4j.helpers.MessageFormatter.arrayFormat(MessageFormatter.java:230)
at org.slf4j.helpers.MessageFormatter.format(MessageFormatter.java:152)
at org.slf4j.impl.SimpleLogger.formatAndLog(SimpleLogger.java:414)
at org.slf4j.impl.SimpleLogger.debug(SimpleLogger.java:512)
at org.codehaus.gmaven.runtime.loader.DefaultProviderSelector.register(DefaultProviderSelector.java:135)
at org.codehaus.gmaven.runtime.loader.DefaultProviderSelector.select(DefaultProviderSelector.java:72)
at org.codehaus.gmaven.runtime.loader.DefaultProviderManager.select(DefaultProviderManager.java:102)
at org.codehaus.gmaven.plugin.ProviderMojoSupport.provider(ProviderMojoSupport.java:120)
at org.codehaus.gmaven.plugin.ComponentMojoSupport.feature(ComponentMojoSupport.java:49)
at org.codehaus.gmaven.plugin.ComponentMojoSupport.feature(ComponentMojoSupport.java:42)
at org.codehaus.gmaven.plugin.ComponentMojoSupport.doExecute(ComponentMojoSupport.java:53)
at org.codehaus.gmaven.plugin.MojoSupport.execute(MojoSupport.java:71)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:106)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:318)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:153)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:555)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:214)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:158)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:414)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:357)
My Maven configuration is:
<build>
<plugins>
<plugin>
<!-- see: http://docs.codehaus.org/display/GMAVEN/Executing+Groovy+Code#ExecutingGroovyCode-ExecuteaLocalGroovyScript -->
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>${pom.basedir}/src/main/groovy/DeltaCodeGen.groovy</source>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.9-RC1</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
回答1:
What I finally got to work was replacing GMaven(which is abandoned) with GMavenPlus (which is currently maintained).
Since running a local script isn't currently given as an example, this is what has worked for me in my POM:
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<scripts>
<script>file:/${pom.basedir}/src/main/groovy/DeltaCodeGen.groovy</script>
</scripts>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.2.1</version>
</dependency>
</dependencies>
</plugin>
回答2:
This is a wild stab in the dark but have you tried to bind the Maven "project" property to the script? In your original question someone suggested this by use of interpolation:
def project = ${project}
but that only works for inline scripts. In your case I would try
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>${pom.basedir}/src/main/groovy/DeltaCodeGen.groovy</source>
<properties>
<project>${project}</project>
</properties>
</configuration>
</execution>
</executions>
Then you could probably put this in your actual groovy code: project = properties['project']
I'm guessing that somehow the script runner is looking for a project member of your script where, of course, cmd line execution is not. So I would look toward defining or coercing the value.
来源:https://stackoverflow.com/questions/21462396/running-script-from-gmaven-results-in-no-such-property-project-exception