使用gmaven插件获取org.codehaus.groovy.control.multipleComplationErrorSexception
这是我的示例程序,当使用mvn编译时,它会引发编译错误,我尝试使用ExpandoMetaClass添加一个静态方法-使用gmaven插件获取org.codehaus.groovy.control.multipleComplationErrorSexception,groovy,gmaven-plugin,expandometaclass,Groovy,Gmaven Plugin,Expandometaclass,这是我的示例程序,当使用mvn编译时,它会引发编译错误,我尝试使用ExpandoMetaClass添加一个静态方法- @Singleton class ThrowError { def parse () { println "Anish" } } ThrowError.metaClass.static.getMap = {m_var ->
@Singleton
class ThrowError {
def parse ()
{
println "Anish"
}
}
ThrowError.metaClass.static.getMap = {m_var -> ThrowError.instance.parse(m_var) }
我正在使用gmaven插件来编译这个项目,同时发布mvn compile
[ERROR] Failed to execute goal org.codehaus.gmaven:gmaven-plugin:1.2:generateStubs (default) on project TestExpandoMetaClass: startup failed:
[ERROR] /C:/groovy/ThrowError.groovy: 4
: Invalid duplicate class definition of class ThrowError : The source /C:/groovy/ThrowError.groovy contains at least two definitions of the class ThrowError.
**[ERROR] One of the classes is a explicit generated class using the class statement, the other is a class generated from the s
cript body based on the file name. Solutions are to change the file name or to change the class name.**
[ERROR] @ line 4, column 1.
[ERROR] @Singleton
[ERROR] ^
[ERROR]
[ERROR] 1 error
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.gmaven:gmaven-plugin:1.2:generate
Stubs (default) on project TestExpandoMetaClass: startup failed:
/C:/groovyThrowError.groovy: 4: Invali
d duplicate class definition of class ThrowError : The source /groovy/ThrowError.groovy contains at least two definitions of the class ThrowError
这是我的pom.xml条目gmaven build plugin条目
<project>
............
............
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.2</version>
<configuration>
<providerSelection>1.7</providerSelection>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.gmaven.runtime</groupId>
<artifactId>gmaven-runtime-1.7</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>1.7.2</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>generateStubs</goal>
<goal>compile</goal>
<goal>generateTestStubs</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
..........
..........
</project>
............
............
org.apache.maven.plugins
maven编译器插件
1.6
1.6
org.codehaus.mojo
execmaven插件
1.1
JAVA
org.codehaus.gmaven
gmaven插件
1.2
1.7
org.codehaus.gmaven.runtime
gmaven-runtime-1.7
1.2
org.codehaus.groovy
groovy all
1.7.2
发电集团
编译
生成测试存根
测试编译
..........
..........
这里的答案与Groovy邮件列表上的答案相同,可能会有更多的解释。。。在Groovy中,我们有脚本和类。类是具有类结构的一切,就像在Java中一样。例如,类B{}是一个类结构,定义了类B。脚本也是类,但它们不在这样的结构中。如果您现在有了“class B{};def B=new B()”,那么您就有了B的类结构,还有一个内容为“def B=new B()”的脚本。正如我所说,这也是一个类,但是这个类的名称是什么?名称由文件名定义,脚本在中定义(如果没有文件,则选择类似于script1456的名称)。现在您可以创建一个B.groovy,其内容为“class B{};def B=new B()”。将有一个名为B的类和一个同名的脚本。这是一场冲突
如果您为文件指定不同的名称,您就完全可以了。如果我导入这种类,script类,我将如何运行它的内容?script有一个run方法,至于类,这取决于您在那里定义了什么。当然,当导入错误导致groovy错误地解释它并在命中定义的类之前生成脚本类定义时,我会遇到这种情况。