繁体   English   中英

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

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

我需要在我的项目中使用tools.jar,但将它打包在jar中没有多大意义,因为用户已经拥有它。 那么,是否有可能将其用作“动态依赖”? 意思是,我希望我的代码可以使用我的 JAVA_HOME tools.jar文件进行编译 ,但我不希望它与它一起打包。 我可以确保在运行时使用用户的 JAVA_HOME将其添加到类路径中并进行双重激活。 例如:

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
}

仅供参考:我在独立的jar文件中使用程序集插件打包应用程序。

编辑:

一个丑陋的解决方案是将tools.jar文件复制到我的项目中的lib目录,并添加:

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

build.sbt可以更优雅地完成,而无需复制jar文件吗? 切换JVM会更容易,并自动使用“正确”的tools.jar文件...

在仔细阅读SBT文档之后 ,我发现了如何做到这一点:
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"}
}

这就是它...简单就是:)

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

我没有对此进行过测试,但您是否可以不使用%配置语法将依赖关系映射到运行时或编译? 肯定还应该自动包含tools.jar?

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

我不确定“系统”配置,我知道这可以在maven中工作,你可以尝试用“编译”来代替。

暂无
暂无

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

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