我的混合 Scala/Java Maven 项目无法编译
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24448582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
My mixed Scala/Java Maven project doesn't compile
提问by david.perez
I have a mixed Scala/Java project that doesn't compile well.
我有一个不能很好编译的混合 Scala/Java 项目。
The problem arises when Java code is trying to call Scala code in the same package.
当 Java 代码试图调用同一个包中的 Scala 代码时,就会出现问题。
Of course, I have the standard layout:
当然,我有标准布局:
- src/main/java
- src/main/scala
- src/test/java
- src/test/scala
- 源代码/主/Java
- 源代码/主/斯卡拉
- 源代码/测试/Java
- 源代码/测试/斯卡拉
I've looked at other similar Stackoverflow questions but this questionis a little outdated. This other questiondoesn't help either.
我看过其他类似的 Stackoverflow 问题,但这个问题有点过时了。这另一个问题也无济于事。
I have also followed the scala-maven-plugin documentation page.
我还关注了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>
I have tried unsuccessfully to follow this blog post.
我曾尝试关注此博客文章,但未成功。
IDEA project with Scala plugin imported from the pom.xml can compile and run my project successfully.
从 pom.xml 导入的带有 Scala 插件的 IDEA 项目可以成功编译和运行我的项目。
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?.
正确的做法是什么?Java 代码会被编译两次吗?首先是 Scala 插件和 Java 插件?。
回答by maksim07
Here is a working example of pom.xml :
这是 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>
回答by david.perez
Problem 1
问题一
At first I thought the problem was due to using a modern version of the maven-compiler-plugin. I was using version 3.0 instead of 2.0.2.
起初我认为问题是由于使用了现代版本的 maven-compiler-plugin。我使用的是 3.0 版而不是 2.0.2 版。
The solution has been: - Remove the maven-compiler-plugin - Add this setting: incremental
解决方案是: - 删除 maven-compiler-plugin - 添加此设置:增量
Finally my pom.xml is this:
最后我的 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>
Problem 2
问题二
Another problem I was experiencing is this:
我遇到的另一个问题是:
<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>
This make this solved problem arise: https://issues.scala-lang.org/browse/SI-5733
这使得这个解决的问题出现了:https: //issues.scala-lang.org/browse/SI-5733
Probably it was using an old version of the Scala compiler.
可能它使用的是旧版本的 Scala 编译器。
The solution has been to rename scala.version to scala.major, as this property has a special meaning.
解决方案是将 scala.version 重命名为 scala.major,因为该属性具有特殊含义。
回答by drsquidop
In order to get the scala-maven-plugin to respect Java 1.8, but still allow mixed compilation of java and scala, we use this:
为了让 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>
...
回答by Colin Wang
I add blow code at pom.xml
file <build>
block and working good!
我在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>