如何设置IDEA以自动配置Scala facet用于混合Scala / Java Maven项目?

时间:2010-09-02 14:32:43

标签: java maven-2 scala intellij-idea maven

如何让IntelliJ在具有混合Scala和Java源代码的Maven项目中自动配置Scala方面?

我正在使用Scala Plugin Nightly for Maia Build 2099。

mvn compile mvn test 都可以从命令提示符和IntelliJ中的 Maven Projects 面板进行操作。但是,如果我尝试直接在IntelliJ中运行ScalaSpec,它会显示一个错误对话框无法编译Scala文件包含内容请在Scala facet中指定编译器

项目目录结构:

MixedJavaScala
│   MixedScalaJava.iml
│   pom.xml
│
└───src
    ├───main
    │   ├───java
    │   │       HelloJava.java
    │   │
    │   └───scala
    │           HelloScala.scala
    │
    └───test
        ├───java
        │       TestJava.java
        │
        └───scala
                ScalaSpec.scala

HelloJava.java 的列表:

public class HelloJava {}

HelloScala.scala 的列表:

class HelloScala

TestJava.java 的列表:

public class TestJava
{
    @org.junit.Test public void example() {}
}

ScalaSpec.scala 的列表:

class ScalaSpec extends org.specs.Specification {
  "nothing interesting should happen" in {}
}

pom.xml 的列表:

<?xml version="1.0" encoding="UTF-8"?>

<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>org.scala-tools.maven-scala-plugin</groupId>
  <artifactId>MixedScalaJava</artifactId>
  <version>1.0</version>
  <name>Test for Java + Scala compilation</name>
  <description>Test for Java + Scala compilation</description>

  <dependencies>
    <dependency>
      <groupId>org.scala-tools.testing</groupId>
      <artifactId>specs_2.8.0</artifactId>
      <version>1.6.5</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.1</version>
    </dependency>
    <dependency>
      <groupId>org.scala-lang</groupId>
      <artifactId>scala-library</artifactId>
      <version>2.8.0</version>
    </dependency>
  </dependencies>
  <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>

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.scala-tools</groupId>
          <artifactId>maven-scala-plugin</artifactId>
          <version>2.14.1</version>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>2.3</version>
        </plugin>
      </plugins>
    </pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.scala-tools</groupId>
        <artifactId>maven-scala-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>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>

1 个答案:

答案 0 :(得分:7)

<configuration/>节点后面的maven-scala-plugin <plugin/>节点添加<executions/>节点

<configuration>
  <scalaVersion>2.8.0</scalaVersion>
</configuration>

现在,Scala插件会自动检测Scala编译器并自动配置Scala Facet。调试,运行,制作等都可以直接通过IntelliJ接口工作。

它适用于scala-maven-plugin 3.2.0。

请注意Since version 2.7 of the plugin, the scala version to use is detected from dependency to scala-library. So it suggested to not use scalaVersion configuration.