2013-02-17 309 views
14

我是ANTLR4的新手,看起來沒有v4的Eclipse插件。所以它很適合從.g4文件自動構建Java源代碼。我有一個簡單的,空的Maven項目,具有src/main/java,src/test/java。在哪裏放置.g4文件?我如何使用Maven自動構建語法?如何使用ANTLR4和Maven自動生成詞法分析器+解析器?

我自己POM-測試失敗:

<repository> 
    <id>mvn-public</id> 
    <name>MVNRepository</name> 
    <url>http://mvnrepository.com</url> 
</repository> 

... 

<build> 
    <plugins> 
     <plugin> 
      <groupId>org.antlr</groupId> 
      <artifactId>antlr4-maven-plugin</artifactId> 
      <version>4.0.0</version> 
      <executions> 
       <execution> 
        <goals> 
         <goal>antlr</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 
</build> 

Eclipse中說:

Failure to find org.antlr:antlr4-maven-plugin:pom:4.0.0 in http://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of 
central has elapsed or updates are forced 

回答

17

我創建了4個語法期間與設計從ANTLR支持自動代碼生成一個pom.xml以下要點一個Eclipse構建。它包含必要的生命週期信息,以便m2e知道代碼生成是必要的,並且使用build-helper-maven-plugin明確添加代碼生成文件夾,因爲Eclipse似乎在定位時遇到了一些問題。

在此配置中,語法文件(*.g4)與其他Java源文件並排放置。 Maven插件會自動將正確的package ...語句添加到生成的文件中,因此您不應該在語法本身中包含@header{package ...}行。

https://gist.github.com/sharwell/4979017

+0

謝謝!當我在pom.xml中註釋build-helper-maven-plugin時,ANTLR爲放置的語法構建詞法分析器和解析器。但是使用build-helper-maven-plugin Eclipse說:「插件執行沒有被生命週期配置所覆蓋:org.codehaus.mojo:build-helper-maven-plugin:1.7:add-source(執行:默認,階段:生成源)」。如果沒有手動添加target/generatet-sources/antlr4作爲src文件夾,我無法使用生成的詞法分析器/解析器。還有一個問題:如何提出論據? ' org.example'不起作用。 – Vertex 2013-02-18 20:31:25

+0

如果您告訴它,Eclipse將自動下載適當的擴展以使用構建助手插件。 package子句總是基於源結構中.g4文件的位置由antlr4目標自動添加。要更改生成的代碼包,請將語法本身移至源樹中的所需包。 – 2013-02-18 20:39:34

+0

是的,你是對的!我忽略了快速修復。 – Vertex 2013-02-18 21:36:15

相關問題