score:40
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>
score:0
You need to combine several approaches in order to mix scala and java classes freely. 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>
score:1
Here's a small project link(https://github.com/mebinjacob/mongo-scala-java-maven-poc) that I did using scala, java and mongo db.
Note in the pom.xml of the project one can specify the source folder of scala files it can be same as that of java or a different folder like src/main/scala. There is no mandate for scala classes to be on a separate src folders. I have tried both of them and both of them work great. The snippet of the pom that I am referring to is
<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>
score:2
I solved this some time ago by having one Maven module written in Scala and the other in Java. But since Scala and Java can cross depend on one another (Java -> Scala -> Java or the other way around), then this is something very desirable without multi module projects.
There is work underway in solving this, you can read about it here and a new version of the maven-scala-plugin will be released soon.
score:4
Look at Sonatype Maven Cookbook Chapter 3. Scala and Maven
score:8
I once asked a very similar question about how to include non-Java code in a Maven project. The gist of the answer was to have under src a different directory for each programming language, and to find/write a Maven plugin that would know what to do with each. Eg:
src/
|-- main
| |-- bin
| |-- sh
| |-- sql
| |-- scala
| |-- java
| `-- resources
|-- site
...
score:12
Yeah, the scala part has to be in a separate module and in src/main/scala
directory. Maven regards mixed source like this as heresy.
You enable scala compilation by importing the scala maven plugin. The "usage" page as a good example.
Source: stackoverflow.com
Related Query
- Building a scala app with maven (that has java source mixed in)
- Building a project with mixed Scala and Java source files using Ant - illegal cyclic reference error
- Can I use Java reflection to get the value of a member that has been added with a Scala macro annotation?
- Run scala app with maven dependencies plus some java code
- Can I compile a Scala project with mixed java and scala code with dependencies both ways in Maven?
- Parse Java source with Scala
- Is it recommended to separate Scala and Java source files for a Maven project?
- Scala class with field that is marked protected in java
- Integrating Scala app with NewRelic Java Agent
- transitive dependencies of scala project built with sbt within a different java maven project
- maven project which has both java and scala
- Working with collections of mixed complex Java generics in Scala 2.10
- Open source projects with Java and Scala interaction
- Unable to compile spring boot project with java and scala using maven
- With scala play, how to convert a class that has an ObjectId to Json?
- What is the best way to run a Java program that also has Scala code?
- Scala gradle console app with java libararies
- Debug Gatling Scala Code in Intellij with Maven Java Project
- Spark-Cassandra Maven project with java source making scala-lib calls
- Ensure that a scala parent class has a formatter associated with it
- When building Scala(2.7.0) maven project getting error "error while loading Consumer" with Java 8 in IntelliJ
- how match mixed type of scala higher-kinder types feature with java generic type?
- Scala How do you sort a map that has a String, with numbers in it
- Compile test source files with scala fsc and maven
- How to call a Scala method that has Numeric parameter from Java
- How to create a Scala executable jar file that is built with Maven, and has log4j included, using IntelliJ IDEA IDE
- akka-http has mixed java and scala dsl definitions, preventing compilation
- How to add a scala dependency to a Java project in intelliJ that use maven
- Building android app with scala "bad class file magic"
- Porting a Maven Java project that uses JavaCC to Scala
More Query from same tag
- Scala: curried function in foreach?
- scala futures - how to get result or failure from both futures
- Reading sql file using getResources in scala
- Scala: How to get all keys from Option[Map[String, Int]]?
- What is Scala's counterpart of Discriminated Union in F#?
- Mass-add an object if it is an instance of a class
- Why does Scala reflection work inside an object but not at the top level of a script?
- adding task to sbt 13.x build.sbt
- (Un)folding a sheet of paper according to a pattern and giving the order of the layers
- Scala: How can I explicitly compare two Options?
- Deduplicate: Different File Contents Found Error for SBT and Scala
- read individual elements of a tuple from a map((tuple),(tuple)) in scala
- Control gzip encoding filter in Java
- How to add modules to Play with Scala?
- Have Scalatra exit if LifeCycle added with Context.setInitParameter throws exception
- Slick 3 Transaction
- Scala shapeless: derive type from Mapper
- JMock and Scala - unexpected invocation error
- Instantiating a List of a List in Scala
- Understanding some basics of Spark SQL
- How to use return of one gatling request into another request - Scala
- How can I create a Spark DataFrame from a nested array of struct element?
- How to rename a duplicate column using column index?
- Using Scala and Spark Apply "like" operator with multiple string (Seq or Array etc). on a DataFrame
- Scala web :: lazy val scope/lifetime
- Scala Def Macros - How do I get the parameterized type member of a symbol?
- Cannot infer contravariant Nothing type parameter
- Change Data Types for Dataframe by Schema in Scala Spark
- Converting from org.apache.spark.sql.Dataset to CoordinateMatrix
- Simple scala.swing application fails