1. 程式人生 > >Maven: 建立Java/Scala混編專案

Maven: 建立Java/Scala混編專案

2018.11.02

文章目錄

前言

專案需求改變,需要開發Spark應用,而原專案是基於Java開發的,所以就需要原專案能支援Java/Scala混編。原專案採用的是Maven專案管理工具,編輯器是IDEA。

方法

本方法基於已存在的Maven專案,未建立Maven專案的可使用mvn archetype:generate建立一個新專案:

  1. 在pom.xml中新增scala-maven-plugin等外掛1
<project>
  <build
>
<pluginManagement> <plugins> <plugin> <groupId>net.alchim31.maven</groupId> <artifactId>scala-maven-plugin</artifactId> <version>3.4.4</version> </plugin> <plugin> <
groupId
>
org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.0.2</version> </plugin> </plugins> </pluginManagement> <plugins> <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> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <executions> <execution> <phase>compile</phase> <goals> <goal>compile</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
  1. 調整目錄結構:

src/main/java
src/main/scala

  1. IDEA支援Scala2, 一步步配置即可:
    IDEA配置Scala支援

  1. Scala Maven plugin ↩︎

  2. Intellij IDEA Scala Project Configuration ↩︎