Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building a scala app with maven (that has java source mixed in)

I have an application where I would like to have mixed Java and Scala source (actually its migrating a java app to scala - but a bit at a time).

I can make this work in IDEs just fine, very nice. But I am not sure how to do this with maven - scalac can compile java and scala intertwined, but how to I set up maven for the module?

Also, does my scala source have to be a different folder to the java?

like image 759
Michael Neale Avatar asked Dec 03 '08 03:12

Michael Neale


People also ask

How do you mix Java and scala?

Is it possible to mix Scala and Java code? Yes, there is the ability to mix both types of code. It is possible to create an SBT project, put Scala code in src/main/scala and java code in src/main/java in the same project and make it work.

Can you build scala with maven?

The Scala Maven Plugin Note: the plugin includes Scala from the Central Repository so there's no need to install it yourself if you're compiling with Maven.

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

Using the maven scala plugin, a config like the one below will work for a project that mixes java and scala source (scala source of course goes in the /scala directory, as mentioned by someone else).

You can run run mvn compile, test etc... and it will all work as normal. Very nice (it will run scalac first automatically).

For a great IDE, IntelliJ 8 works nicely: add in the scala plug in, then add a scala facet, and then adjust the compile setting for scala to run scalac first (critical if you have circular dependencies with scala and java source).

<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>demo</groupId> <artifactId>scala-java-app</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>scala-java-app</name> <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>     <plugins>         <plugin>             <groupId>org.scala-tools</groupId>             <artifactId>maven-scala-plugin</artifactId>             <executions>                  <execution>                     <id>compile</id>                     <goals>                         <goal>compile</goal>                     </goals>                     <phase>compile</phase>                 </execution>                 <execution>                     <id>test-compile</id>                     <goals>                         <goal>testCompile</goal>                     </goals>                     <phase>test-compile</phase>                 </execution>                 <execution>                    <phase>process-resources</phase>                    <goals>                      <goal>compile</goal>                    </goals>                 </execution>             </executions>         </plugin>         <plugin>             <artifactId>maven-compiler-plugin</artifactId>             <configuration>                 <source>1.5</source>                 <target>1.5</target>             </configuration>         </plugin>     </plugins>   </build> <dependencies>     <dependency>         <groupId>org.scala-lang</groupId>         <artifactId>scala-library</artifactId>         <version>2.7.2</version>     </dependency>     <dependency>         <groupId>junit</groupId>         <artifactId>junit</artifactId>         <version>3.8.1</version>         <scope>test</scope>     </dependency> </dependencies> 

like image 90
Michael Neale Avatar answered Oct 11 '22 04:10

Michael Neale