How to express the artifact built by a project

I have recently had the need to express “the artifact built by this project” a couple of times:

I wanted to start a functional test tool (Fitnesse) with the newly built jar on the classpath. I ended up doing this:

libsDir.list().each { fileName ->
        builder.append("!path ${libsDir.absolutePath}/${fileName}\n")
}

I also got the requirement to collect the artifacts from a number of sub projects into a common directory. I imagine I could solve it in a similar fashion.

However, I have the feeling that this should be possible to express in a more elegant way, not having to go through the libsDir.

You can get hold of artifact files built by a project via:

configurations.archives.artifacts.files

See documentation of the artifacts property in dsl reference for ‘Configuration’ for details.

If you want to collect a number of artifacts from subprojects then it’s best to do it via a custom configuration and a ‘Copy’ task - this way task dependencies for building these rtifacts will be set up for you:

configurations {
    subProjectsArtifacts
}
  dependencies {
    subProjectsArtifacts project(path: ":subproject1", configuration: "archives")
    subProjectsArtifacts project(path: ":subproject2", configuration: "archives")
}
  task copySuprojectArtifacts(type: Copy) {
    from configurations.subProjectsArtifacts
    into "some/path/here"
}

Note that this will only copy the artifacts produced by a project and not project dependencies. If you want to copy dependencies as well then don’t specify a configuration when defining project dependencies.

Thanks for the great answer!