Building a scala application with maven (with java source code)

I have an application in which I would like to have mixed Java and Scala source (in fact, its java application migration to Scala), but a little at a time).

I can make this work in the IDE just fine, very enjoyable. But I'm not sure how to do it with maven - scalac can compile java and Scala intertwined, but how to configure maven for the module?

Also, should my Scala source be another folder for java?

+60
java scala maven-2
Dec 03 '08 at 3:22
source share
7 answers

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

You can run run mvn compile, test, etc., and everything will work fine. Very nice (it automatically starts the rope).

For a great development environment, IntelliJ 8 works beautifully: add scala to the module, then add the scala facet, and then configure the compilation option for scala to run the scalas 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> 

+39
Dec 16 '08 at 3:23
source share

Yes, the scala part should be in a separate module and in the src/main/scala directory. Maven treats a mixed source like this as heresy.

You enable scala compilation by importing the scala maven plugin . "use" as a good example.

+12
Dec 03 '08 at 4:04
source share

I once asked a very similar question about how to include non-Java code in a Maven project. The essence of the answer was to have a different directory for each programming language under src and find / write a Maven plugin that would know what to do with each. For example:

 src/ |-- main | |-- bin | |-- sh | |-- sql | |-- scala | |-- java | `-- resources |-- site ... 
+7
Dec 03 '08 at 7:13
source share

Look at the Maven Sonatip Cookie Chapter 3. Scala and Maven

+4
Aug 04 '09 at 13:48
source share

I solved this some time ago, having one Maven module written in Scala and another in Java. But since Scala and Java can intersect from each other (Java β†’ Scala β†’ Java or vice versa), this is very desirable without projects with several modules.

Work is being done to solve this problem, you can read about it here and a new version of maven-scala-plugin will be released soon.

+2
Dec 03 '08 at 9:59
source share

Here's a small project link ( https://github.com/mebinjacob/mongo-scala-java-maven-poc ) that I used with scala, java and mongo db.

Note. In the pom.xml of the project, you can specify the source folder from scala files, it can be the same as that of java or another folder, such as src / main / scala. There are no credentials for scala classes in separate src folders. I tried both of them, and both of them work great. Fragment of the pump I'm talking about

  <build> <plugins> <plugin> <groupId>org.scala-tools</groupId> <artifactId>maven-scala-plugin</artifactId> <executions> <execution> <goals> <goal>compile</goal> <goal>testCompile</goal> </goals> </execution> </executions> <configuration> <sourceDir>src/main/scala</sourceDir> <jvmArgs> <jvmArg>-Xms64m</jvmArg> <jvmArg>-Xmx1024m</jvmArg> </jvmArgs> </configuration> </plugin> </plugins> </build> 
+1
May 6 '14 at 13:49
source share

You need to combine several approaches to freely mix the scala and java classes. This solution worked for me:

  <properties> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> <!-- <encoding>UTF-8</encoding> --> <scala.tools.version>2.10</scala.tools.version> <scala.version>2.10.3</scala.version> </properties> <build> <sourceDirectory>src/main/scala</sourceDirectory> <testSourceDirectory>src/test/scala</testSourceDirectory> <plugins> <plugin> <groupId>org.scala-tools</groupId> <artifactId>maven-scala-plugin</artifactId> <version>2.15.2</version> <executions> <execution> <id>scala-compile-first</id> <phase>process-resources</phase> <goals> <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>net.alchim31.maven</groupId> <artifactId>scala-maven-plugin</artifactId> <version>3.1.3</version> <executions> <execution> <goals> <goal>compile</goal> <goal>testCompile</goal> </goals> <configuration> <args> <arg>-make:transitive</arg> <arg>-dependencyfile</arg> <arg>${project.build.directory}/.scala_dependencies</arg> </args> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.13</version> <configuration> <useFile>false</useFile> <disableXmlReport>true</disableXmlReport> <includes> <include>**/*Test.*</include> <include>**/*Suite.*</include> </includes> </configuration> </plugin> </plugins> </build> 
0
Mar 21 '14 at 10:11
source share



All Articles