scalasbtsbt-plugin

SBT package produces empty JAR after enhancing compiled classes


I am an author of a SBT plugin: https://github.com/atais/sbt-eclipselink-static-weave
It's purpose is to enhance compiled classes using provided StaticWeaveProcessor.

To achieve this step I have overriden the compile step with:

override def projectSettings: Seq[Def.Setting[_]] = Seq(
    ... 
    // https://www.scala-sbt.org/1.0/docs/Howto-Dynamic-Task.html#build.sbt+v2
    compile in Compile := Def.taskDyn {
      val c = (compile in Compile).value
      Def.task {
        (copyResources in Compile).value // we need to copy META-INF folder first, https://github.com/sbt/sbt/issues/3934
        weaveTask.value
        c
      }
    }.value
  )

Problem

Projects using this plugin compile properly, but they might produce an empty jar, during package or publish IF the compiled sources are not available beforehand.

You might want to check my pull-request where I have prepared a test project with test scenario. https://github.com/atais/sbt-eclipselink-static-weave/pull/2

Details

I have tried printing all the settings and mappings, but they seem fine.

However, I have found, that Package.Configuration step is using cacheStoreFactory: CacheStoreFactory which I think is my problem.

during the function makeJar(sources.toSeq, jar.file, manifest, log) there is a log.debug(sourcesDebugString(sources)) which gives empty result on first run:

[info] Finished EclipseLink static weaving in 610 ms.
[info] Packaging /home/atais/Documents/sbt-eclipselink-static-weave/sbt-test/target/scala-2.12/test_2.12-0.1.0-SNAPSHOT.jar ...
[debug] Input file mappings:
[debug]   

but properly lists files on the second:

[info] Finished EclipseLink static weaving in 559 ms.
[info] Packaging /home/atais/Documents/sbt-eclipselink-static-weave/sbt-test/target/scala-2.12/test_2.12-0.1.0-SNAPSHOT.jar ...
[debug] Input file mappings:
[debug]         META-INF/persistence.xml
[debug]           /home/atais/Documents/sbt-eclipselink-static-weave/sbt-test/target/scala-2.12/classes-weaved/META-INF/persistence.xml
[debug]         META-INF
[debug]           /home/atais/Documents/sbt-eclipselink-static-weave/sbt-test/target/scala-2.12/classes-weaved/META-INF
[debug]         com
[debug]           /home/atais/Documents/sbt-eclipselink-static-weave/sbt-test/target/scala-2.12/classes-weaved/com
[debug]         META-INF/orm-rtb.xml
[debug]           /home/atais/Documents/sbt-eclipselink-static-weave/sbt-test/target/scala-2.12/classes-weaved/META-INF/orm-rtb.xml
[debug]         com/github/atais/entity
[debug]           /home/atais/Documents/sbt-eclipselink-static-weave/sbt-test/target/scala-2.12/classes-weaved/com/github/atais/entity
[debug]         com/github
[debug]           /home/atais/Documents/sbt-eclipselink-static-weave/sbt-test/target/scala-2.12/classes-weaved/com/github
[debug]         com/github/atais
[debug]           /home/atais/Documents/sbt-eclipselink-static-weave/sbt-test/target/scala-2.12/classes-weaved/com/github/atais
[debug]         com/github/atais/entity/EntityB.class
[debug]           /home/atais/Documents/sbt-eclipselink-static-weave/sbt-test/target/scala-2.12/classes-weaved/com/github/atais/entity/EntityB.class
[debug]         com/github/atais/entity/EntityA.class
[debug]           /home/atais/Documents/sbt-eclipselink-static-weave/sbt-test/target/scala-2.12/classes-weaved/com/github/atais/entity/EntityA.class

SBT is not restarted between this runs. It's just second time I call package.

How could I produce proper jar on first run?


Solution

  • OK it turned out I should have used

    productDirectories in Compile := Seq(weavedClassesDest.value),
    

    instead of

    products in Compile := Seq(weavedClassesDest.value),
    

    and now it seems to be working