用 Maven 构建一个 Scala 应用程序(混合了 Java 源代码)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/336144/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 13:36:20  来源:igfitidea点击:

Building a scala app with maven (that has java source mixed in)

javamaven-2scala

提问by Michael Neale

I have an application where I would like to have mixed Java and Scala source (actually its migrating a java app to scala - but a bit at a time).

我有一个应用程序,我想在其中混合 Java 和 Scala 源(实际上是将 Java 应用程序迁移到 Scala - 但一次一点)。

I can make this work in IDEs just fine, very nice. But I am not sure how to do this with maven - scalac can compile java and scala intertwined, but how to I set up maven for the module?

我可以在 IDE 中很好地完成这项工作,非常好。但我不知道如何用 maven 做到这一点——scalac 可以编译 java 和 Scala 交织在一起,但是我如何为模块设置 maven?

Also, does my scala source have to be a different folder to the java?

另外,我的 Scala 源是否必须是与 java 不同的文件夹?

采纳答案by Michael Neale

Using the maven scala plugin, a config like the one below will work for a project that mixes java and scala source (scala source of course goes in the /scala directory, as mentioned by someone else).

使用 maven scala 插件,像下面这样的配置将适用于混合 java 和 scala 源的项目(scala 源当然位于 /scala 目录中,正如其他人提到的那样)。

You can run run mvn compile, test etc... and it will all work as normal. Very nice (it will run scalac first automatically).

您可以运行 run mvn compile、test 等……一切都会照常进行。非常好(它会首先自动运行 scalac)。

For a great IDE, IntelliJ 8 works nicely: add in the scala plug in, then add a scala facet, and then adjust the compile setting for scala to run scalac first (critical if you have circular dependencies with scala and java source).

对于出色的 IDE,IntelliJ 8 运行良好:添加 scala 插件,然后添加 scala facet,然后调整 scala 的编译设置以首先运行 scalac(如果您有 scala 和 java 源的循环依赖关系,则至关重要)。

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>demo</groupId>
<artifactId>scala-java-app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>scala-java-app</name>
<repositories>
    <repository>
        <id>scala-tools.org</id>
        <name>Scala-tools Maven2 Repository</name>
        <url>http://scala-tools.org/repo-releases</url>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>scala-tools.org</id>
        <name>Scala-tools Maven2 Repository</name>
        <url>http://scala-tools.org/repo-releases</url>
    </pluginRepository>
</pluginRepositories>
<build>
    <plugins>
        <plugin>
            <groupId>org.scala-tools</groupId>
            <artifactId>maven-scala-plugin</artifactId>
            <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>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.5</source>
                <target>1.5</target>
            </configuration>
        </plugin>
    </plugins>  
</build>
<dependencies>
    <dependency>
        <groupId>org.scala-lang</groupId>
        <artifactId>scala-library</artifactId>
        <version>2.7.2</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>

回答by sblundy

Yeah, the scala part has to be in a separate module and in src/main/scaladirectory. Maven regards mixed source like this as heresy.

是的,Scala 部分必须在单独的模块和src/main/scala目录中。Maven 将这样的混合源视为异端。

You enable scala compilation by importing the scala maven plugin. The "usage"page as a good example.

您可以通过导入scala maven 插件来启用 Scala 编译。在“用法”页作为一个很好的例子。

回答by lindelof

I once asked a very similar questionabout how to include non-Java code in a Maven project. The gist of the answer was to have under src a different directory for each programming language, and to find/write a Maven plugin that would know what to do with each. Eg:

我曾经问过一个关于如何在 Maven 项目中包含非 Java 代码的非常相似的问题。答案的要点是在 src 下为每种编程语言设置一个不同的目录,并找到/编写一个知道如何处理每种语言的 Maven 插件。例如:

src/
|-- main
|   |-- bin
|   |-- sh
|   |-- sql
|   |-- scala
|   |-- java
|   `-- resources
|-- site
...

回答by Guemundur Bjarni

I solved this some time ago by having one Maven module written in Scala and the other in Java. But since Scala and Java can cross depend on one another (Java -> Scala -> Java or the other way around), then this is something very desirable without multi module projects.

前段时间我通过使用 Scala 编写一个 Maven 模块而使用 Java 编写另一个模块解决了这个问题。但是由于 Scala 和 Java 可以相互依赖(Java -> Scala -> Java 或相反的方式),因此在没有多模块项目的情况下这是非常可取的。

There is work underway in solving this, you can read about it hereand a new version of the maven-scala-plugin will be released soon.

正在解决这个问题,您可以在此处阅读相关信息,并且即将发布新版本的 maven-scala-plugin。

回答by cetnar

Look at Sonatype Maven Cookbook Chapter 3. Scala and Maven

查看 Sonatype Maven Cookbook 第 3 章。Scala 和 Maven

回答by Julian Dehne

You need to combine several approaches in order to mix scala and java classes freely. This solution worked for me:

您需要结合多种方法才能自由地混合使用 scala 和 java 类。这个解决方案对我有用:

   <properties>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <!-- <encoding>UTF-8</encoding> -->
        <scala.tools.version>2.10</scala.tools.version>
        <scala.version>2.10.3</scala.version>
    </properties>
<build>
        <sourceDirectory>src/main/scala</sourceDirectory>
        <testSourceDirectory>src/test/scala</testSourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.scala-tools</groupId>
                <artifactId>maven-scala-plugin</artifactId>
                <version>2.15.2</version>
                <executions>                    
                    <execution>
                        <id>scala-compile-first</id>
                        <phase>process-resources</phase>
                        <goals>
                            <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>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>3.1.3</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                        <configuration>
                            <args>
                                <arg>-make:transitive</arg>
                                <arg>-dependencyfile</arg>
                                <arg>${project.build.directory}/.scala_dependencies</arg>
                            </args>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.13</version>
                <configuration>
                    <useFile>false</useFile>
                    <disableXmlReport>true</disableXmlReport>               
                    <includes>
                        <include>**/*Test.*</include>
                        <include>**/*Suite.*</include>
                    </includes>
                </configuration>
            </plugin>       
        </plugins>
    </build>

回答by Mebin

Here's a small project link(https://github.com/mebinjacob/mongo-scala-java-maven-poc) that I did using scala, java and mongo db.

这是我使用 scala、java 和 mongo db 所做的一个小项目链接(https://github.com/mebinjacob/mongo-scala-java-maven-poc)。

Note in the pom.xml of the project one can specify the source folder of scala files it can be same as that of java or a different folder like src/main/scala. There is no mandate for scala classes to be on a separate src folders. I have tried both of them and both of them work great. The snippet of the pom that I am referring to is

注意在项目的 pom.xml 中可以指定 scala 文件的源文件夹,它可以与 java 的源文件夹相同,也可以是不同的文件夹,如 src/main/scala。没有要求 Scala 类位于单独的 src 文件夹中。我已经尝试了它们,它们都很好用。我所指的 pom 片段是

    <build>
    <plugins>
        <plugin>
            <groupId>org.scala-tools</groupId>
            <artifactId>maven-scala-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>testCompile</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <sourceDir>src/main/scala</sourceDir>
                <jvmArgs>
                    <jvmArg>-Xms64m</jvmArg>
                    <jvmArg>-Xmx1024m</jvmArg>
                </jvmArgs>
            </configuration>
        </plugin>
    </plugins>
</build>