GoogleContainerTools / jib

🏗 Build container images for your Java applications.
Apache License 2.0
13.7k stars 1.44k forks source link

Collect all output of a Gradle's SourceSetOutput.getDirs() #3642

Open jutoft opened 2 years ago

jutoft commented 2 years ago

Environment:

Description of the issue: A gradle sourceSet has a common output as sourceSets.main.output When an additional output directory is registered to the main source set then it should be part of the created image.

Expected behavior: the contents of $buildDir/generated/extraResources/ to be added to the jib image.

Steps to reproduce:

def generatorTask = tasks.register("generateExtraResources") {
    outputs.dir("$buildDir/generated/extraResources")
    doLast {
        file("$buildDir/generated/extraResources/some-file.properties").write "Hello"
    }
}

// the directory "$buildDir/generated/extraResources" is now registered as part of the output of the sourceset and would be added to the jar of a java project.
sourceSets.main.output.dir(generatorTask)

Log output:

Additional Information:

The issue seems to be the code here: https://github.com/GoogleContainerTools/jib/blob/86a3c855cb302da7671356dad7d2559fe3f138f5/jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/GradleProjectProperties.java#L223

// current code

      SourceSet mainSourceSet = getMainSourceSet();
      FileCollection classesOutputDirectories =
          mainSourceSet.getOutput().getClassesDirs().filter(File::exists);
      Path resourcesOutputDirectory = mainSourceSet.getOutput().getResourcesDir().toPath();
      FileCollection allFiles =
          project.getConfigurations().getByName(configurationName).filter(File::exists);

    ...
      switch (containerizingMode) {
        case EXPLODED:
          // Adds resource files
          if (Files.exists(resourcesOutputDirectory)) {
            javaContainerBuilder.addResources(resourcesOutputDirectory);
          }
    ...

// perhaps better approach

      SourceSet mainSourceSet = getMainSourceSet();
      FileCollection classesOutputDirectories =
          mainSourceSet.getOutput().getClassesDirs().filter(File::exists);
      FileCollection resourcesOutputDirectories = mainSourceSet.getOutput().minus(mainSourceSet.getClassesDirs()).filter(File::exists);

      ...
      switch (containerizingMode) {
        case EXPLODED:
          // Adds resource files
          for (File resourcesOutputDirectory: resourcesOutputDirectories) {
            javaContainerBuilder.addResources(resourcesOutputDirectory.toPath());
          }
      ...

mainSourceSet.getOutput() is a fileCollection by itself so the split is only relevant to get the two separate directories classes and resources in the image.

ddixit14 commented 2 years ago

Hi @jutoft, thanks for raising this issue. It does look like a valid approach, we'll consider this as a feature request and discuss about it. For now, the workaround you can set your eyes on is configuring jib.extraDirectories to copy additional output directory generated.

jutoft commented 2 years ago

Yes thank you.

I have added the directory using extraDirectories as a workaround. The build dependency is uphold as the folder does get the right dependency "hooks".

So yes this is a feature request :)

ddixit14 commented 2 years ago

Thanks, will look into this!