vlambda博客
学习文章列表

IDEA下基于Maven的Java和Scala混合编程

开发Spark项目时,有时候需要用到Java和Scala进行混合编程,比如Java类中调用Scala,或者Scala中调用Java

1. 创建Java Maven工程

首先,创建一个空的Java Maven工程java-scala-program

src/main/java目录下新建JavaTest类,输出"Hello Java!"

IDEA下基于Maven的Java和Scala混合编程

2. 将Scala混合到Java Meavn项目中

为了能够在Java Maven工程中使用Scala,我们需要使用一个Maven插件:scala-maven-plugin,把下面的代码加入到pom.xml文件中

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

因为我们需要使用Scala代码,所以我们还需要加入scala-library依赖,如下:

<dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-library</artifactId> <version>2.11.8</version></dependency>

src/main目录下,新建scala目录,并右键-->Make Directory as --> Source Root, 并新建 ScalaTest类:

IDEA下基于Maven的Java和Scala混合编程

3. 在Java中调用Scala

IDEA下基于Maven的Java和Scala混合编程
mark

4. 在Scala中调用Java

IDEA下基于Maven的Java和Scala混合编程

5. 添加maven打包工具进行打包

这里我们使用 maven plugin:maven-assembly-plugin对工程进行打包,在pom文件添加如下:

<plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>JavaTest</mainClass> </manifest> </archive> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions></plugin>

mainClass: 是主函数所在的类,如果运行Java,则为JavaTest, 如果运行Scala,则为ScalaTest

打包Java程序:

IDEA下基于Maven的Java和Scala混合编程

打包Scala程序:

6. 完整代码

JavaTest.java

public class JavaTest { public static void main(String[] args) { System.out.println("Hello Java!"); ScalaTest.output(); }
public void output(){ System.out.println("JavaTest"); }}

ScalaTest.scala

object ScalaTest { def main(args: Array[String]): Unit = { println("Hello Scala!") val javaTest = new JavaTest() javaTest.output() }
def output(): Unit = { println("Scala Test") }}

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>cn.unicom</groupId> <artifactId>java-scala-program</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source.version>1.8</maven.compiler.source.version> <maven.compiler.target.version>1.8</maven.compiler.target.version> <encoding>UTF-8</encoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> <scala.version>2.11.8</scala.version> </properties>

<dependencies> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-library</artifactId> <version>${scala.version}</version> </dependency> </dependencies>
<build> <plugins> <!-- This plugin compiles Scala files --> <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> <configuration> <scalaVersion>${scala.version}</scalaVersion> </configuration> </plugin> <!-- This plugin compiles Java files --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>${java.version}</source> <target>${java.version}</target> </configuration> <executions> <execution> <phase>compile</phase> <goals> <goal>compile</goal> </goals> </execution> </executions> </plugin> <!-- This plugin adds all dependencies to JAR file during 'package' command. Pay EXTRA attention to the 'mainClass' tag. You have to set name of class with entry point to program ('main' method) --> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>JavaTest</mainClass> </manifest> </archive> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
</project>

注意:Scala plugin一定要位于Java plugin前面,即先编译Scala在编译Java;

否则,会出现找不到Scala类的情况