Java and Scala overview
Pants's support for Java and Scala.
We are done implementing most functionality for Pants's Java and Scala support (tracked here). However, there may be use cases that we aren't yet handling.
Please share feedback for what you need to use Pants with your JVM project by either opening a GitHub issue or joining our Slack!
Check out github.com/pantsbuild/example-jvm to try out Pants's Java and Scala support.
Initial setup
First, activate the relevant backends in pants.toml
:
[GLOBAL]
backend_packages = [
# Each backend can be used independently, so there is no need to enable Scala if you
# have a pure-Java repository (or vice versa).
"pants.backend.experimental.java",
"pants.backend.experimental.scala",
]
Then run pants tailor ::
to generate BUILD files. This will create java_sources
and scala_sources
targets in every directory containing library code, as well as test targets like scalatest_tests
and junit_tests
for filenames that look like tests.
❯ pants tailor ::
Created src/jvm/org/pantsbuild/example/app/BUILD:
- Add scala_sources target app
Created src/jvm/org/pantsbuild/example/lib/BUILD:
- Add java_sources target lib
Created tests/jvm/org/pantsbuild/example/lib/BUILD:
- Add scalatest_tests target lib
You can run pants list ::
to see all targets in your project:
❯ pants list
...
src/jvm/org/pantsbuild/example/app:app
src/jvm/org/pantsbuild/example/app/ExampleApp.scala
src/jvm/org/pantsbuild/example/lib:lib
src/jvm/org/pantsbuild/example/lib/ExampleLib.java
tests/jvm/org/pantsbuild/example/lib:lib
tests/jvm/org/pantsbuild/example/lib/ExampleLibSpec.scala
Choosing JDK and Scala versions
Pants 2.11.x
adds support for choosing JDK and Scala versions per target in your repository, but to reduce the amount of boilerplate required, most users set repository-wide defaults in pants.toml
, and then only override them when necessary for particular targets.
JDK
JDKs used by Pants are automatically fetched using Coursier, and are chosen using the [jvm].jdk
setting to set a repository-wide default.
To override the default on a particular target, you can use the jdk=
field. It can be useful to use the parametrize
builtin with the jdk=
field, particularly to run test targets under multiple JDKs.
Scala version
The Scala version to use is configured on a resolve-by-resolve basis (see the "Third-party dependencies" section below) using the [scala].version_for_resolve
option. The default Scala version for your repository will thus be whichever Scala version is configured for the "default" resolve, which is configured by the [jvm].default_resolve
option.
To use multiple Scala versions in a repository, you would define multiple resolves, and then adjust the resolve
field of any targets which should be used with the non-default_resolve
resolve.
To cross-build a set of Scala targets for multiple Scala versions, you can use the parametrize
builtin with the resolve=
field of the target and its dependencies.
The Scala backend currently requires that a jvm_artifact
target for the org.scala-lang:scala-library
Scala runtime be present in any resolve used for Scala. If such a jvm_artifact is missing, Pants will error. Pants will automatically inject a dependency on the runtime. (This target may be automatically supplied by Pants in a future version, but that is not currently implemented.)