2013-02-17 132 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

相关问题