sbt-assembly icon indicating copy to clipboard operation
sbt-assembly copied to clipboard

Allow resources for assembly jar (only for assembly)

Open hochgi opened this issue 7 years ago • 9 comments

Use case:

Consider a project that serves as a library, but also provides a standalone tool packaged with sbt-assembly. Now, as a library, some resources must not be included in the main jar (e.g: application.conf,logback.xml,etc'...), And should be provided by the application that depends on the library. For the standalone tool however, we need to supply such files.

I couldn't figure out how to do so, and I suspect it's not possible (easily).

Ideally I would be happy with either dropping such resources under src/assembly/resources, or adding to build.sbt something like: resourceDirectory in assembly := sourceDirectory.value / "assembly". (or something equivalent)

hochgi avatar Jul 13 '17 08:07 hochgi

I think sbt-pack is better for this job

manuzhang avatar Jul 25 '17 07:07 manuzhang

I use sbt-pack to get a directory with jars in lib dir.
But sometimes, you'll need a single runnable jar.
(The "need" doesn't have to be technically... sometimes bureaucracy gets in the way...)

hochgi avatar Jul 25 '17 10:07 hochgi

as a library, some resources must not be included in the main jar

I'm confused here since you also said you need a single runnable jar. Also, I believe the resources files are assembled if they are under src/main/resources.

manuzhang avatar Jul 25 '17 11:07 manuzhang

Ok, my scenario is this: A multi project, with many subprojects. One module, is a dependency of other modules, but also provides a tool, packaged with assembly, to debug this module's functionality.

I can't have application.conf or logback.xml files in main/resources, since these will be pulled together with (in) the module's jar as a dependency for other modules.

But I do want to customize config & logging in the debugging tool.

hochgi avatar Jul 25 '17 12:07 hochgi

Try adding resourceDirectory in Compile := baseDirectory.value / "src" / "assembly" / "resources"

manuzhang avatar Jul 25 '17 12:07 manuzhang

But wouldn't it add the files there also to main jar?

hochgi avatar Jul 25 '17 13:07 hochgi

You may pass in a flag to only customize the resource directory when needed, like sbt -Ddebug=true assembly

      resourceDirectory in Compile := {
        if (System.getProperty("debug") == "true") {
          baseDirectory.value / "src" / "assembly" / "resources"
        } else {
          (resourceDirectory in Compile).value
        }
      }

manuzhang avatar Jul 27 '17 00:07 manuzhang

That's a nice workaround (Thanks! I'll probably use commands tt do it in a similar way to what I did here: https://gist.github.com/hochgi/e0dd854a8774414cd750 ). So, I'll use it for now, but I do think though, that the usecase is general enough, and that it would be beneficial to many to have it as part of the plugin functionality.

hochgi avatar Jul 27 '17 02:07 hochgi

Hello, we need the same feature as mentioned above: the ability to add a single file only during assembly. Functionality is very similar to maven assembly plugin and custom fileSet entry in assembly.xml:

<!-- We only want to include the logback.xml in the fat jar distribution. Not when any other project depends on this module. -->
<fileSets>
    <fileSet>
        <directory>src/main/assembly-resources</directory>
        <outputDirectory>/</outputDirectory>
    </fileSet>
</fileSets>

tdziurko avatar Sep 18 '17 15:09 tdziurko