Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SBT how to use classes from Build.sbt inside plugin Task execution

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?

like image 776
Rich Avatar asked Mar 13 '14 08:03

Rich


1 Answers

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.

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/"

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"))

and of course plugins.sbt with

addSbtPlugin("org.scalastyle" %% "scalastyle-sbt-plugin" % "0.4.0")

resolvers += "sonatype-releases" at "https://oss.sonatype.org/content/repositories/releases/"

Now in your main project create build.sbt and there include scalastyle settings

org.scalastyle.sbt.ScalastylePlugin.Settings
like image 75
lpiepiora Avatar answered Nov 04 '22 10:11

lpiepiora