1

I'm starting a new project in Scala that heavily depends on source files in another Java project. At first I thought about creating a new package/module in that project, but that seemed messy. However, I don't want to duplicate the Java code in my new project. The best solution I came up with is to reference the Java project through an svn external dependancy. Another solution is creating a jar file from the original, I would attach it as a library to the new Scala project. However, this makes keeping up-to-date more inconvenient. Which route is better?

SVN external:

  • Easy to update
  • Easy to commit
  • Inconvenient setup

Jar file as library:

  • Easy to setup
  • Old code isn't in active development(stable, bug fixes only)
  • Multi-step update
  • Need to open old project to make changes
stan
  • 4,885
  • 5
  • 49
  • 72
  • 1
    I would keep the project separate and possibly look at upgrading it to be built with maven, then you can use maven to manage the dependency between the projects with the artifacts maintained in a repository. – codeghost Jan 23 '13 at 17:01
  • What does it mean to be dependent on _source_, per se? The Scala compiler can read `.class` files to derive Java class definitions. In any event, source duplication is a very bad idea. If you use IDEA (probably Eclipse, as well) you can have it link to source from an external library without literally incorporating that source code into the project that uses it. – Randall Schulz Jan 23 '13 at 17:02
  • @RandallSchulz: I use Intellij and I thought about that too, but some of my coworkers use Eclipse/Netbeans, so I want to stay away from IDE specific solutions. – stan Jan 23 '13 at 17:13
  • I agree with the suggestion to use maven. Just switch your build system to maven (or compatible systems like SBT, Leiningen etc.) and reference to your Java project from Scala project. BTW, using maven automatically implies that you will have IDE-agnostic solution. Most of modern IDEs support maven. – Vladimir Matveev Jan 23 '13 at 17:44
  • In sbt you able to reference and depend on external project via git/svn/hg link. Not sure, if there is requirement to have both projects managed under SBT. – om-nom-nom Jan 23 '13 at 18:57

3 Answers3

2

You have your Scala project, and it depends on parts of your Java project. To me, that sounds like the Java project should be a library, with a version number, which is simply referenced by your Scala project. Or perhaps those parts that are shared across the two projects should be separated into a library. Using build tools like Maven will keep it clear which version is being used. The Java project can then evolve separately, or if it needs to change for the sake of the Scala project, you can bring out a new version and keep using an older one in other contexts if you're afraid of breakage.

The only exception where you go beyond binary dependencies that I can think of is if the Java code itself is actually being processed in some way at compile-time that is specific to the Scala project. Like, say, annotation processing.

Using SVN externals could be a solution. Just make sure you work with branches and snapshots to make sure some update to your Java code on the trunk doesn't suddenly make your Scala project inoperable.

G_H
  • 11,739
  • 3
  • 38
  • 82
2

Whether you have mixed scala/java or not is irrelevent. You probably want to use external dependencies (jars) if both projects are very distinct and have different release cycles. Otherwise, you can use sbt and have your two projects be sub-projects of the same build (your sbt build file would have the scala project depend on the java project). Or even, if they are really so intertwined, just have one project with both java and scala source files. Sbt handles that just fine.

Maven is an option too.

Régis Jean-Gilles
  • 32,541
  • 5
  • 83
  • 97
0

You can very easily set up a mixed scala/Java project using maven. Have a look at the scala-maven-plugin.

https://github.com/davidB/scala-maven-plugin

Eclipse users may not be too pleased due the poor maven integration.

sorencito
  • 2,517
  • 20
  • 21