Include generated resources in the bank (SBT)

I wrote an SBT plugin that generates resources in resource_managed . Now I want to include these generated resources in the generated jar in the form of detailed SBT documentation:

Resource Creation :

By default, generated resources are not included in the packaged source artifact. To do this, add them, as well as other mappings. See Adding Files to a Package.

I read the documents, but honestly, I can’t understand how to do this. Can someone explain this or point me to another project that does this so that I can see how they do it?

+8
scala jar sbt
source share
1 answer

At first, just to clarify, they are included in banks containing compiled classes. They are not included in banks containing sources.

By default, generated resources are not included in the packaged source artifact .

For packageBin generated files should already be included - just make sure that you return all the generated files from the generator method. Assuming you want to pack them into an artifact of sources, this is what you need to do.

Suppose you have a generator that generates a properties file.

 lazy val generatePropertiesTask = Def.task { val file = (resourceManaged in Compile).value / "stack-overflow" / "res.properties" val contents = s"name=${name.value}\nversion=${version.value}" IO.write(file, contents) Seq(file) } resourceGenerators in Compile += generatePropertiesTask.taskValue 

To include this in the generated sources, you must tell sbt where res.properties should be copied to the generated artifact source. The task that generates packaged sources is called packageSrc , so you need to set mappings to this task .

 mappings in (Compile, packageSrc) += { ((resourceManaged in Compile).value / "stack-overflow" / "res.properties") -> "path/in/jar/res.properties" } 

Since your generator can generate many tasks, and displaying each of them will be a tedious task, sbt provides you with a utility to map multiple paths at once.

 mappings in (Compile, packageSrc) ++= { val allGeneratedFiles = ((resourceManaged in Compile).value ** "*") filter { _.isFile } allGeneratedFiles.get pair relativeTo((resourceManaged in Compile).value) } 

The first line finds all the generated files using the path finders , and the second line maps them to its path in the target bank.

+9
source share

All Articles