从GMaven运行脚本导致“没有这样的属性:项目”异常

时间:2014-01-30 16:44:55

标签: java maven groovy gmaven

tl; dr:如何在使用GMaven从Maven中运行Groovy脚本时解决“No such property: project”错误?我正在使用GMaven 1.5,Groovy 2.2.1。和Java 1.7,以及Maven 3.1.0。


我有一个Groovy脚本 - DeltaCodeGen(它没有包) - 生成Java代码(对于Java项目,这是项目中唯一的Groovy)。过去,我们会让脚本生成.java文件,然后检入这些文件,但现在我们正在尝试在generate-sources阶段的Maven构建过程中构建它们。

我可以从我的IDE成功运行脚本,但是当我尝试将其作为构建的一部分运行时,我得到了

Caused by: org.codehaus.groovy.runtime.metaclass.MissingPropertyExceptionNoStack: No such property: project for class: DeltaCodeGen

尽管other answers,我的脚本没有名为project的变量,也没有包含字符串“project”。它不会扩展任何其他脚本。它使用的唯一库是Reflections(对Servlet API的需求仍然是open question)。

正在运行构建,我在stderr(通过运行mvn -X 2>mvn_err.log)注意到以下错误:

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)

我的Maven配置是:

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

2 个答案:

答案 0 :(得分:4)

我最终开始工作的是用GMaven(目前正在维护)替换GMavenPlus(已被放弃)。

由于目前没有以example的形式提供本地脚本,因此这在我的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>

答案 1 :(得分:-1)

这是一个黑暗中的疯狂刺,你试图将Maven“project”属性绑定到脚本吗?在您的原始问题中,有人通过使用插值来建议:

def project = ${project}

但这仅适用于内联脚本。在你的情况下,我会尝试

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

然后你可以把它放在你的实际groovy代码中: project = properties ['project']

我猜测脚本运行器正在寻找脚本的项目成员,当然,cmd行执行不是。所以我会考虑定义或强制价值。