How to sneak-in Scala in a Java Maven project ?

56 views
Skip to first unread message

Jan Goyvaerts

unread,
May 4, 2012, 8:38:36 AM5/4/12
to bes...@googlegroups.com
I might have a candidate to solve a specific problem with Scala. But I'd like to make sure the other developers don't need to care - or even notice -  there is actually Scala source code in the project.

Is that possible ?

Jo Voordeckers

unread,
May 4, 2012, 9:15:49 AM5/4/12
to BeScala on behalf of Jan Goyvaerts
It's a bit tricky assembling the maven POM, but I've done a few basic tests with interoperability between java and scala classes in a maven project with java, spring, hibernate in the past and that seemed to work, I don't know if it's efficient nor if the development experience in the long term is good enough, but I think all developers probably need to install scala and/or scala plugins in their IDEs as I'm not sure the maven plugins in the IDEs respond well to suddenly having scala sources that they don't necessarily know how to build/package when deploying to their local appserver.

The only way to truly fly below the radar would be to package the scala code as a JAR outside of the project, but then again your scala project would need to depend on the other project classes as so probably this is not worth the trouble.... sneaking-in scala code via unit tests might be easier, as these usually only run on the CI system using the native maven plugins, don't need to be deployed, packaged, etc...

good luck! 

- Jo


On Fri, May 4, 2012 at 2:38 PM, BeScala on behalf of Jan Goyvaerts <bes...@googlegroups.com> wrote:
I might have a candidate to solve a specific problem with Scala. But I'd like to make sure the other developers don't need to care - or even notice -  there is actually Scala source code in the project.

Is that possible ?



--
- Jo

Jo Voordeckers

unread,
May 4, 2012, 9:31:37 AM5/4/12
to BeScala on behalf of Jan Goyvaerts
Sure, but as I said, I'm not sure if the IDE plugins for maven use the native maven plugin compilers when doing deployments to appservers... and that becomes a problem when your scala code is a compile- or runtime dependency of the java code... if the scala code is on it's own or only depends on java code I think it can work.

On Fri, May 4, 2012 at 3:23 PM, BeScala on behalf of Jan Goyvaerts <bes...@googlegroups.com> wrote:
I think it is acceptable that opening the Scala source files is at their own risk. It's just that if they don't open them, everything should go unnoticed. At least not impede with their work.

The pom file will be changed of course. As will be the classpath of the application of include Scala's runtime. 



--
- Jo

Renato Guerra Cavalcanti

unread,
May 4, 2012, 9:32:14 AM5/4/12
to BeScala on behalf of Jan Goyvaerts
This will only work if your Scala code never ever calls a Java class. Otherwise, the code will break if you refactored it without plugin support. For instance, if you use Scala to test your Java code.

If the Java code need to call the Scala code, than I would shield it behind a Java interface. Without a plugin, your co-workers will not have the Scala code compiled in the IDE. They can eventually have it in target/classes if they compile from maven, but it'll be probably not always the case.


On 04 May 2012, at 15:23, BeScala on behalf of Jan Goyvaerts wrote:

I think it is acceptable that opening the Scala source files is at their own risk. It's just that if they don't open them, everything should go unnoticed. At least not impede with their work.

The pom file will be changed of course. As will be the classpath of the application of include Scala's runtime. 

Jan Goyvaerts

unread,
May 4, 2012, 9:23:57 AM5/4/12
to BeScala on behalf of Jo Voordeckers
I think it is acceptable that opening the Scala source files is at their own risk. It's just that if they don't open them, everything should go unnoticed. At least not impede with their work.

The pom file will be changed of course. As will be the classpath of the application of include Scala's runtime. 

Jan Goyvaerts

unread,
May 4, 2012, 10:23:13 AM5/4/12
to BeScala on behalf of Renato Cavalcanti
So probably better be in a single Maven module where dependencies can be imposed...

Jan Goyvaerts ©

unread,
May 7, 2012, 4:33:05 PM5/7/12
to BeScala on behalf of Renato Cavalcanti
I'm wondering whether they'll see tomorrow I've extended the project's pom with Scala.
http://blog.e-beyond.de/2011/09/10/maven-mixed-projects-with-java-and-scala/

That would be the first step to sneaking in foreign language code. :-)

Renato Guerra Cavalcanti

unread,
May 7, 2012, 4:36:12 PM5/7/12
to BeScala on behalf of Jan Goyvaerts
Well done. 

Please, keep us up to date. :-)

Jan Goyvaerts ©

unread,
May 10, 2012, 1:18:10 AM5/10/12
to BeScala on behalf of Renato Cavalcanti
There's something fishy with multi-module Maven projects in IntelliJ. Complaining about classes with the same name in different modules. It works again when telling it should compile java with javac and scala with scalac.

It all works when only using Maven.

Nobody saw something - yet. :-)

Jan Goyvaerts ©

unread,
May 10, 2012, 7:54:07 AM5/10/12
to BeScala on behalf of Renato Cavalcanti
Now I'm probably asking an incredibly stupid question... 

Are classes with no access modifier in the default package not visible to other classes ? As soon as I'm giving it a package name it's visible.

Sorry for asking something that trivial...

Renato Guerra Cavalcanti

unread,
May 10, 2012, 8:26:36 AM5/10/12
to BeScala on behalf of Jan Goyvaerts
Hi Jan,

Are the other classes also on the default package?

You'll not be able to access class in the default package if you are not in the default package as well. That's also valid for java.

The following code will work:
class A {
  def f() = println("Hey, from f")

object App {
  def main(args: Array[String]) {
      val a = new A
      a.f()
}

But it App is on a package, it won't.
class A {
  def f() = println("Hey, from f")

package test
object App {
  def main(args: Array[String]) {
      val a = new A // compilation error - not found: type A
      a.f()
}

And you can't import A because it has no package.

Jan Goyvaerts ©

unread,
May 10, 2012, 8:59:45 AM5/10/12
to BeScala on behalf of Renato Cavalcanti
Well look at that ! Never tried that in Java. And indeed...

You didn't see this email, did you ? :-)

Renato Guerra Cavalcanti

unread,
May 10, 2012, 9:05:41 AM5/10/12
to BeScala on behalf of Jan Goyvaerts
Which email? :-)

Jan Goyvaerts I❤©

unread,
May 10, 2012, 10:09:52 AM5/10/12
to BeScala on behalf of Renato Cavalcanti
Anyone has a tutorial of some kind to set up intellij 11 for scala/java/maven ?

I can compile it all using Maven.
Code completion works java <-> scala.

But when rebuilding the complete project the java classes can't compile because they can't find the scala classes.

Jan Goyvaerts I❤©

unread,
May 11, 2012, 4:20:58 AM5/11/12
to BeScala on behalf of Renato Cavalcanti
It seems the lastest scala plugin update of today fixed something with the compilation because now it all works. Yeah ! :-)
Reply all
Reply to author
Forward
0 new messages