Tuesday, December 5, 2006

Building scala from maven - maven-scala-plugin

I recently wanted to build scala code in maven. There was no plugin to do this but I did find a patch for maven. I didn't like this approach because maven is supposed to be plugin based and you should be able to extend it without changing it. So I wrote a quick and dirty little plugin http://millstone.iodp.tamu.edu/~blambi/maven-scala-plugin(docs). Update 7/11/07: The maven-scala-plugin is available on Google Code

At first I wanted to use the api provided by scala to compile code. I used the scala eclipse plugin as an example, and got it working. When I ran it as a maven plugin I got a strange error: "object scala not found". I assumed this meant that the package scala wasn't available, due to the fact that the scala jar files are not included on the classpath when the jvm is started by maven. So next I tried creating a new ClassLoader and force loading every class in the scala libraries, but I received the same error.

The simple way, I decided, was to run a new jvm with the proper classpath. This is what scalac does when it runs. So I simply setup the classpath and execute 'java scala.tools.nsc.Main'. It may be a naive implementation, but it works and I'm happy I can now compile my scala code using maven.

4 comments:

Matt Bone said...

Thanks for the plugin. I'm using it now. Any plans on releasing the source so that we can hack on it? Or updating it to scala 2.4?

Brice Lambi said...

I have updated the repository with scala-2.4. The source tarballs are available for download on the website. If you are having trouble downloading them shoot me an email.

Jules Morrison said...

Hi, I was having trouble with scala:run. It seemed not to be including the full maven classpath. A jarball that's referenced via Class.forName was unavailable at runtime.

I finally tracked the problem down - it seems only explicitly mentioned dependencies end up on the classpath. Transitive dependencies are skipped. Once I added the transitive dependency to my POM, the run was a success.

Brice Lambi said...

I've noticed the same problem before and I've been meaning to fix it. I think I just need to change the way that I setup the classpath before running it. The maven plugin API provides two methods for getting dependencies and I must be using the one that only includes dependencies declared in the POM. I created an issue on the new google code site for the project. There are several issues there that I need to address. I haven't spent any time on this project for a while and need to get busy!

Thanks!