Search code examples
scalasbtsbteclipse

Using classpathTransformerFactories in sbteclipse plugin to exclude Scala library?


I'm extracting my Scala Swing applications in separate modules. And I don't want to have the Scala Library from the IDE in the classpath because it also includes Scala Swing.

I've changed the following classpathentry

<classpathentry
  kind="con"
  path="org.scala-ide.sdt.launching.SCALA_CONTAINER"/>

to

<classpathentry
  sourcepath="C:\Users\wwagner\.ivy2\cache\org.scala-lang\scala-library\srcs\scala-library-2.10.3-sources.jar" 
  kind="lib" 
  path="C:\Users\wwagner\.ivy2\cache\org.scala-lang\scala-library\jars\scala-library-2.10.3.jar"/>

That works as expected but I found that the sbteclipse plugin supports classpathTransformerFactories, which can do it automatically.

How can classpathTransformerFactories help me with the use case?


Solution

  • What I had to do was implementing the following in my Build.scala

      // sbteclipse rewrite rules
      object ClasspathentryRewriteRule extends RewriteRule {
        override def transform(parent: Node): Seq[Node] = {
          parent match {
            case c @ <classpathentry/> if (c \ "@path").toString().endsWith("SCALA_CONTAINER") =>
              val home = System.getProperty("user.home")
              val base = s"""$home\\.ivy2\\cache\\org.scala-lang\\scala-library"""
              val srcPath = s"""${base}\\srcs\\scala-library-${D.scalaVersion}-sources.jar"""
              val path = s"""${base}\\jars\\scala-library-${D.scalaVersion}.jar"""
              <classpathentry sourcepath={ srcPath } kind="lib" path={ path }/>
            case other => other
          }
        }
      }
    
      // sbteclipse transformer
      object ClasspathentryTransformer extends EclipseTransformerFactory[RewriteRule] {
        override def createTransformer(ref: ProjectRef, state: State): Validation[RewriteRule] = {
          ClasspathentryRewriteRule.success
        }
      }
    

    In the setting the following code completed the job:

          ....
          EclipseKeys.classpathTransformerFactories := Seq(ClasspathentryTransformer)
          ....