推荐 原创 视频 Java开发 iOS开发 前端开发 JavaScript开发 Android开发 PHP开发 数据库 开发工具 Python开发 Kotlin开发 Ruby开发 .NET开发 服务器运维 开放平台 架构师 大数据 云计算 人工智能 开发语言 其它开发
Lambda在线 > 大数据AI > IDEA下基于Maven的Java和Scala混合编程

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

大数据AI 2020-02-23

开发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类的情况



版权声明:本站内容全部来自于腾讯微信公众号,属第三方自助推荐收录。《IDEA下基于Maven的Java和Scala混合编程》的版权归原作者「大数据AI」所有,文章言论观点不代表Lambda在线的观点, Lambda在线不承担任何法律责任。如需删除可联系QQ:516101458

文章来源: 阅读原文

相关阅读

关注大数据AI微信公众号

大数据AI微信公众号:songxt1990

大数据AI

手机扫描上方二维码即可关注大数据AI微信公众号

大数据AI最新文章

精品公众号随机推荐