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.设置之好的项目结构如图所示:

IDEA Java/Scala混合项目Maven打包

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效果如下:

IDEA Java/Scala混合项目Maven打包

相关推荐