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

时间:2015-02-15 09:58:42

标签: scala maven maven-plugin javacc scala-java-interop

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

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

问题似乎是如何让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很小,所以如果我做了一些被误导的事情,我不会感到惊讶。

1 个答案:

答案 0 :(得分:0)

事实证明我忘了为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>