How to add tools.jar as a "dynamic dependency" in sbt. Is it possible?

I need to use tools.jar in my project, but it makes no sense to put it in a jar, since the user already has it. So, can it be used as a โ€œdynamic dependencyโ€? value, I want my code to compile using the tools.jar file found in my JAVA_HOME , but I do not want it to be packaged with it. I can add it to the classpath with double activation at runtime using the user JAVA_HOME . eg:

 object Main1 extends App { val myjar = Main1.getClass.getProtectionDomain.getCodeSource.getLocation.getFile val tools = System.getProperty("java.home").dropRight(3)+"lib/tools.jar" // drop "jre" val arguments = Array("java", "-cp", myjar+":"+tools, "me.myapp.Main2") ++ args val p = Runtime.getRuntime.exec(arguments) p.getErrorStream.close p.getOutputStream.close } 

FYI: I will package the application using the build plugin in a separate jar file.

EDIT:

an ugly solution would be to copy the tools.jar file to the lib directory in my project and add:

 excludedJars in assembly <<= (fullClasspath in assembly) map { cp => cp filter {_.data.getName == "tools.jar"} } 

in build.sbt can this be done more elegantly without copying the jar file? it would be much easier to switch the JVM and automatically use the "right" tools.jar file ...

+3
source share
3 answers

After carefully reading the SBT Documentation , I learned how to do this:
in build.sbt I needed to add:

 // adding the tools.jar to the unmanaged-jars seq unmanagedJars in Compile ~= {uj => Seq(Attributed.blank(file(System.getProperty("java.home").dropRight(3)+"lib/tools.jar"))) ++ uj } // exluding the tools.jar file from the build excludedJars in assembly <<= (fullClasspath in assembly) map { cp => cp filter {_.data.getName == "tools.jar"} } 

and what about it ... just like that :)

+5
source

There is now an sbt plugin for this: https://github.com/chipsenkbeil/sbt-jdi-tools

0
source

I have not tested this, but could you use the% configuration syntax to display the dependency only at runtime or compile? Of course, tools.jar should be automatically included anyway?

 libraryDependencies += "com.sun" % "tools" % "1.6.0" % system 

I'm not sure about the "system" configuration, I know that this works in maven, you can try this with "compilation" instead.

-1
source

Source: https://habr.com/ru/post/928104/


All Articles