correct way to set-up mixed Scala/Java compilation

2,195 views
Skip to first unread message

bryan hunt

unread,
Feb 17, 2012, 10:55:07 AM2/17/12
to Maven and Scala
Hi there,

I'm trying, once and for all to get my Scala builds working correctly
with Maven. I've been copying-n-pasting from various sources for the
last year or so. But I thought it was about time I sorted out a
definitive configuration.

I've created a git project (at Bitbucket), comprising a pom project
and two jar modules.

The first module is structured in such a way that the Scala code
depends upon the Java code.

https://bitbucket.org/sentimental/idea_mvn_mixed_java_scala/src/05abab836b0a/java_then_scala/pom.xml

The second module is structured in such a way that the Java code
depends upon the Scala code.

https://bitbucket.org/sentimental/idea_mvn_mixed_java_scala/src/05abab836b0a/scala_then_java/pom.xml

It works as is, but I noticed that the Java code is being compiled
twice when run at the command line. Does anyone have any suggestion as
to why this?

The Scala console task works great. I didn't even know that feature
existed for Maven. I thought it was only something that could be done
with SBT (which I don't have the time to deal with).

Anyhow the code is public, I've just made it Public Domain, so feel
free to take it and use it as a quick start project or whatever.

Best Regards,

Bryan Hunt

David Bernard

unread,
Feb 21, 2012, 7:52:04 AM2/21/12
to maven-a...@googlegroups.com
On Fri, Feb 17, 2012 at 16:55, bryan hunt <sentimen...@gmail.com> wrote:
Hi there,

I'm trying, once and for all to get my Scala builds working correctly
with Maven. I've been copying-n-pasting from various sources for the
last year or so. But I thought it was about time I sorted out a
definitive configuration.

I've created a git project (at Bitbucket), comprising a pom project
and two jar modules.

The first module is structured in such a way that the Scala code
depends upon the Java code.

https://bitbucket.org/sentimental/idea_mvn_mixed_java_scala/src/05abab836b0a/java_then_scala/pom.xml

The second module is structured in such a way that the Java code
depends upon the Scala code.

https://bitbucket.org/sentimental/idea_mvn_mixed_java_scala/src/05abab836b0a/scala_then_java/pom.xml

It works as is, but I noticed that the Java code is being compiled
twice when run at the command line. Does anyone have any suggestion as
to why this?

How do you notice the twice compilation ?

FYI scalac will not compile java but will read *.java, so you have line like :
  /home/david/tmp/idea_mvn_mixed_java_scala/scala_then_java/src/main/java:-1: info: compiling 
but no .class will be generated.

In Fact in the case java then scala, scalac doesn't really need to read *.java if *.class are already generated.
You can try (not tested):
   <excludes>
     <exclude>*.java</exclude>
   </excludes>


The Scala console task works great. I didn't even know that feature
existed for Maven. I thought it was only something that could be done
with SBT (which I don't have the time to deal with).

scala:console, scala:cc are present before SBT exists. But not a good 'promotion'

Bryan Hunt

unread,
Feb 22, 2012, 9:07:21 AM2/22/12
to David Bernard, maven-a...@googlegroups.com
> How do you notice the twice compilation ?
>
> FYI scalac will not compile java but will read *.java, so you have line
> like :
>
> /home/david/tmp/idea_mvn_mixed_java_scala/scala_then_java/src/main/java:-1:
> info: compiling
> but no .class will be generated.
>
> In Fact in the case java then scala, scalac doesn't really need to read
> *.java if *.class are already generated

Hi David, my observation was based upon the following output.

Firstly establish what is on the src path.

print -C1 java_then_scala/**/*.(java|scala)
java_then_scala/src/main/java/Hello.java
java_then_scala/src/main/java/IncludeInJarPlease.java
java_then_scala/src/main/scala/World.scala

I observed that the maven-scala-plugin indicated it was compiling 3
source files, meanwhile the maven-compiler-plugin indicated that it was
compiling 3 files.

Here is the (trimmed) Maven output:

------------------------------------------------------------------------
Building java_then_scala 1.0-SNAPSHOT
------------------------------------------------------------------------

maven-scala-plugin:2.15.2:compile (project-resources-execution) @ java_then_scala
Checking for multiple versions of scala
includes = [**/*.scala,**/*.java,]
excludes = []
/common/idea_mvn_mixed_java_scala/java_then_scala/src/main/java:-1: info: compiling
/common/idea_mvn_mixed_java_scala/java_then_scala/src/main/scala:-1: info: compiling
Compiling 3 source files to /common/idea_mvn_mixed_java_scala/java_then_scala/target/classes at 1329840782560
prepare-compile in 0 s
compile in 13 s

--- maven-compiler-plugin:2.3.2:compile (default-compile) @ java_then_scala ---
Compiling 2 source files to /common/idea_mvn_mixed_java_scala/java_then_scala/target/classes

--- maven-scala-plugin:2.15.2:compile (compile) @ java_then_scala ---
Checking for multiple versions of scala
includes = [**/*.scala,**/*.java,]
excludes = []

Josh Suereth

unread,
Feb 22, 2012, 9:16:47 AM2/22/12
to maven-a...@googlegroups.com, David Bernard
It looks like you the scala:compile mojo is being added to two executions...  Could the default be doing this?

Josh Suereth

unread,
Feb 22, 2012, 11:48:32 AM2/22/12
to Bryan Hunt, maven-a...@googlegroups.com, David Bernard
That was more a question for David.  The goals have default execution phases, and it appears that this default is being run, as well as process-resources.

- Josh

On Wed, Feb 22, 2012 at 11:18 AM, Bryan Hunt <bryan...@sentimentmetrics.com> wrote:
On Wed, 22 Feb 2012 09:16:47 -0500, Josh Suereth <joshua....@gmail.com> wrote:
Non-text part: multipart/alternative

> It looks like you the scala:compile mojo is being added to two
> executions...  Could the default be doing this?

I'm not sure I follow, could you elaborate please?

Bryan

Bryan Hunt

unread,
Feb 22, 2012, 11:40:03 AM2/22/12
to maven-a...@googlegroups.com
On Wed, 22 Feb 2012 09:16:47 -0500, Josh Suereth <joshua....@gmail.com> wrote:
Non-text part: multipart/alternative
> It looks like you the scala:compile mojo is being added to two
> executions... Could the default be doing this?

I'm not sure I follow, could you elaborate please?

Bryan

David Bernard

unread,
Jan 27, 2013, 1:25:32 PM1/27/13
to Maven and Scala
On Sun, Jan 27, 2013 at 6:34 AM, Stephen Boesch <jav...@gmail.com> wrote:
> What is different between the "scala then java" vs the "java then scala".?
> I looked at the pom.xml and could not see the distinction.

scala then java : need to configure the phase of scala-compilation
before "compile" phase
In fact, with maven (3) the goal compile of scala-maven-plugin run at
"compile" phase (it's default phase) in every case. It seems to be a
"feature" of maven 3. I was not aware of.
By example if you want to run surfire at a other phase than its
default one, you should "skip" it by default.
I'll add a skip option. to allow : scala-then-java != scala-then-java-then-scala

> btw i also see java compiled twice even in the new scala-maven-plugin.

Can you provide the log and the pom.xml ?


/davidB

Stephen Boesch

unread,
Jan 27, 2013, 1:42:15 PM1/27/13
to maven-a...@googlegroups.com
My issue appears to be with my gradual discovery of how maven works and not with the scala-maven-plugin.  I did not know that maven will automagically decide to do a javac compilation even when no compilation phase information or plugin are included in the pom.    Unfortunately (from a maven novice standpoint at least) maven also runs that automagic javac even when one does have an explicit compile phase.  This results in rampant confusion.  like as in "why did my updates to the pom.xml not affect the actual execution?"  And the answer being "well maven did its own thing first and if that were successful (it's not..) it may well decide to do the compilation you actually were looking forward to seeing".

Referring back to the files you posted:  I did a diff on the two files, and they  have differences only in whitespace and in the maven-scala-plugin is explicitly called out 2.15.2 in the java-then-scala  and the  "groupId" is missing from org.apache.maven.plugins in the java-then-scala.   So it appears any differences between the two are due to haphazard aspects.  Pls do take a look (run diff for yourself) and maybe comment why you ended up with different behavior between the two.




2013/1/27 David Bernard <david.be...@gmail.com>

--




David Bernard

unread,
Jan 27, 2013, 2:47:59 PM1/27/13
to Maven and Scala

Can you provide the link of the two files ?

> --
>
>
>

Stephen Boesch

unread,
Jan 27, 2013, 3:24:32 PM1/27/13
to maven-a...@googlegroups.com

David Bernard

unread,
Jan 27, 2013, 3:36:52 PM1/27/13
to Maven and Scala

Xiaofeng Lin

unread,
Aug 23, 2013, 7:41:04 PM8/23/13
to maven-a...@googlegroups.com
I wrote up some notes on how I set up a mixed Java/Scala maven project in my blog,
http://xflin.blogspot.com/2013/08/mixed-scala-and-java-in-maven-project.html

Hope it'll help.
--
Xiaofeng

Stephen Boesch

unread,
Aug 24, 2013, 10:24:13 AM8/24/13
to maven-a...@googlegroups.com
Hi Xiafeng,
  Thx v much for sharing this - will try it out in a few weeks (other fire fights happening presently..).  These difficult bugs in scala/java interoperability and building were a significant contributor to decision at  my company not to move forward with scala.  We decided to give scala another shot in the future when the tools matured - and if your instructions lead to  a clean bidirectional java/scala build env that would be a big step in that direction.

stephenb 


2013/8/23 Xiaofeng Lin <xfg...@gmail.com>

--
 
---
You received this message because you are subscribed to a topic in the Google Groups "Maven and Scala" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/maven-and-scala/L1r4D_L_7SY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to maven-and-sca...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages