Mixing Tycho, Scala and Java

I recently found myself writing a Java/Scala mixed project for the Eclipse RCP plattform. Of course, I wanted to have a headless build for my project and the only way to do that (when we are talking about Eclipse Plugin Development) is using maven. In this post, I describe how to setup mixed Scala/Java project using Tycho.

Used software

I'm using the following setup:

Project directory structure:

project/
   |---build.properties
   |---pom.xml
   |---META-INF/
       |---MANIFEST.MF   
   |---src
       |---main
           |---java
           |---scala

Maven/Tycho configuration

The main thing to take into account is that tycho adds the directories defined in the file build.properties to the build path. Thus our pom.xml needs to add the xml bellow. 

  <properties>
    <tycho.version>0.20.0</tycho.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <luna-repo.url>http://download.eclipse.org/releases/luna</luna-repo.url>
    <scala-repo.url>http://download.scala-ide.org/sdk/lithium/e44/scala211/stable/site</scal...
    <scala.version>2.11.5</scala.version>
    <maven-scala-plugin.version>3.2.0</maven-scala-plugin.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.scala-lang</groupId>
      <artifactId>scala-library</artifactId>
      <version>${scala.version}</version>
    </dependency>
  </dependencies>
  <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>net.alchim31.maven</groupId>
                    <artifactId>scala-maven-plugin</artifactId>
                    <version>${maven-scala-plugin.version}</version>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>${maven-scala-plugin.version}</version>
                <executions>
                    <execution>
                        <id>scala-compile-first</id>
                        <phase>process-resources</phase>
                        <goals>
                            <!-- we omit the add-source goal that is described in the maven-scala-plugin documentation -->
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>scala-test-compile</id>
                        <phase>process-test-resources</phase>
                        <goals>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
           <recompileMode>incremental</recompileMode>
                    <scalaVersion>${scala.version}</scalaVersion>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.eclipse.tycho</groupId>
                <artifactId>tycho-maven-plugin</artifactId>
                <version>${tycho.version}</version>
                <extensions>true</extensions>
                <configuration>
                    <excludeResources>
                        <!-- we exclude scala files so that they don't end up in the jar -->
                        <excludeResource>**/*.scala</excludeResource>
                    </excludeResources>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.eclipse.tycho</groupId>
                <artifactId>target-platform-configuration</artifactId>
                <version>${tycho.version}</version>
                <configuration>
                    <environments>
                        <environment>
                            <os>linux</os>
                            <ws>gtk</ws>
                            <arch>x86</arch>
                        </environment>
                        <environment>
                            <os>linux</os>
                            <ws>gtk</ws>
                            <arch>x86_64</arch>
                        </environment>
                        <environment>
                            <os>win32</os>
                            <ws>win32</ws>
                            <arch>x86</arch>
                        </environment>
                        <environment>
                            <os>win32</os>
                            <ws>win32</ws>
                            <arch>x86_64</arch>
                        </environment>
                        <environment>
                            <os>macosx</os>
                            <ws>cocoa</ws>
                            <arch>x86_64</arch>
                        </environment>
                    </environments>
                </configuration>
            </plugin>
        </plugins>
    </build>

Notice that at line 36 we omit the add-source goal from the configuration of the maven-scala-plugin (necessary for mixed projects that don't use Tycho). Thus, our build.properties file needs to declare the directories where we have our scala source files.

source.. = src/main/java/,\
           src/main/scala/
output.. = bin/
bin.includes = META-INF/,\
               .

 

tweet: