Any classes defined in project/*.scala
files are made available for use by SBT inside the build definition code.
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?
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
.
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
...
}
... 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)
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?
project/Build.scala
code? I think it should be surrounded byobject MyBuild extends Build { ... }
. See sbt docs – laughedelicproject/
meta-project available to the plugins when building the main project. The linked question is asking about using project classes in the meta-project. They are related though. – Rich