Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Have sbt put javadocs and sources of dependencies on the class path

When using a managed dependency, I can tell sbt to download the javadocs and sources:

"mygroup" % "mymodule" % "myversion" withJavadoc() withSources()

But these jars don't seem to be on the runtime classpath?

What I would like to do, is access the javadocs and sources from my application. Can I make these jars appear as managed resources, such that I could do

ClassLoader.getSystemClassLoader.getResource("/my/package/MyDependency.scala")

?

like image 791
0__ Avatar asked Mar 04 '14 00:03

0__


People also ask

How to solve SBT dependency tree problems?

SBT dependency tree If we need to solve the previous problem, we’ll need to inspect the dependencies and transitive dependencies. The solution now depends on our SBT version: For SBT 1.3.x, we need to include the sbt-dependency-graph plugin by adding the following to the project/plugins.sbt file:

How does sbt build up classpaths?

This page discusses how sbt builds up classpaths for different actions, like compile, run, and test and how to override or augment these classpaths. In sbt, the classpath includes the Scala library and (when declared as a dependency) the Scala compiler. Classpath-related settings and tasks typically provide a value of type Classpath.

How to exclude source files from a Classpath?

You can exclude source files by name ( butler.scala in the example below) like: Classpaths are also divided into internal and external dependencies. The internal dependencies are inter-project dependencies.

How to add the SBT-dependency-graph plugin for SBT?

The solution now depends on our SBT version: For SBT 1.3.x, we need to include the sbt-dependency-graph plugin by adding the following to the project/plugins.sbt file: addSbtPlugin ( "net.virtual-void" % "sbt-dependency-graph" % "0.10.0-RC1") For SBT >= 1.4 has built-in support for this plugin, so we just need to add to project/plugins.sbt:


1 Answers

You can do this by adding a classifier.

For a given library dependency, add a javadoc or sources classifer:

libraryDependencies += "org.scalaz" %% "scalaz-core" % "7.0.6" classifier "javadoc"

libraryDependencies += "org.scalaz" %% "scalaz-core" % "7.0.6" classifier "sources"

Then, access its contents from the classpath:

val docStream = getClass.getResourceAsStream("""/scalaz/Monad$.html""")
val doc = io.Source.fromInputStream(docStream).mkString
println(doc)

Here's a working example: https://earldouglas.com/ext/stackoverflow.com/questions/22160701/

like image 192
earldouglas Avatar answered Nov 15 '22 05:11

earldouglas