2011-10-29 36 views
2

這是我的示例程序,同時使用MVN它拋出我的編譯錯誤編譯,我試圖用靜態方法ExpandoMetaClass添加 -獲取使用org.codehaus.groovy.control.MultipleCompilationErrorsException gmaven插件

@Singleton 
     class ThrowError { 
      def parse() 
      { 
       println "Anish" 
      } 

     } 
     ThrowError.metaClass.static.getMap = {m_var -> ThrowError.instance.parse(m_var) } 

我使用gmaven插件來編譯項目,同時發出MVN編譯 ..........

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

回答

7

與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的類和一個名稱相同的腳本。這是一個衝突。

如果你給這個文件一個不同的名字,那麼你完全沒問題。

+0

如果我導入這種類,腳本類,我將如何運行它的內容? –

+0

腳本有一個運行方法,至於它的類取決於你在那裏定義的當然 – blackdrag

+0

我遇到這種情況時,導入錯誤導致groovy錯誤地解釋它,並在命中定義的類之前生成腳本類定義。 – user2782001