retiman
retiman

Reputation: 33

Possible for IDE's to resolve Scala classes in Java using Maven?

I have a project with mixed Java and Scala sources, following the instructions on this page, which works when running Maven from the command line.

However, people using IDEs like IDEA and Netbeans have problems resolving Scala classes in Java code (but not the other way around, thanks to the nice plugins available). Is there a way to resolve them?

Note: I can build from the command line just fine; the Scala classes are compiled before the Java classes. I just want the IDE to recognize this as well. I could create a separate module for the Scala classes to resolve this problem, but it seems like overkill to me.

Note: In IDEA, I have "Compile Scala classes first" and that still does not do the trick.

Update: Here are the versions I'm using: scala-library 2.8.0 maven-scala-plugin 2.12 IDEA 9.0 Ultimate with latest scala plugin from plugin repos Netbeans 6.9 with scala nightly plugin

Upvotes: 3

Views: 755

Answers (2)

Edson
Edson

Reputation: 61

I have being trying to figure out how to work with Eclipse Indigo + Scala IDE 2.9, m2eclipse, mix of scala 2.9 + jdk1.7 without luck.

I found out that using maven eclipse plugin (mvn eclipse:eclipse) and importing the project as eclipse project (not maven project) with the below customization cleaned up the error marks.

<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.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>

    <executions>
        <execution>
            <id>add-source</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>add-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>src/main/scala</source>
                </sources>
            </configuration>
        </execution>
        <execution>
            <id>add-test-source</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>add-test-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>src/test/scala</source>
                </sources>
            </configuration>
        </execution>
    </executions    
</plugin>

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-eclipse-plugin</artifactId>
    <version>2.8</version>

    <configuration>
        <downloadSources>true</downloadSources>
        <downloadJavadocs>true</downloadJavadocs>
        <projectnatures>
            <projectnature>org.scala-ide.sdt.core.scalanature</projectnature>
            <projectnature>org.eclipse.jdt.core.javanature</projectnature>
        </projectnatures>
        <buildcommands>
            <buildcommand>org.scala-ide.sdt.core.scalabuilder</buildcommand>
        </buildcommands>
        <classpathContainers>
            <classpathContainer>org.scala-ide.sdt.launching.SCALA_CONTAINER"</classpathContainer>
            <classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>
        </classpathContainers>
        <excludes>
            <exclude>org.scala-lang:scala-library</exclude>
            <exclude>org.scala-lang:scala-compiler</exclude>
        </excludes>
        <sourceIncludes>
            <sourceInclude>**/*.scala</sourceInclude>
            <sourceInclude>**/*.java</sourceInclude>
        </sourceIncludes>
    </configuration>
</plugin>

Upvotes: 1

Aaron Novstrup
Aaron Novstrup

Reputation: 21017

Which versions (of Scala, the IDEs, the Scala plugins) are you using?

I had the same issues when I started using Scala 2.7 around 9 months ago. While I haven't tried a mixed project recently, my understanding was that the issues would be resolved in Scala 2.8. It may be worth trying Eclipse 3.5.2 with Scala 2.8 -- my impression is that the Eclipse plugin is keeping up with changes in 2.8 better than the other IDE plugins (but I could be wrong).

Upvotes: 1

Related Questions