将使用 JavaCC 的 Maven Java 项目移植到 Scala

Porting a Maven Java project that uses JavaCC to Scala

我正在尝试将使用 JavaCC 的基于 Maven 的 Java 项目移植到 Scala。 我很高兴至少暂时将 JavaCC 生成的 classes 留在 Java 中。 然而,生成的 JavaCC class 取决于正在转换的 classes。

该项目从原始的 Java 源构建得很好,但是如果我用 Scala 源替换它们,Maven 会首先尝试编译 JavaCC 生成的 class 但看不到以及新代码提供的符号。

然后问题似乎是如何让Maven先生成Java代码,然后将Scala代码和生成的代码一起编译,编译器可以找到所有依赖项。解决方案似乎是向 pom.xml 添加一些魔法咒语。那应该是什么?

我目前的pom.xml是:

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>bobj</groupId>
    <artifactId>bobj</artifactId>
    <version>0.9</version>
    <packaging>jar</packaging>

    <name>BOBJ algebraic specification and verification system</name>
    <url>http://github.com/fh-wedel/BOBJ</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <profiles>
        <profile>
            <id>scala-2.10</id>
            <properties>
                <scalaVersion>2.10.4</scalaVersion>
                <scalaBinaryVersion>2.10</scalaBinaryVersion>
            </properties>
            <dependencies>
                <dependency>
                    <groupId>org.scala-lang</groupId>
                    <artifactId>scala-library</artifactId>
                    <version>${scalaVersion}</version>
                </dependency>
                <dependency>
                    <groupId>org.scala-lang</groupId>
                    <artifactId>scala-swing</artifactId>
                    <version>${scalaVersion}</version>
                </dependency>
            </dependencies>
        </profile>
    </profiles>

    <build>
        <sourceDirectory>src/main/java</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>bobj.BOBJ</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>javacc-maven-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <id>javacc</id>
                        <goals>
                            <goal>javacc</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <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>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>3.1.6</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <args>
                        <!-- work-around for https://issues.scala-lang.org/browse/SI-8358 -->
                        <arg>-nobootcp</arg>
                    </args>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>1.9.1</version>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>src/main/scala</source>
                                <source>src/main/javacc</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

这不是我写的。我从项目附带的 pom.xml 开始,并添加了 scala 和 build-helper-maven-plugin 的部分 我从其他来源复制的。我的 Maven-fu 很少,所以如果我做的事情被严重误导,我不会感到惊讶。

原来我忘了为 mixed scala java project

设置 scala-maven-plugin 声明

scala-maven-plugin 需要

            <executions>
                <execution>
                    <id>scala-process-resources</id>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>add-source</goal>
                        <goal>compile</goal>
                    </goals>
                </execution>
                <execution>
                    <id>scala-process-test-resources</id>
                    <phase>process-test-resources</phase>
                    <goals>
                        <goal>testCompile</goal>
                    </goals>
                </execution>
            </executions>

而不是通常的

            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>testCompile</goal>
                    </goals>
                </execution>
            </executions>