Advertisement
Guest User

sbt dependencies artifacts

a guest
Aug 5th, 2011
665
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.31 KB | None | 0 0
  1. import sbt._
  2. import Keys._
  3. import Defaults._
  4.  
  5. /**
  6.  * Provides a way to obtain the location of the main project artifact, and the artifacts of all its dependencies.
  7.  */
  8. object Artifacts {
  9.   val allTheArtifacts = TaskKey[Seq[(sbt.Artifact, java.io.File)]]("all-artifacts")
  10.   val theMainArtifact = TaskKey[File]("main-artifact", "The main custom artifact that should be published")
  11.   val artifactConf = config("Artifact") hide //use this to scope the create artifact to this config
  12.  
  13.   lazy val settings = inConfig(artifactConf)(Seq(
  14.     allTheArtifacts <<= (thisProjectRef, buildStructure, streams) flatMap allArtifactsTask,
  15.     theMainArtifact <<= artifactsTask,
  16.     artifact in artifactConf <<= (name, version)((n,v) => Artifact(n + "-" + v, "zip", "zip")) //create the Artifact with project name and version, and zip type
  17.   )) ++ addArtifact(artifact in artifactConf, theMainArtifact in artifactConf) ++ Seq(
  18.     theMainArtifact in artifactConf <<= theMainArtifact in artifactConf dependsOn(packageBin in Compile)
  19.   )
  20.  
  21.   def allArtifactsTask(projectRef: ProjectRef, structure: Load.BuildStructure, s: TaskStreams): Task[Seq[(sbt.Artifact, java.io.File)]] = {
  22.     val projects = dependencies(projectRef, structure, s)
  23.     s.log.info("found projects %s".format(projects))
  24.     projects flatMap {ref => packagedArtifact in Compile in ref.project in packageBin get structure.data } join
  25.   }
  26.  
  27.   /**
  28.    * Gets the given project's dependencies. This is done recursively, so the dependencies of a dependency are also part of the result.
  29.    * The result may contain duplicates, these are not filtered out at the moment.
  30.    */
  31.   def dependencies(projectRef: ProjectRef, structure: Load.BuildStructure, s: TaskStreams): Seq[ProjectRef] = {
  32.     val deps = Project.getProject(projectRef, structure).toSeq.flatMap(_.dependencies)
  33.     s.log.info("got deps %s for project ref %s".format(deps, projectRef))
  34.     deps flatMap { ref => ref.project +: dependencies(ref.project, structure, s)}
  35.   }
  36.  
  37.   def artifactsTask = (allTheArtifacts in artifactConf, target in Compile, streams) map {
  38.       (arts, t, s) => {s.log.info("executing task with artifacts %s".format(arts.map(a => a._2)))}
  39.       val result = new File(t, "myFile") //just create a simple file in the target directory for illustration
  40.       result.createNewFile()
  41.       result
  42.   }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement