18.17. Using a Maven Repository Library with SBT

Problem

You want to use a Java library that’s in a Maven repository, but the library doesn’t include information about how to use it with Scala and SBT.

Solution

Translate the Maven groupId, artifactId, and version fields into an SBT libraryDependencies string.

For example, I wanted to use the Java HTMLCleaner project in a Scala/SBT project. The HTMLCleaner website provided the following Maven information, but no SBT information:

<dependency>
  <groupId>net.sourceforge.htmlcleaner</groupId>
  <artifactId>htmlcleaner</artifactId>
  <version>2.2</version>
</dependency>

Fortunately this translates into the following SBT libraryDependencies string:

libraryDependencies += "net.sourceforge.htmlcleaner" % "htmlcleaner" % "2.2"

After adding this line to my build.sbt file, I ran sbt compile, and watched as it downloaded the HTMLCleaner JAR file and dependencies:

[info] downloading http://repo1.maven.org/maven2/net/sourceforge/htmlcleaner/ htmlcleaner/2.2/htmlcleaner-2.2.jar ... [info] [SUCCESSFUL ] net.sourceforge.htmlcleaner#htmlcleaner;2.2!htmlcleaner.jar (864ms) [info] downloading http://repo1.maven.org/maven2/org/jdom/jdom/1.1/jdom-1.1.jar ... [info] [SUCCESSFUL ] org.jdom#jdom;1.1!jdom.jar (514ms) [info] downloading http://repo1.maven.org/maven2/org/apache/ant/ant/1.7.0/ant-1.7.0.jar ... [info] [SUCCESSFUL ] org.apache.ant#ant;1.7.0!ant.jar (1997ms) [info] downloading http://repo1.maven.org/maven2/org/apache/ant/ant-launcher/ 1.7.0/ant-launcher-1.7.0.jar ...

Get Scala Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.