我的混合 Scala/Java Maven 项目无法编译

2022-09-01 17:18:53

我有一个混合的Scala/Java项目,编译不好。

当Java代码尝试在同一包中调用Scala代码时,问题就出现了。

当然,我有标准的布局:

  • src/main/java
  • src/main/scala
  • src/test/java
  • src/test/scala

我看过其他类似的Stackoverflow问题,但这个问题有点过时了。另一个问题也无济于事。

我也关注了 scala-maven-plugin 文档页面

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>3.1.6</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.0.2</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>
    </plugins>
</build>

我试图关注这篇博客文章,但没有成功。

带有从pom导入的Scala插件的IDEA项目.xml可以成功编译并运行我的项目。

正确的方法是什么?Java代码是否被编译了两次?首先是Scala插件和Java插件?。


答案 1

以下是pom.xml的工作示例:

<?xml version="1.0" encoding="UTF-8"?>
<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>stackoverflow</groupId>
<artifactId>q24448582</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
    <scala.version>2.10.3</scala.version>
</properties>

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

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.scala-tools</groupId>
            <artifactId>maven-scala-plugin</artifactId>
            <version>2.15.2</version>
            <executions>

                <execution>
                    <id>compile</id>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                    <phase>compile</phase>
                </execution>
                <execution>
                    <id>test-compile</id>
                    <goals>
                        <goal>testCompile</goal>
                    </goals>
                    <phase>test-compile</phase>
                </execution>
                <execution>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

</project>

答案 2

问题 1

起初,我认为问题是由于使用了maven-compiler-plugin的现代版本。我使用的是3.0版本而不是2.0.2。

解决方案是: - 删除 maven-编译器-插件 - 添加此设置:增量

最后,我的pom.xml是这样的:

<plugin>
    <groupId>net.alchim31.maven</groupId>
    <artifactId>scala-maven-plugin</artifactId>
    <version>3.1.6</version>
    <configuration>
        <recompileMode>incremental</recompileMode>
        <args>
            <arg>-deprecation</arg>
            <arg>-explaintypes</arg>
            <arg>-target:jvm-1.7</arg>
        </args>
    </configuration>
    <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>add-source</goal>
                <goal>testCompile</goal>
            </goals>
        </execution>
    </executions>
</plugin>

问题 2

我遇到的另一个问题是这样的:

<dependencies>
    <dependency>
        <groupId>org.json4s</groupId>
        <artifactId>json4s-native_${scala.version}</artifactId>
        <version>3.2.9</version>
    </dependency>
    <dependency>
        <groupId>org.scala-lang</groupId>
        <artifactId>scala-library</artifactId>
        <version>${scala.version}.4</version>
    </dependency>
</dependencies>

<properties>
    <scala.version>2.10</scala.version>
</properties>

这使得这个已解决的问题出现:https://issues.scala-lang.org/browse/SI-5733

可能是它使用的是旧版本的Scala编译器。

解决方案是将scala.version重命名为scala.major,因为此属性具有特殊含义。


推荐