使用IDEA进行scala/java混合工程的构建

来源:互联网 发布:屏风专卖店淘宝网 编辑:程序博客网 时间:2024/06/09 06:39

构建scala/java混合工程,大致有2种方式:


方式一、

File ----》New Project ----》勾选Maven ----》 再勾选Create from archetype ----》选择 "org.scala-tools.archetypes:scala-archetype-simple"


这种方式构建混合工程较为简单,但是由于插件更新不及时,安装时的scala版本及jdk版本可能较小,需要与目标项目的版本做相应的适配。

如将默认scala2.7.0版本替换为2.10版本,将jvm-1.5版本替换为jvm-1.7。


方式二、

①、本机安装scala sdk

去scala官网 http://www.scala-lang.org/download 下载与操作系统对应的scala版本,安装。


②、Idea上Plugins搜索下载Scala插件,也可以下载离线安装包(点击打开链接),本地以disk方式安装。

务必注意在下载时,留意scala插件库中版本号与本机IEDA的版本对应关系。


③、与maven整合

编辑pom.xml,设置为如下内容:

<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/maven-v4_0_0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>com.balabala.mixed</groupId>  <artifactId>mixed-scala-java-test</artifactId>  <version>1.0-SNAPSHOT</version>  <properties>    <scala.version>2.10.0</scala.version>  </properties>  <repositories>    <repository>      <id>scala-tools.org</id>      <name>Scala-Tools Maven2 Repository</name>      <url>http://scala-tools.org/repo-releases</url>    </repository>  </repositories>  <pluginRepositories>    <pluginRepository>      <id>scala-tools.org</id>      <name>Scala-Tools Maven2 Repository</name>      <url>http://scala-tools.org/repo-releases</url>    </pluginRepository>  </pluginRepositories>  <dependencies>    <dependency>      <groupId>org.scala-lang</groupId>      <artifactId>scala-library</artifactId>      <version>${scala.version}</version>    </dependency>    <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>4.4</version>      <scope>test</scope>    </dependency>    <dependency>      <groupId>org.specs</groupId>      <artifactId>specs</artifactId>      <version>1.2.5</version>      <scope>test</scope>    </dependency>  </dependencies>  <build>    <sourceDirectory>src/main/scala</sourceDirectory>    <testSourceDirectory>src/test/scala</testSourceDirectory>    <plugins>      <plugin>        <groupId>org.scala-tools</groupId>        <artifactId>maven-scala-plugin</artifactId>        <version>2.15.2</version>        <executions>          <execution>            <goals>              <goal>compile</goal>              <goal>testCompile</goal>            </goals>          </execution>        </executions>        <configuration>          <scalaVersion>${scala.version}</scalaVersion>          <args>            <arg>-target:jvm-1.8</arg>          </args>        </configuration>      </plugin>      <plugin>        <groupId>org.apache.maven.plugins</groupId>        <artifactId>maven-eclipse-plugin</artifactId>        <configuration>          <downloadSources>true</downloadSources>          <buildcommands>            <buildcommand>ch.epfl.lamp.sdt.core.scalabuilder</buildcommand>          </buildcommands>          <additionalProjectnatures>            <projectnature>ch.epfl.lamp.sdt.core.scalanature</projectnature>          </additionalProjectnatures>          <classpathContainers>            <classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>            <classpathContainer>ch.epfl.lamp.sdt.launching.SCALA_CONTAINER</classpathContainer>          </classpathContainers>        </configuration>      </plugin>    </plugins>  </build>  <reporting>    <plugins>      <plugin>        <groupId>org.scala-tools</groupId>        <artifactId>maven-scala-plugin</artifactId>        <configuration>          <scalaVersion>${scala.version}</scalaVersion>        </configuration>      </plugin>    </plugins>  </reporting></project>

④、maven编译打包

直接使用mvn clean package会报错,因为默认只会打包java文件,需要使用如下命令替换之:

mvn clean scala:compile compile package


命令解释:

如③中的 maven-scala-plugin 配置,是其提供的对scala编译打包的插件命令,将先编译scala源码,再编译java源码,最后做package操作。


原创粉丝点击