IDEA Java/Scala混合專案maven打包

IDEA Java/Scala混合專案maven打包

1.在建立Maven專案時,選擇骨架為maven-archetype-quickstart

2.pom檔案內容為:

<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>scalalearn</groupId>
<artifactId>scalalearn</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>scalalearn</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.11.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<version>2.15.2</version>
<executions>
<execution>
<id>scala-compile-first</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<includes>
<include>**/*.scala</include>
</includes>
</configuration>
</execution>
<execution>
<id>scala-test-compile</id>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

3.在src/main下新建scala資料夾,用於存放scala原始碼,並在project structure中將其設為source目錄;在src/main下建立resources/META-INF目錄,並建立MANIFEST.MF檔案,在裡面填入mainclass,並將resources目錄設定為資源目錄

Main-Class: com.scalalearn.java.main.App

4.設定之好的專案結構如圖所示:

5.com.scalalearn.scala.main.ScalaPrint.scala的內容為:

package com.scalalearn.scala.main
/**
* Created by Administrator on 2016/5/14.
*/
object ScalaPrint {
def print():Unit={
println("I love scala");
}
}

com.scalalearn.java.main.App.java的內容為:

package com.scalalearn.java.main;
import com.scalalearn.scala.main.ScalaPrint;
/**
* Hello world!
*
*/
public class App {
public static void main( String[] args ) {
ScalaPrint.print();
}
}

6.打jar包的mvn命令為:mvn clean scala:compile compile package

7.最後會在target目錄下生成兩個jar包,分別為:scalalearn-1.0-SNAPSHOT.jar和scalalearn-1.0-SNAPSHOT-jar-with-dependencies.jar

8.執行jar效果如下: