Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up IDEA to auto-configure Scala facet for mixed Scala/Java Maven project?

How do I get IntelliJ to auto-configure the Scala facet in a Maven project with mixed Scala and Java source code?

I am using Scala Plugin Nightly for Maia Build 2099.

mvn compile and mvn test both work from the command prompt and from the Maven Projects panel in IntelliJ. However, if I try to run ScalaSpec directly in IntelliJ it presents an error dialog Cannot compile Scala files with content Please, specify compiler in Scala facet.

Project directory structure:

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

Listing of HelloJava.java:

public class HelloJava {}

Listing of HelloScala.scala:

class HelloScala

Listing of TestJava.java:

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

Listing of ScalaSpec.scala:

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

Listing of 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>
like image 835
Alain O'Dea Avatar asked Sep 02 '10 14:09

Alain O'Dea


People also ask

How do I add Scala nature to maven project in Eclipse?

To get this project in Eclipse, choose File -> Import -> Existing Maven Project and navigate to the directory containing your maven project. When you click Finish, the project is imported and it should have both Maven and Scala natures.

Can I use maven with Scala?

Maven is one of the most common build tools in the JVM ecosystem and it also works with Scala through the scala-maven-plugin.

What is the difference between SBT and maven?

Once you familiarize yourself with how one Maven project builds you automatically know how all Maven projects build saving you immense amounts of time when trying to navigate many projects. On the other hand, SBT is detailed as "An open-source build tool for Scala and Java projects".


1 Answers

Add a <configuration/> node to the <plugin/> node for maven-scala-plugin right after the <executions/> node

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

Now the Scala Plugin auto-detects the Scala compiler and auto-configures the Scala Facet. Debugging, running, make and the like all work directly through the IntelliJ interface now.

It works up to scala-maven-plugin 3.2.0.

Please note that 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.

like image 161
Alain O'Dea Avatar answered Oct 29 '22 03:10

Alain O'Dea