jay
jay

Reputation: 53

Deploy jar files from multi-project gradle build to destination directory

I have a multi-project gradle build in which I want to deploy all the jars that are built from the multiple projects into a destination directory and just can't figure out how to do it exactly.

The directory structure is pretty standard:

.
├── project1
│   ├── build
│   │   └── libs
│   │       └── project1.jar
│   └── src
│       └── main
│           ├── java
│           └── resources
├── project2
│   ├── build
│   │   └── libs
│   │       └── project2.jar
│   └── src
│       └── main
│           ├── java
│           └── resources
├── build.gradle
├── gradle.properties
└── settings.gradle

I need to get project1.jar & project2.jar copied into a destination directory.

Another problem is that the number of projects will continue to grow. So in a few weeks there will likely be a project3.jar. It would be nice if I didn't have to update anything to get its jar files included in the copy as well (other than editing my settings.gradle file to include project3 in the build).

Just copying ./*/build/libs/*.jar to some destination directory would work for me I think, I just don't know how to do that.

Thanks for any help!

Upvotes: 3

Views: 4375

Answers (2)

Opal
Opal

Reputation: 84874

You can just add the following piece of code to build.gradle in project1 and project2:

jar {
   destinationDir project.file('../dest')
}

EDIT (after discussion in comments)

You need to add the following piece of code to root build.gradle file

subprojects {
   apply plugin: 'java'
}

task copyFiles(type: Copy, dependsOn: subprojects.jar) {
   from(subprojects.jar)
   into project.file('dest')
}

Upvotes: 4

Matthias Braun
Matthias Braun

Reputation: 34413

Just as a supplement to Opal's answer:

If you want to copy only a subset of your subprojects' jars you can apply a filter like this:

/** Returns true if a project has a source directory */
def hasSrc(proj){
    return new File(proj.projectDir, "src").exists()
}

/** Copies the jars of the subprojects to a dir of the umbrella project */
task copyJars(type: Copy, dependsOn: subprojects.jar) {
        // Skip empty subprojects
        from(subprojects.findAll{hasSrc(it)}.jar)
        into project.file("$buildDir/jars")
}
run.dependsOn copyJars

Upvotes: 1

Related Questions