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

时间:2014-06-27 09:35:43

标签: java scala maven

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

当Java代码试图在同一个包中调用Scala代码时,会出现问题。

当然,我有标准布局:

  • 的src /主/ JAVA
  • 的src /主/阶
  • 的src /测试/ JAVA
  • 的src /测试/阶

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

我也关注了scala-maven-plugin documentation page

<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>

我尝试过关注blog post

,但未成功

使用从pom.xml导入的Scala插件的IDEA项目可以成功编译和运行我的项目。

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

4 个答案:

答案 0 :(得分:15)

以下是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>

答案 1 :(得分:11)

问题1

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

解决方案是:   - 删除maven-compiler-plugin   - 添加此设置:增量

最后我的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,因为此属性具有特殊含义。

答案 2 :(得分:5)

为了让scala-maven-plugin尊重Java 1.8,但仍然允许java和scala的混合编译,我们使用这个:

...
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
    </configuration>
    <executions>
        <execution>
            <id>default-compile</id>
            <phase>none</phase>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>net.alchim31.maven</groupId>
    <artifactId>scala-maven-plugin</artifactId>
    <version>3.2.2</version>
    <configuration>
        <recompileMode>incremental</recompileMode>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>compile</goal>
                <goal>testCompile</goal>
            </goals>
        </execution>
    </executions>
</plugin>
...

答案 3 :(得分:0)

我在pom.xml文件<build>块中添加了打击代码,并且运行良好!

    <build>
        <finalName>${project.artifactId}</finalName>

        <plugins>
            <!-- SCALA AND JAVA MIX COMPILATION -->
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>scala-compile</id>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>scala-test-compile</id>
                        <goals>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.2</version>
                <configuration>
                    <artifactSet>
                        <includes>
                            <include>*:*</include>
                        </includes>
                    </artifactSet>

                    <filters>
                        <filter>
                            <artifact>*:*</artifact>
                            <excludes>
                                <exclude>META-INF/*.SF</exclude>
                                <exclude>META-INF/*.DSA</exclude>
                                <exclude>META-INF/*.RSA</exclude>
                            </excludes>
                        </filter>
                    </filters>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>

                        <configuration>
                            <finalName>${project.artifactId}-shaded-${project.version}</finalName>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>

        <pluginManagement>
            ...
        </pluginManagement>

        <resources>
            ...
        </resources>
    </build>