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

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

问题描述:

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

I have a mixed Scala/Java project that doesn't compile well.

当Java代码试图在同一程序包中调用Scala代码时,就会出现问题.

The problem arises when Java code is trying to call Scala code in the same package.

当然,我有标准布局:

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

我看过其他类似的*问题,但其他问题也没有帮助.

I've looked at other similar * questions but this question is a little outdated. This other question doesn't help either.

我还遵循了 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.xml导入的具有Scala插件的IDEA项目可以成功编译并运行我的项目.

IDEA project with Scala plugin imported from the pom.xml can compile and run my project successfully.

正确的做法是什么? Java代码是否被编译两次?首先是Scala插件,是Java插件?.

What is the right way of doing it? Does the Java code gets compiled twice? First by the Scala plugin and the by the Java plugin?.

以下是pom.xml的有效示例:

Here is a working example of 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>*</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>