SBT如何在插件任务执行中使用Build.sbt中的类 [英] SBT how to use classes from Build.sbt inside plugin Task execution
问题描述
在 project / *。scala
文件中定义的任何类都可以由SBT在构建定义代码中使用。
Any classes defined in project/*.scala
files are made available for use by SBT inside the build definition code.
我希望这些类在执行SBT插件任务期间可用,但它们似乎不可用。
I would like those classes to be available during the execution of an SBT plugin task, but they do not appear to be available.
为什么?我该如何解决?
Why is that, and how can I fix it?
我要解决的特定问题是为 Scalastyle
添加自定义规则。项目目前不支持编写自己的规则,但我认为我可以将规则添加到 project / *。sbt
文件,然后在 Scalastyle
中使用。
The specific problem I am trying to solve is adding custom rules for Scalastyle
. The project does not currently support writing your own rules, but I thought I might be able to add a rule to the project/*.sbt
files and then use it inside Scalastyle
.
如果我在 project / MyRule.scala
中定义一个规则,则该规则在 project / Build中可用。 scala
设置:
If I define a rule in project/MyRule.scala
, then it is available in the project/Build.scala
settings:
object MyBuild extends Build {
lazy val project = Project("MyProject", file("."))
.settings(ScalastylePlugin.Settings: _*)
// my rule is available in this classloader:
val test = classOf[MyRule].getName
...
}
...但是运行ScalastylePlugin Task时,它使用的类加载器看不到该类:
... but when the ScalastylePlugin Task runs, the classloader it is using cannot see that class:
java.lang.NoClassDefFoundError: MyRule
java.net.URLClassLoader$1.run(URLClassLoader.java:202)
java.security.AccessController.doPrivileged(Native Method)
java.net.URLClassLoader.findClass(URLClassLoader.java:190)
java.lang.ClassLoader.loadClass(ClassLoader.java:307)
java.lang.ClassLoader.loadClass(ClassLoader.java:248)
java.lang.Class.forName0(Native Method)
java.lang.Class.forName(Class.java:169)
org.scalastyle.Checker$.newInstance(Checker.scala:194)
org.scalastyle.Checker$$anonfun$verifySource0$1.apply(Checker.scala:116)
org.scalastyle.Checker$$anonfun$verifySource0$1.apply(Checker.scala:116)
...
org.scalastyle.ScalastyleChecker.checkFiles(Checker.scala:64)
org.scalastyle.sbt.Tasks$.runScalastyle(Plugin.scala:121)
org.scalastyle.sbt.Tasks$.doScalastyle(Plugin.scala:90)
org.scalastyle.sbt.ScalastylePlugin$$anonfun$4$$anonfun$apply$1.apply(Plugin.scala:63)
org.scalastyle.sbt.ScalastylePlugin$$anonfun$4$$anonfun$apply$1.apply(Plugin.scala:63)
scala.Function6$$anonfun$tupled$1.apply(Function6.scala:35)
scala.Function6$$anonfun$tupled$1.apply(Function6.scala:34)
scala.Function1$$anonfun$compose$1.apply(Function1.scala:47)
sbt.$tilde$greater$$anonfun$$u2219$1.apply(TypeFunctions.scala:42)
sbt.std.Transform$$anon$4.work(System.scala:64)
sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:237)
sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:237)
sbt.ErrorHandling$.wideConvert(ErrorHandling.scala:18)
sbt.Execute.work(Execute.scala:244)
sbt.Execute$$anonfun$submit$1.apply(Execute.scala:237)
sbt.Execute$$anonfun$submit$1.apply(Execute.scala:237)
sbt.ConcurrentRestrictions$$anon$4$$anonfun$1.apply(ConcurrentRestrictions.scala:160)
sbt.CompletionService$$anon$2.call(CompletionService.scala:30)
java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
...
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
java.lang.Thread.run(Thread.java:619)
我已经检查了 Scalastyle
任务期间使用的类加载器,并且该类加载器在其类路径上具有所有SBT库依赖罐,但没有来自SBT的类
I have inspected the classloader in use during the Scalastyle
task, and it has all the SBT librarydependency jars on its classpath, but no classes from the SBT project.
如何将这些类添加到其类路径中?
How can I add these classes to its classpath?
推荐答案
我认为这是不可能的,因为 SBT文档中记录的文件
I don't think it is possible, as documented in the SBT documentation
注意:在运行时,所有构建的所有插件都将在单独的
父类加载器中加载用于构建的类加载器。这意味着
插件将看不到构建定义中的类或资源。
Note: At runtime, all plugins for all builds are loaded in a separate, parent class loader of the class loaders for builds. This means that plugins will not see classes or resources from build definitions.
已编辑
基于关于评论,我认为也许这种解决方案可能有效。在 project /
中创建一个名为 scala-style-defs
的额外项目。在那里放置您的规则。在同一项目中,使用构建定义创建 build.sbt
,例如
Edited
Based on the comments, I think maybe this solution may work. In the project/
create extra project called scala-style-defs
. There place your rules. In the same project create build.sbt
with the build definition e.g.
libraryDependencies += "org.scalastyle" %% "scalastyle" % "0.4.0"
resolvers += "sonatype-releases" at "https://oss.sonatype.org/content/repositories/releases/"
然后在 project /
中创建带有以下内容的build.sbt
Then in the project/
create build.sbt with following content
lazy val root = project.in(file(".")) dependsOn(scalastyleDefs)
lazy val scalastyleDefs = Project(id="scalastyleDefs", base=file("scala-style-defs"))
当然还有 plugins.sbt
和
addSbtPlugin("org.scalastyle" %% "scalastyle-sbt-plugin" % "0.4.0")
resolvers += "sonatype-releases" at "https://oss.sonatype.org/content/repositories/releases/"
现在在您的主项目中创建 build.sbt
,其中包括scalastyle设置
Now in your main project create build.sbt
and there include scalastyle settings
org.scalastyle.sbt.ScalastylePlugin.Settings
这篇关于SBT如何在插件任务执行中使用Build.sbt中的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!