Gradle Java Plugin - Copy task with multiple froms

(mvm) #1


I am writing a Gradle plugin in Java to help with the distribution of our pipeline tasks.
Currently, I’ve got everything migrated and working as expected except for the Copy task. I am not sure what would be the proper way to configure multiple from in it, as with the configuration below it is not picking up both files.

public class StageApp extends Copy {

  private final CommonProperties commonProperties;

  public StageApp() {
    this.commonProperties = getProject().getExtensions().getByType(CommonProperties.class);


  private void configureTask() {
    from(getProject().getBuildDir(), (copySpec) -> {
      rename((it) -> getProject().getName() + ".jar");

        getProject().fileTree(getProject().getProjectDir() + "/config/" + commonProperties.getEnv())
            .getFiles(), (copySpec -> include("configuration.yml")));

    into(getProject().getRootDir() + "/docker/" + getProject().getName());

At the same time, this is working while having it as a task:

task stageApp(type: Copy, dependsOn: fetchArtifact) {
    from(buildDir) {
        include "*.jar"
        rename { "${}.jar" }
    from(fileTree("${projectDir}/config/${env}").files) {
        include "configuration.yml"

Any idea of what am I missing here?

(Chris Doré) #2

When the plugin creates a task of type StageApp, what is the value of commonProperties.getEnv()? Are you creating the task in the plugin before this value is configured?

(mvm) #3

Hello Chris and thanks for your answer,

The value is, in this particular case, a String with the content local. This is initialized before creating the plugin (on the parent plugin).
As an interesting note, if I were to comment out the first from section, the second one executes successfully, no matter if I switch their order or not.