SBT how to use classes from Build.sbt built-in plugin

Any classes defined in project/*.scala files are made available for use by SBT inside assembly definition code.

I would like these classes to be available during the execution of the SBT plugin task, but they do not seem to be available.

Why is this and how can I fix it?

The specific problem I'm trying to solve is to add custom rules for Scalastyle . The project does not currently support creating its own rules , but I thought I could add the rule to the project/*.sbt and then use it inside Scalastyle .

If I define a rule in project/MyRule.scala , then it is available in the project/Build.scala :

 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 is running, the used class loader cannot see this 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 checked the classloader, which is used during the Scalastyle task, and has all the SBD librarydependency banks in its class path, but does not have classes from the SBT project.

How can I add these classes to my classpath?

+5
scala classloader sbt
source share
1 answer

I do not think this is possible, as documented in the SBT documentation

Note. At run time, all plugins for all collections are loaded into a separate, parent class loader of the class loaders for the assembly. This means that plugins will not see classes or resources from assembly definitions.

Edited

Based on the comments, I think maybe this solution might work. In project/ create an additional project called scala-style-defs . There are your rules. In the same project, create build.sbt with the assembly definition, for example.

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

Then in project/ create build.sbt with the following contents

 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 enable scalastyle settings

 org.scalastyle.sbt.ScalastylePlugin.Settings 
+3
source share

All Articles