简体   繁体   English

如何在sbt中添加tools.jar作为“动态依赖”。可能吗?

[英]how to add tools.jar as a “dynamic dependency” in sbt. is it possible?

i need to use tools.jar in my project, but there is not much sense to package it in the jar, since the user already have it. 我需要在我的项目中使用tools.jar,但将它打包在jar中没有多大意义,因为用户已经拥有它。 so, is it possible to use it as a "dynamic dependency"? 那么,是否有可能将其用作“动态依赖”? meaning, i want my code to compile by using tools.jar file found in my JAVA_HOME , but i don't want it to get packaged with it. 意思是,我希望我的代码可以使用我的 JAVA_HOME tools.jar文件进行编译 ,但我不希望它与它一起打包。 i can make sure to add it to the classpath with double activation at runtime with the user's JAVA_HOME used instead. 我可以确保在运行时使用用户的 JAVA_HOME将其添加到类路径中并进行双重激活。 for example: 例如:

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 packge the app using assembly plugin in a standalone jar file. 仅供参考:我在独立的jar文件中使用程序集插件打包应用程序。

EDIT: 编辑:

an ugly solution would be to copy the tools.jar file to a lib directory in my project, and add: 一个丑陋的解决方案是将tools.jar文件复制到我的项目中的lib目录,并添加:

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

in build.sbt can it be done more elegantly, without copying the jar file? build.sbt可以更优雅地完成,而无需复制jar文件吗? would be much more easier to switch JVMs, and use the "right" tools.jar file automatically... 切换JVM会更容易,并自动使用“正确”的tools.jar文件...

after reading more carefully the SBT Documentation , i found out how to do this: 在仔细阅读SBT文档之后 ,我发现了如何做到这一点:
in build.sbt i needed to add: build.sbt我需要添加:

// 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 that's about it... simple as that :) 这就是它...简单就是:)

现在有一个sbt插件可以帮到你: https//github.com/chipsenkbeil/sbt-jdi-tools

I haven't tested this, but could you not use the % configuration syntax to only map the dependency into runtime or compile? 我没有对此进行过测试,但您是否可以不使用%配置语法将依赖关系映射到运行时或编译? surely tools.jar should be automatically included anyway? 肯定还应该自动包含tools.jar?

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

I'm not sure about the "system" configuration, I know this works in maven, you could try this with "compile" instead though. 我不确定“系统”配置,我知道这可以在maven中工作,你可以尝试用“编译”来代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM