Define a Scala or Java Library Target
Problem
You need to define a new Scala or Java library target that other projects can use as a dependency.
Note: If you need to define a new Scala or Java binary target, see Define a JVM Executable
Solution
Add a scala_library
or java_library
target to a BUILD
file in the appropriate directory. A scala_library
or java_library
target will enable you to compile the library using Pants' compile
goal. Here's an example:
$ ./pants compile myproject/src/main/scala # Assuming there's a BUILD file with a scala_library definition named "scala" in that location
Discussion
A scala_library
or java_library
target should specify the following:
- A
name
for the library. This may be something like justscala
if you have only onescala_library
target in a project or something more specific likeclient-lib
. - Either a single
source
file or a list ofsources
. If you're including just a few files, you should consider specifying a sources list, e.g.sources=['File1.scala', 'File2.scala']
; if you're including, you may want to specify aglobs
orrglobs
, e.g.sources=globs('*.scala')
. More info can be found in Use globs and rglobs to Group Files. The example further down use anrglobs
definition. - A list of
dependencies
(optional). More info on dependencies can be found in Add a Dependency on Another Target.
Here's an example target definition:
# myproject/src/main/scala/BUILD scala_library(name='scala', sources=globs('*.scala'), dependencies=[ 'client-lib', 'analytics-lib', 'static/resources/json:config', ], )
That library can then be compiled (perhaps for debugging purposes):
$ ./pants compile myproject/src/main/scala
You can combine library targets together into a single target using a target alias. More info can be found in Create an Alias for a Target.
See Also
- Compile a Library Target
- Define a JVM Executable
- Create a Bundled zip or Other Archive
- Create an Alias for a Target
Generated by publish_docs
from dist/markdown/html/src/docs/common_tasks/jvm_library.html 2018-05-03T17:02:35.723906