How To Build a Scala 2.8/Java Application with Maven

Have been wondering how to build your application that have both Java and Scala sources with Maven?

Here you can find a tutorial from http://mackaz.de:

Build a mixed Scala 2.8/Java application and Eclipse Settings for this project

Comments

<properties>
        <scala.version>2.7.7</scala.version>
        <maven.scala.plugin>2.13.1</maven.scala.plugin>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>${scala.version}</version>
        </dependency>

    </dependencies>

    <build>
        <outputDirectory>${project.basedir}/target/classes</outputDirectory>
        <directory>${project.basedir}/target</directory>
        <plugins>

            <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-clean-plugin</artifactId>
                 <version>2.2</version>
                 <configuration>
                    <verbose>true</verbose>
                     <filesets>
                         <fileset>
                             <directory>${project.basedir}</directory>
                             <includes>
                                 <include>target.timestamp</include>
                                 <include>.scala_dependencies</include>
                             </includes>
                         </fileset>
                     </filesets>
                 </configuration>
             </plugin>    

              <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.0.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
                <executions>
                    <execution>
                        <id>java-compile-first</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.scala-tools</groupId>
                <artifactId>maven-scala-plugin</artifactId>
                <version>${maven.scala.plugin}</version>
                <executions>
                     <execution>
                         <id>scala-compile-second</id>
                         <phase>compile</phase>
                         <goals>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                         </goals>
                     </execution>
                </executions>
                 <configuration>
                         <displayCmd>true</displayCmd>
                         <checkMultipleScalaVersions>true</checkMultipleScalaVersions>
                         <failOnMultipleScalaVersions>true</failOnMultipleScalaVersions>
                         <args>
                                 <arg>-target:jvm-1.5</arg>
                         </args>
                 </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.8</version>
                <configuration>
                    <downloadSources>true</downloadSources>
                    <sourceIncludes>
                        <sourceInclude>**/*.scala</sourceInclude>
                    </sourceIncludes>
                    <buildcommands>
                        <buildcommand>ch.epfl.lamp.sdt.core.scalabuilder</buildcommand>
                    </buildcommands>
                    <additionalProjectnatures>                    
                        <projectnature>ch.epfl.lamp.sdt.core.scalanature</projectnature>
                    </additionalProjectnatures>
                    <classpathContainers>
                        <classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>
                        <classpathContainer>ch.epfl.lamp.sdt.launching.SCALA_CONTAINER</classpathContainer>
                    </classpathContainers>
                </configuration>
            </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>${project.basedir}/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>${project.basedir}/src/test/scala</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

nice links, I thought sharing with you my pom.xml for java/scala
hope you'll find it usefull
this example is good for scala using java and not the other way around
by the way 2.8.1 is still a beta.

 

I've found that if your 'target' directory is different from its default name, e.g. 'build', then line 29 in the above configuration should be

<include>build.timestamp</include>