【问题标题】:How to set up IDEA to auto-configure Scala facet for mixed Scala/Java Maven project?如何设置 IDEA 为混合 Scala/Java Maven 项目自动配置 Scala facet?
【发布时间】:2011-04-07 09:18:01
【问题描述】:

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

我正在为 Maia Build 2099 每晚使用 Scala 插件。

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

项目目录结构:

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>

【问题讨论】:

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


    【解决方案1】:

    maven-scala-plugin&lt;executions/&gt; 节点之后将&lt;configuration/&gt; 节点添加到&lt;plugin/&gt; 节点

    <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.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-23
      • 1970-01-01
      • 2011-12-08
      • 2012-01-11
      • 2014-08-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多