2012-09-15 00:08:35 +02:00
|
|
|
============
|
|
|
|
|
Java Sources
|
|
|
|
|
============
|
|
|
|
|
|
|
|
|
|
sbt has support for compiling Java sources with the limitation that
|
|
|
|
|
dependency tracking is limited to the dependencies present in compiled
|
|
|
|
|
class files.
|
|
|
|
|
|
|
|
|
|
Usage
|
|
|
|
|
=====
|
|
|
|
|
|
|
|
|
|
- ``compile`` will compile the sources under ``src/main/java`` by
|
|
|
|
|
default.
|
2012-11-19 02:29:01 +01:00
|
|
|
- ``testCompile`` will compile the sources under ``src/test/java`` by
|
2012-09-15 00:08:35 +02:00
|
|
|
default.
|
|
|
|
|
|
2012-11-19 02:29:01 +01:00
|
|
|
Pass options to the Java compiler by setting ``javacOptions``:
|
2012-09-15 00:08:35 +02:00
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
|
|
javacOptions += "-g:none"
|
|
|
|
|
|
|
|
|
|
As with options for the Scala compiler, the arguments are not parsed by
|
|
|
|
|
sbt. Multi-element options, such as ``-source 1.5``, are specified like:
|
|
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
|
|
javacOptions ++= Seq("-source", "1.5")
|
|
|
|
|
|
|
|
|
|
You can specify the order in which Scala and Java sources are built with
|
2012-11-19 02:29:01 +01:00
|
|
|
the ``compileOrder`` setting. Possible values are from the
|
2012-09-15 00:08:35 +02:00
|
|
|
``CompileOrder`` enumeration: ``Mixed``, ``JavaThenScala``, and
|
|
|
|
|
``ScalaThenJava``. If you have circular dependencies between Scala and
|
|
|
|
|
Java sources, you need the default, ``Mixed``, which passes both Java
|
|
|
|
|
and Scala sources to ``scalac`` and then compiles the Java sources with
|
|
|
|
|
``javac``. If you do not have circular dependencies, you can use one of
|
|
|
|
|
the other two options to speed up your build by not passing the Java
|
|
|
|
|
sources to ``scalac``. For example, if your Scala sources depend on your
|
|
|
|
|
Java sources, but your Java sources do not depend on your Scala sources,
|
|
|
|
|
you can do:
|
|
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
|
|
compileOrder := CompileOrder.JavaThenScala
|
|
|
|
|
|
|
|
|
|
To specify different orders for main and test sources, scope the setting
|
|
|
|
|
by configuration:
|
|
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
|
|
// Java then Scala for main sources
|
|
|
|
|
compileOrder in Compile := CompileOrder.JavaThenScala
|
|
|
|
|
|
|
|
|
|
// allow circular dependencies for test sources
|
|
|
|
|
compileOrder in Test := CompileOrder.Mixed
|
|
|
|
|
|
|
|
|
|
Note that in an incremental compilation setting, it is not practical to
|
|
|
|
|
ensure complete isolation between Java sources and Scala sources because
|
|
|
|
|
they share the same output directory. So, previously compiled classes
|
|
|
|
|
not involved in the current recompilation may be picked up. A clean
|
|
|
|
|
compile will always provide full checking, however.
|
|
|
|
|
|
|
|
|
|
By default, sbt includes ``src/main/scala`` and ``src/main/java`` in its
|
|
|
|
|
list of unmanaged source directories. For Java-only projects, the
|
|
|
|
|
unnecessary Scala directories can be ignored by modifying
|
|
|
|
|
``unmanagedSourceDirectories``:
|
|
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
|
|
// Include only src/main/java in the compile configuration
|
2012-11-19 02:29:01 +01:00
|
|
|
unmanagedSourceDirectories in Compile := (javaSource in Compile).value :: Nil
|
2012-09-15 00:08:35 +02:00
|
|
|
|
|
|
|
|
// Include only src/test/java in the test configuration
|
2012-11-19 02:29:01 +01:00
|
|
|
unmanagedSourceDirectories in Test := (javaSource in Test).value :: Nil
|
2012-09-15 00:08:35 +02:00
|
|
|
|
|
|
|
|
However, there should not be any harm in leaving the Scala directories
|
|
|
|
|
if they are empty.
|