Skip to content

Instantly share code, notes, and snippets.

@RichardBradley
Last active August 13, 2017 19:59
Show Gist options
  • Save RichardBradley/5384a5e0da2427df237f42fe512b30b8 to your computer and use it in GitHub Desktop.
Save RichardBradley/5384a5e0da2427df237f42fe512b30b8 to your computer and use it in GitHub Desktop.
q43195989

q43195989 SBT how to publish an artifact generated from the run command

This project is a demo for

https://stackoverflow.com/questions/43195989/sbt-how-to-publish-an-artifact-generated-from-the-run-command

Run

sbt publishLocal

to generate a jar which contains a zip file that was generated by the ZipGeneratorMainClass class from the project's main code. (The class is in this dir rather than src/main, so that this project will fit in a Github Gist.)

Run:

sbt run

to see the "MainClass" output: "Output from main class"

val myZipTask = taskKey[File]("return the swagger-zip file")
val runZipCodeTask = taskKey[Unit]("run the swagger-zip code")
// See http://www.scala-sbt.org/0.13.2/docs/faq.html, “How can I create a custom run task, in addition to run?”
// and https://stackoverflow.com/questions/23409993/defining-sbt-task-that-invokes-method-from-project-code
fullRunTask(runZipCodeTask, Compile, "ZipGeneratorMainClass")
myZipTask := {
runZipCodeTask.value
file("target/swagger/swagger.zip")
}
addArtifact(Artifact("swagger", "zip", "zip"), myZipTask)
mainClass in Compile := Some("MainClass")
object MainClass {
def main(args: Array[String]): Unit = {
println("Output from main class")
}
}
import java.io.File
import java.nio.file.Paths
import java.nio.file.Files
import java.nio.charset.StandardCharsets;
object ZipGeneratorMainClass {
def main(args: Array[String]): Unit = {
val target = new File("target/swagger/swagger.zip");
Files.createDirectories(target.getParentFile.toPath);
Files.write(
target.toPath,
"example zip file contents generated by ZipGeneratorMainClass"
.getBytes(StandardCharsets.UTF_8))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment