问题描述
在调用特殊的 run
任务之前,我想将工件从存储库复制到 target
子目录.
//审计任务必须调用运行任务,在某个目录复制工件后审计:= {println("审核中")//1. 将一些工件复制到目录val auditAgent = ((fullClasspath in Test value) filter (_.data.getName.startsWith("reactive-audit-agent"))).head.dataval targetAgent= target.value/"reactive-audit-libs"/"reactive-audit-agent.jar"IO.copyFile(auditAgent,targetAgent)//2. 设置javaOpt.像这样的东西javaOptions += "-javagent:"+targetAgent//3. 使用 javaagent 调用运行任务???}
使用 sbt run
我想运行该项目.使用 sbt audit
我想用特定的 javagent 运行项目.
我尝试使用 fullRunTask(audit,...)
来扩展 audit
任务,但是 audit
中的主体没有运行
审核 := {println("审核中")}fullRunTask(审计,运行时,com.octo.reactive.sample.TestApp")
带有更新报告的自定义配置
基于配置和更新报告 我想出了另一种方法来声明依赖项并将其用于一个任务.这种方法明确声明了对任务所需的外部文件的依赖,比如成为 Java 代理.
我在过去的答案修订中似乎错过了要求:
使用 sbt run
我想运行该项目.使用 sbt audit
我想用特定的 javagent 运行项目.
以下 build.sbt
给出了一个解决方案(它假设 sbt 0.13.7-M3 与 无空行的更改 - 如果您还不想升级,请添加空行):
lazy val Agent = config("agent") 扩展运行时inConfig(Agent)(Defaults.configSettings)代理中的 sourceDirectory <<= 编译中的 sourceDirectoryivyConfigurations += 代理//使文件在 Ivy2 本地存储库中可用libraryDependencies += "org.aspectj" % "aspectjweaver" % "1.8.2" % "agent"fork in (Agent, run) := truejavaOptions in (Agent, run) +="-javaagent:" + update.value.select(configurationFilter("agent")).filter(_.name.contains("aspectjweaver")).headaddCommandAlias("审计", "agent:run")
构建定义了一个新的隐藏 agent
配置,其依赖项仅属于该配置.
使用 update
(如 Update 中所述报告) 我可以只选择声明的依赖项作为 run
的代理",但是由于配置 extend Runtime
我不得不排除其他传递"依赖项(Runtime
给出).
当您执行 run
时,它会运行旧的 run
任务 - 这里没有变化:
>跑步[信息] 运行 com.example.Hello你好,世界!
当您运行 agent:run
时,它会在 Agent
配置中执行自定义的 run
(除非您更改,否则您看不到任何更改 - 打错字in - -javaagent
到其他最终破坏启动的东西):
>代理:运行[信息] 运行 com.example.Hello[信息]你好,世界!
作为最终解决方案,audit
别名实际上是 agent:run
.
>审计[信息] 运行 com.example.Hello[信息]你好,世界!
使用解决方案 sbt 像往常一样下载依赖项(因此我们不必担心文件是否存在,如果不存在,构建将简单地失败).
使用资源生成器将文件复制到目录
如何在运行项目之前将一些文件复制到目录中?
资源生成任务应该在resourceManaged的子目录中生成资源并返回生成的文件序列.
做show resourceManaged
来找出target/scala-[scalaVersion]
下默认的路径.
I would like to copy artifact from repository to the target
subdirectory, before invoke the special run
task.
// audit task must invoke the run task, after copy artifact in some directory
audit := {
println("In audit")
// 1. Copy some artifact to directory
val auditAgent = ((fullClasspath in Test value) filter (_.data.getName.startsWith("reactive-audit-agent"))).head.data
val targetAgent= target.value / "reactive-audit-libs" / "reactive-audit-agent.jar"
IO.copyFile(auditAgent,targetAgent)
// 2. Set javaOpt. Something like this
javaOptions += "-javagent:"+targetAgent
// 3. Invoke run task with javaagent
???
}
With sbt run
I would like to run the project.With sbt audit
I would like to run the project with a specific javagent.
I try to use fullRunTask(audit,...)
to extend the audit
task, but the body in audit
was not running
audit := {
println("In audit")
}
fullRunTask(audit, Runtime, "com.octo.reactive.sample.TestApp")
Custom configuration with update report
Based upon Configurations and Update Report I've figured out another approach to declare a dependency and use it in a task. This approach explicitly declares a dependency on an external file that's required for a task, say to become a java agent.
What I seem to have missed in the past revisions of the answer was the requirement:
The following build.sbt
gives a solution (it assumes sbt 0.13.7-M3 with the change for no blank lines - add blank lines if you don't want to upgrade yet):
lazy val Agent = config("agent") extend Runtime
inConfig(Agent)(Defaults.configSettings)
sourceDirectory in Agent <<= sourceDirectory in Compile
ivyConfigurations += Agent
// that makes the file available in the Ivy2 local repository
libraryDependencies += "org.aspectj" % "aspectjweaver" % "1.8.2" % "agent"
fork in (Agent, run) := true
javaOptions in (Agent, run) +=
"-javaagent:" + update.value.select(configurationFilter("agent")).filter(_.name.contains("aspectjweaver")).head
addCommandAlias("audit", "agent:run")
The build defines a new hidden agent
configuration with a dependency that belongs to the configuration only.
Using update
(as described in Update Report) I could select just the declared dependency as the "agent" for run
, but since the configuration extend Runtime
I had to exclude the other "transitive" dependencies (that Runtime
gives).
When you execute run
it runs the good old run
task - no changes here:
> run
[info] Running com.example.Hello
Hello, world!
When you run agent:run
it executes the custom run
in Agent
configuration (you see no change unless you change - make a typo in - -javaagent
to something else that ultimately breaks the startup):
> agent:run
[info] Running com.example.Hello
[info] Hello, world!
As the final solution there's the audit
alias that's effectively agent:run
.
> audit
[info] Running com.example.Hello
[info] Hello, world!
With the solution sbt downloads the dependency as usual (so we don't have to worry if the file exists or not and when it does not, the build will simply fail).
Copying files to directory using resource generators
Read the page Generating files where you find the section Generate resources that says:
Do show resourceManaged
to find out the path that's by default under target/scala-[scalaVersion]
.
这篇关于如何将文件复制到目标以便在运行任务中使用它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!