1. Java调用Scala类
整个工程的目录结构如下:
java-call-scala/
├── pom.xml
└── src
└── main
├── java
│ └── com
│ └── hank
│ └── java
│ └── main
│ └── App.java
└── scala
└── com
└── hank
└── scala
└── main
└── MyScala.scala
App.java为主程序,由Java语言实现,MyScala.scala为被调用的类,由Scala语言实现。
App.java的代码如下:
package com.hank.java.main;
import com.hank.scala.main.MyScala;
public class App {
public static void main(String[] args) {
System.out.println("Hello, I am JAVA, I will call Scala");
MyScala.print();
}
}
MyScala.scala的代码如下:
package com.hank.scala.main
object MyScala {
def print() : Unit = {
println("Hello, I am SCALA")
}
}
程序的内容很简单,要在marven中实现Java调用Scala最关键的是编写pom文件,它的内容如下:
4.0.0
com.hank.mvn
java-call-scala
1.0-SNAPSHOT
java-call-scala
http://maven.apache.org
org.scala-lang
scala-library
2.12.1
net.alchim31.maven
scala-maven-plugin
3.2.2
compile
org.apache.maven.plugins
maven-shade-plugin
2.4.1
package
shade
com.hank.java.main.App
编译运行:
# mvn clean scala:compile compile package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building java-call-scala 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ java-call-scala ---
[INFO]
[INFO] --- scala-maven-plugin:3.2.2:compile (default-cli) @ java-call-scala ---
[INFO] /home/hank/Study/Java/java-call-scala/src/main/java:-1: info: compiling
[INFO] /home/hank/Study/Java/java-call-scala/src/main/scala:-1: info: compiling
[INFO] Compiling 2 source files to /home/hank/Study/Java/java-call-scala/target/classes at 1507437522083
[INFO] prepare-compile in 0 s
[INFO] compile in 2 s
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ java-call-scala ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/hank/Study/Java/java-call-scala/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ java-call-scala ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /home/hank/Study/Java/java-call-scala/target/classes
[INFO]
[INFO] --- scala-maven-plugin:3.2.2:compile (default) @ java-call-scala ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ java-call-scala ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/hank/Study/Java/java-call-scala/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ java-call-scala ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- scala-maven-plugin:3.2.2:compile (default) @ java-call-scala ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ java-call-scala ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/hank/Study/Java/java-call-scala/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ java-call-scala ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ java-call-scala ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ java-call-scala ---
[INFO] Building jar: /home/hank/Study/Java/java-call-scala/target/java-call-scala-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-shade-plugin:2.4.1:shade (default) @ java-call-scala ---
[INFO] Including org.scala-lang:scala-library:jar:2.12.1 in the shaded jar.
[INFO] Replacing original artifact with shaded artifact.
[INFO] Replacing /home/hank/Study/Java/java-call-scala/target/java-call-scala-1.0-SNAPSHOT.jar with /home/hank/Study/Java/java-call-scala/target/java-call-scala-1.0-SNAPSHOT-shaded.jar
[INFO] Dependency-reduced POM written at: /home/hank/Study/Java/java-call-scala/dependency-reduced-pom.xml
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.016 s
[INFO] Finished at: 2017-10-08T12:38:47+08:00
[INFO] Final Memory: 18M/189M
[INFO] ------------------------------------------------------------------------
root@hank-Vostro-270s:/home/hank/Study/Java/java-call-scala# cd target/
root@hank-Vostro-270s:/home/hank/Study/Java/java-call-scala/target# java -jar java-call-scala-1.0-SNAPSHOT.jar
Hello, I am JAVA, I will call Scala
Hello, I am SCALA
这里要注意的是先编译Scala的类,然后再编译Java的类,最后在target目录下生成了jar文件,可以直接运行。
2. Scala调用Java类
整个工程的结构如下:
scala-call-java/
├── pom.xml
└── src
└── main
├── java
│ └── com
│ └── hank
│ └── java
│ └── main
│ └── MyJava.java
└── scala
└── com
└── hank
└── scala
└── main
└── App.scala
App.scala的代码如下:
package com.hank.scala.main
import com.hank.java.main.MyJava
object App {
def main(args: Array[String]) : Unit = {
println("Hello, I am Scala, I will call Java")
val myObj = new MyJava()
myObj.print()
}
}
MyJava.java的代码如下:
package com.hank.java.main;
public class MyJava {
public void print() {
System.out.println("Hello, I am Java");
}
}
pom.xml的文件内容如下:
4.0.0
com.hank.mvn
java-call-scala
1.0-SNAPSHOT
java-call-scala
http://maven.apache.org
org.scala-lang
scala-library
2.12.1
net.alchim31.maven
scala-maven-plugin
3.2.2
compile
org.apache.maven.plugins
maven-shade-plugin
2.4.1
package
shade
com.hank.scala.main.App
编译并运行:
# mvn clean compile scala:compile package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building java-call-scala 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ java-call-scala ---
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ java-call-scala ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/hank/Study/Java/scala-call-java/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ java-call-scala ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /home/hank/Study/Java/scala-call-java/target/classes
[INFO]
[INFO] --- scala-maven-plugin:3.2.2:compile (default) @ java-call-scala ---
[INFO] /home/hank/Study/Java/scala-call-java/src/main/java:-1: info: compiling
[INFO] /home/hank/Study/Java/scala-call-java/src/main/scala:-1: info: compiling
[INFO] Compiling 2 source files to /home/hank/Study/Java/scala-call-java/target/classes at 1507438404427
[INFO] prepare-compile in 0 s
[INFO] compile in 2 s
[INFO]
[INFO] --- scala-maven-plugin:3.2.2:compile (default-cli) @ java-call-scala ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ java-call-scala ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/hank/Study/Java/scala-call-java/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ java-call-scala ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- scala-maven-plugin:3.2.2:compile (default) @ java-call-scala ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ java-call-scala ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/hank/Study/Java/scala-call-java/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ java-call-scala ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ java-call-scala ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ java-call-scala ---
[INFO] Building jar: /home/hank/Study/Java/scala-call-java/target/java-call-scala-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-shade-plugin:2.4.1:shade (default) @ java-call-scala ---
[INFO] Including org.scala-lang:scala-library:jar:2.12.1 in the shaded jar.
[INFO] Replacing original artifact with shaded artifact.
[INFO] Replacing /home/hank/Study/Java/scala-call-java/target/java-call-scala-1.0-SNAPSHOT.jar with /home/hank/Study/Java/scala-call-java/target/java-call-scala-1.0-SNAPSHOT-shaded.jar
[INFO] Dependency-reduced POM written at: /home/hank/Study/Java/scala-call-java/dependency-reduced-pom.xml
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.788 s
[INFO] Finished at: 2017-10-08T12:53:28+08:00
[INFO] Final Memory: 18M/169M
[INFO] ------------------------------------------------------------------------
root@hank-Vostro-270s:/home/hank/Study/Java/scala-call-java# ls
dependency-reduced-pom.xml pom.xml src target
root@hank-Vostro-270s:/home/hank/Study/Java/scala-call-java# cd target/
root@hank-Vostro-270s:/home/hank/Study/Java/scala-call-java/target# java -jar ./java-call-scala-1.0-SNAPSHOT.jar
Hello, I am Scala, I will call Java
Hello, I am Java
这里在编译的时候要先编译Scala的类。
3. Marven的源
默认的Marven源在国外,下载插件的时候会非常慢,找了一个国内的镜像,速度非常快。
nexus-aliyun
central
Nexus aliyun
http://maven.aliyun.com/nexus/content/groups/public
将上面的内容加入到marven的配置文件中,我的机器上对应的文件是/opt/apache-maven-3.5.0/conf/settings.xml,注意要放在