Skip to content

Instantly share code, notes, and snippets.

@arjanblokzijl
Created August 4, 2011 18:33
Show Gist options
  • Save arjanblokzijl/1125865 to your computer and use it in GitHub Desktop.
Save arjanblokzijl/1125865 to your computer and use it in GitHub Desktop.
sbt all artifacts
import sbt._
import Keys._
import Defaults._
//adding the Artifacts.settings to you project settings will create and publish the artifact defined as 'theMainArtifact'
object Artifacts {
val allTheArtifacts = TaskKey[Seq[(sbt.Artifact, java.io.File)]]("all-artifacts")
val theMainArtifact = TaskKey[File]("zip-artifact", "Create zip with all library dependencies")
val artifactConf = config("Artifact") hide //use this to scope the create artifact to this config
lazy val settings = inConfig(artifactConf)(Seq(
allTheArtifacts <<= (thisProjectRef, buildStructure, streams) flatMap allArtifactsTask,
theMainArtifact <<= artifactsTask,
artifact in artifactConf <<= (name, version)((n,v) => Artifact(n + "-" + v, "zip", "zip")) //initialize with the project name and version, as an example
)) ++ addArtifact(artifact in artifactConf, theMainArtifact in artifactConf) ++ Seq(
theMainArtifact in artifactConf <<= theMainArtifact in artifactConf dependsOn(packageBin in Compile)
) //add the artifact to published artifacts
def allArtifactsTask(projectRef: ProjectRef, structure: Load.BuildStructure, s: TaskStreams): Task[Seq[(sbt.Artifact, java.io.File)]] = {
val projects = dependencies(projectRef, structure, s)
s.log.info("found projects %s".format(projects))
projects flatMap {ref => packagedArtifact in Compile in ref.project in packageBin get structure.data } join
}
def dependencies(projectRef: ProjectRef, structure: Load.BuildStructure, s: TaskStreams): Seq[ProjectRef] = {
val deps = Project.getProject(projectRef, structure).toSeq.flatMap(_.dependencies)
s.log.info("got deps %s for project ref %s".format(deps, projectRef))
deps flatMap { ref => ref.project +: dependencies(ref.project, structure, s)}
}
def artifactsTask = (allTheArtifacts in artifactConf, target in Compile, streams) map {
(arts, t, s) => {s.log.info("executing task with artifacts %s".format(arts.map(a => a._2)))}
val result = new File(t, "myFile") //just create a simple file in the target directory for demo purposes
result.createNewFile()
result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment