views:

2569

answers:

6

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?

+5  A: 

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.

sblundy
Can the scala code call java source in the same module and vice versa? When I am in intelliJ - it shows it all together anyway, so its not a huge deal.
Michael Neale
Oh and will this hook in to mvn compile etc? (I can see how scala:compile works).
Michael Neale
yes this works quite well.
Michael Neale
A: 

Maven is notorious for being difficult when attempting to handle nonobvious cases. I suggest that you follow the example of the rest of the industry and abandon Maven.

Ant is an obvious alternate choice if you are considering/using Maven, and add Ivy to get some of the goodness from Maven without the pain.

Beyond that, there are several other choices. Maven is problematic because it does too much magic without explaining itself or allowing you much easy control. It is overly complicated both in concept and in implementation. Therefore, seek an alternative that is simple, straightforward, tweakable, extendable, and proven.

Best wishes.

Rob Williams
that's not really an answer. Sorry.
Michael Neale
well don't bother posting - wasting your and others times. Move on.
Michael Neale
Some people actually want to know when their problems are caused by bad assumptions or bad tools. I have been through similar pain that prompted such a question--I wish that someone would have shared their experience and saved me all the trouble with Maven.
Rob Williams
Many people use maven for what ever reason, yes its a hassle but for some it works quite well. I think its stupid to assume someone can just swap out the build tool on the whim of a random posting. Don't both answering posts like that.
Michael Neale
Naw, I don't buy it. You are not the only audience, and most people actually want to know about consequences and alternatives. If the alternatives are outside their control, so be it, but being informed is never a bad thing (especially regarding a huge investment like with Maven).
Rob Williams
"I suggest that you follow the example of the rest of the industry and abandon Maven." - got ANY evidence of that?
Michael Neale
Now THAT is a useful comment: take a look at http://stackoverflow.com/questions/303853/what-are-your-impressions-of-maven for starters.
Rob Williams
We just switched to Maven from Ant/Ivy hell and I never felt better. About 40 various configuration and build files are gone in the puff
DroidIn.net
+2  A: 

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
...
lindelof
+2  A: 

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.

Guðmundur Bjarni
I have had some success with http://scala-tools.org/mvnsites/maven-scala-plugin/ for the moment - not sure if that is what you were referring to or not.
Michael Neale
+5  A: 

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"&gt;
<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&lt;/url&gt;
 </repository>
</repositories>
<pluginRepositories>
 <pluginRepository>
  <id>scala-tools.org</id>
  <name>Scala-tools Maven2 Repository</name>
  <url>http://scala-tools.org/repo-releases&lt;/url&gt;
 </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>

Michael Neale
Thanks, this worked really well!
James Kingsbery
+3  A: 

Look at Sonatype Maven Cookbook Chapter 3. Scala and Maven

cetnar