使用 gmaven 插件获取 org.codehaus.groovy.control.MultipleCompilationErrorsException
这是我的示例程序,在使用 mvn 编译时它会抛出编译错误,我正在尝试使用 ExpandoMetaClass 添加静态方法 -
@Singleton
class ThrowError {
def parse ()
{
println "Anish"
}
}
ThrowError.metaClass.static.getMap = {m_var -> ThrowError.instance.parse(m_var) }
我正在使用 gmaven 插件来编译项目,同时发出 mvncompile ............
[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 构建插件条目
<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>
This is my sample program, while compiling using mvn it throws me the compilation error, i'm trying to add a Static Methods using ExpandoMetaClass -
@Singleton
class ThrowError {
def parse ()
{
println "Anish"
}
}
ThrowError.metaClass.static.getMap = {m_var -> ThrowError.instance.parse(m_var) }
i'm using gmaven plugin to compile the project, while issuing 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
this is my pom.xml entry gmaven build plugin entry
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的答案与 Groovy 邮件列表中的答案相同,也许还有更多解释...在 Groovy 中,我们有脚本和类。类是具有类结构的一切,就像 Java 中的那样。例如,class B{} 是一个类结构,并定义了一个类 B。脚本也是类,但它们不属于这样的结构。如果您现在有“class B{}; def b = new B()”,那么您就有了 B 的类结构,而且还有一个内容为“def b = new B()”的脚本。正如我所说,这也是一个类,但是该类的名称是什么?该名称由文件名定义,该脚本在其中定义(如果没有文件,则选择像 script1456 这样的名称)。现在您可以创建一个 B.groovy,其内容为“class B{}; def b = new B()”。将有一个名为 B 的类,以及一个同名的脚本。这是一个冲突。
如果你给文件起一个不同的名称,那就完全没问题了。
Same answer here as on the Groovy mailing list, well a bit more explained maybe... In Groovy we have scripts and classes. A class is everything with a class structure, like in Java. For example class B{} is a class structure and defines a class B. scripts are classes too, but they are not in such a strucuture. If you now have "class B{}; def b = new B()",you have a class structure for B, but also a script with the content "def b = new B()". As I said thisis a class as well, but what is the name of that class? The name is defined by the name of the file, that script is defined in (if there is no file, then a name like script1456 is chosen). Now you may create a B.groovy, with the content "class B{}; def b = new B()". There would be one class named B, and a script with the very same name. This is a conflict.
You are perfectly fine if you give the file a different name.