使用Maven构建Groovy项目时出现源代码错误

使用Maven构建Groovy项目时出现源代码错误,maven,groovy,gmaven-plugin,Maven,Groovy,Gmaven Plugin,我正试图用maven构建我的第一个groovy项目,但我从maven那里得到了以下错误。。这是某种类型的源错误,但我不明白为什么我会得到它 [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.186s [INFO] Finished at: Fri Jan 25 15:36:09 EST 2013 [INF

我正试图用maven构建我的第一个groovy项目,但我从maven那里得到了以下错误。。这是某种类型的源错误,但我不明白为什么我会得到它

[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.186s
[INFO] Finished at: Fri Jan 25 15:36:09 EST 2013
[INFO] Final Memory: 15M/163M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.gmaven:gmaven-plugin:1.3:execute (default) on project groovyhello: org.codehaus.groovy.runtime.metaclass.MissingPropertyExceptionNoStack: No such property: project for class: org.smith.Example -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

Process finished with exit code 1
下面是我的源代码:

package org.smith

/**
 * Example Groovy class.
 */
class Example
{
    def show() {
        println 'Hello World'
    }
}
这是我的pom.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>org.smith</groupId>
<artifactId>groovyhello</artifactId>
<name>Example Project</name>
<version>1.0-SNAPSHOT</version>

<dependencies>
    <dependency>
        <groupId>org.codehaus.groovy.maven.runtime</groupId>
        <artifactId>gmaven-runtime-1.6</artifactId>
        <version>1.0</version>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>


        <plugin>
            <groupId>org.codehaus.gmaven</groupId>
            <artifactId>gmaven-plugin</artifactId>
            <version>1.3</version>
            <executions>
                <execution>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>execute</goal>
                    </goals>
                    <configuration>
                        <source>${pom.basedir}/src/main/groovy/org/smith/Example.groovy</source>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

您的第一条线索是错误:没有这样的属性:project for class:org.jsmith.Example 抱歉,我重复了你的错误作为你的答案,但让我解释一下。这意味着在源代码的某个地方有一个对变量项目的引用。可能在源代码中您没有发布,或者可能在源代码中您未及时更改它,并且在您发布之前

我想在类定义之后,您可能在包名或一些额外的测试代码中有输入错误?例如,类似这样的事情可能会产生这样的错误:

package org.smith

/**
 * Example Groovy class.
 */
class Example
{
    def show() {
        println 'Hello World'
    }
}
println project.path

同样,您应该发布发生错误时更新的代码和与代码匹配的确切错误。根据上面的内容很难确定问题所在。

如果您使用的是脚本而不是内联,则必须定义project并绑定到mavenProject

def project = ${project}; // this will bind to the Maven Project property.
同样地

def session = ${session} //will bind to MavenSession

-查找自定义属性

您的groovy文件看起来不像是可以执行的脚本。你有没有试着简单地做一些打印,而不是上课?你试过使用gmaven插件的1.4版吗?我已经更新了我的答案以反映你更新后的错误。似乎错误仍然与代码不同步。请发布错误发生时存在的代码。我有相同的错误,并且我的脚本中没有名为project或字符串proj的变量。我面临与OP相同的错误。您能详细说明您的答案吗?您必须定义project并绑定到mavenProject是什么意思?@AlexKravets您找到解决方案了吗?@ArtB不,我没有解决这个问题,而是用ANT脚本调用了Groovy类,这在当时对我所做的工作更有意义。@AlexKravets我解决了它。@ArtB非常好,我会记下来。谢谢你的回复。