2017-04-26 81 views
0

我目前正在開發一個項目,我想從語法中生成ANTLR解析器,並繼續使用我的Scala代碼中的解析器。將生成的java源文件添加到混合的scala-java項目中

對於這一點,我設置了以下POM文件的新項目:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>foo</groupId> 
    <artifactId>bar</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    <name>${project.artifactId}</name> 
    <description>Foo</description> 

    <properties> 
    <maven.compiler.source>1.6</maven.compiler.source> 
    <maven.compiler.target>1.6</maven.compiler.target> 
    <encoding>UTF-8</encoding> 
    <scala.version>2.11.5</scala.version> 
    <scala.compat.version>2.11</scala.compat.version> 
    <antlr.version>4.7</antlr.version> 
    </properties> 

    <dependencies> 
    <dependency> 
     <groupId>org.scala-lang</groupId> 
     <artifactId>scala-library</artifactId> 
     <version>${scala.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>org.antlr</groupId> 
     <artifactId>antlr4-runtime</artifactId> 
     <version>${antlr.version}</version> 
    </dependency> 

    <!-- Test --> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>4.11</version> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.specs2</groupId> 
     <artifactId>specs2-core_${scala.compat.version}</artifactId> 
     <version>2.4.16</version> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.scalatest</groupId> 
     <artifactId>scalatest_${scala.compat.version}</artifactId> 
     <version>2.2.4</version> 
     <scope>test</scope> 
    </dependency> 
    </dependencies> 

    <build> 
    <pluginManagement> 
     <plugins> 
     <plugin> 
      <groupId>net.alchim31.maven</groupId> 
      <artifactId>scala-maven-plugin</artifactId> 
      <version>3.2.1</version> 
     </plugin> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <version>2.0.2</version> 
     </plugin> 
     <plugin> 
      <groupId>org.antlr</groupId> 
      <artifactId>antlr4-maven-plugin</artifactId> 
      <version>${antlr.version}</version> 
     </plugin> 
     </plugins> 
    </pluginManagement> 
    <plugins> 
     <plugin> 
     <groupId>net.alchim31.maven</groupId> 
     <artifactId>scala-maven-plugin</artifactId> 
     <executions> 
      <execution> 
      <id>scala-compile-first</id> 
      <phase>process-resources</phase> 
      <goals> 
       <goal>add-source</goal> 
       <goal>compile</goal> 
      </goals> 
      </execution> 
      <execution> 
      <id>scala-test-compile</id> 
      <phase>process-test-resources</phase> 
      <goals> 
       <goal>testCompile</goal> 
      </goals> 
      </execution> 
     </executions> 
     </plugin> 
     <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-compiler-plugin</artifactId> 
     <executions> 
      <execution> 
      <phase>compile</phase> 
      <goals> 
       <goal>compile</goal> 
      </goals> 
      </execution> 
     </executions> 
     </plugin> 
     <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-surefire-plugin</artifactId> 
     <version>2.18.1</version> 
     <configuration> 
      <useFile>false</useFile> 
      <disableXmlReport>true</disableXmlReport> 
      <!-- If you have classpath issue like NoDefClassError,... --> 
      <!-- useManifestOnlyJar>false</useManifestOnlyJar --> 
      <includes> 
      <include>**/*Test.*</include> 
      <include>**/*Suite.*</include> 
      </includes> 
     </configuration> 
     </plugin> 
     <plugin> 
     <groupId>org.antlr</groupId> 
     <artifactId>antlr4-maven-plugin</artifactId> 
     <executions> 
      <execution> 
      <id>antlr</id> 
      <goals> 
       <goal>antlr4</goal> 
      </goals> 
      </execution> 
     </executions> 
     <configuration> 
      <visitor>true</visitor> 
     </configuration> 
     </plugin> 
    </plugins> 
    </build> 
</project> 

編譯在默認位置的語法文件的項目工作得很好 - Java源代碼生成到target/generated-sources/antlr預期。我不能在我的Scala代碼中導入生成的分析器(我們稱之爲FooParser) - IntelliJ認識到有一些東西叫 FooParser,但是當我嘗試自動導入它時,它失敗了。

另外,從命令行編譯mvn compile源代碼 取決於FooParser失敗。

回答

0

整個事情的解決是相當愚蠢的: 我的文件結構是錯誤的:

src/ 
    main/ 
    antlr/ 
     Foo.g4 

你必須把你的語法正確成包,否則 整個事情不會是正確的「導入的」。

src/ 
    main/ 
    antlr/ 
     packagename/ 
     Foo.g4 
相關問題