Search code examples
scalasbtsbt-assembly

How to sequence sbt-assembly before custom task


I would like to define a custom task in my project build.sbt file. The assembly task from the sbt-assembly plugin returns the filename of each assembled jar. The custom task should wait until the assembly task has completed and use the list of file names to execute a shell script.

Here's an idea of what I'm trying to achieve

lazy val listOfFiles = taskKey[Unit]("Run script passing jar file names as parameters.")

listOfFiles := {
    val files = List[File]()
    files :+ assembly
    // run script passing file names as parameters
} 

I have tried using Def.sequential but I don't think it's suitable for the requirements of my custom task. Any help on how to create a custom task like this is much appreciated!


Solution

  • After spending hours in researching sbt and searching documentation online, I have discovered that I was thinking of the problem in a slightly incorrect way.

    The solution I have come up with is to define the task like this:

    lazy val listOfFiles = taskKey[Unit]("Run script passing jar file names as parameters.")
    
    listOfFiles := {
        val files = assembly.all(ScopeFilter(inProjects(project1, project2, project3))).value
        files.map(x => println("jar file: " + x))
        // run script passing file names as parameters
    } 
    

    The listOfFiles task above will run the assembly task in the scope of project1, project2, project3 and return a sequence of file names. This is exactly what I was trying to achieve. Hope this helps someone.