Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin should not default to using overlapping task outputs #212

Open
Fiouz opened this issue Aug 3, 2022 · 0 comments
Open

Plugin should not default to using overlapping task outputs #212

Fiouz opened this issue Aug 3, 2022 · 0 comments

Comments

@Fiouz
Copy link

Fiouz commented Aug 3, 2022

While working on #209 to make use of inferred task dependencies on the processResources task (instead of depending on the dependent classes task), it seems there is an issue with the default location with regards to recommended practices(1):

(NOTE: By default, the file will be generated at `build/resources/main/git.properties`)

private static final String DEFAULT_OUTPUT_DIR = "resources/main"

return layout.buildDirectory.dir(DEFAULT_OUTPUT_DIR).get()

By default, that task is using the same output location as the Java plugin: build/resources/main.

(1) However, as documented at

... this is not a recommended practice (I guess this is the root cause of #128/#131).

I think this is not detected by the existing unit test because of a combination of:

If I "fix" #209 with inferred task dependencies, and modify the unit test as follows:

Tentative fix for #209
Index: src/main/groovy/com/gorylenko/GitPropertiesPlugin.groovy
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/main/groovy/com/gorylenko/GitPropertiesPlugin.groovy b/src/main/groovy/com/gorylenko/GitPropertiesPlugin.groovy
--- a/src/main/groovy/com/gorylenko/GitPropertiesPlugin.groovy	(revision 80fd874532d027bc74e5b2e7acea3b1abfd3d577)
+++ b/src/main/groovy/com/gorylenko/GitPropertiesPlugin.groovy	(date 1659503616239)
@@ -3,14 +3,11 @@
 import groovy.transform.ToString
 import org.gradle.api.Plugin
 import org.gradle.api.Project
-import org.gradle.api.file.Directory
 import org.gradle.api.file.DirectoryProperty
-import org.gradle.api.file.ProjectLayout
 import org.gradle.api.plugins.BasePlugin
 import org.gradle.api.plugins.JavaPlugin
 import org.gradle.api.tasks.InputDirectory
-import org.gradle.api.tasks.SourceSet
-import org.gradle.api.tasks.SourceSetContainer
+import org.gradle.language.jvm.tasks.ProcessResources
 
 class GitPropertiesPlugin implements Plugin<Project> {
 
@@ -27,31 +24,11 @@
         // if Java plugin is applied, execute this task automatically when "classes" task is executed
         // see https://guides.gradle.org/implementing-gradle-plugins/#reacting_to_plugins
         project.plugins.withType(JavaPlugin) {
-            project.tasks.named(JavaPlugin.CLASSES_TASK_NAME).configure {
-                dependsOn(task)
-
-                // if Java plugin is used, this method will be called to register gitPropertiesResourceDir to classpath
-                // at the end of evaluation phase (to make sure extension values are set)
-                if (extension.gitPropertiesResourceDir.present) {
-                    String gitPropertiesDir = getGitPropertiesDir(extension, project.layout).asFile.absolutePath
-                    def sourceSets = project.extensions.getByType(SourceSetContainer)
-                    sourceSets.named(SourceSet.MAIN_SOURCE_SET_NAME).configure {
-                        it.resources.srcDir(gitPropertiesDir)
-                    }
-                }
+            project.tasks.named(JavaPlugin.PROCESS_RESOURCES_TASK_NAME, ProcessResources).configure {
+                it.from(task)
             }
         }
     }
-
-    private static Directory getGitPropertiesDir(GitPropertiesPluginExtension extension, ProjectLayout layout) {
-        if (extension.gitPropertiesResourceDir.present) {
-            return extension.gitPropertiesResourceDir.get()
-        } else if (extension.gitPropertiesDir.present) {
-            return extension.gitPropertiesDir.get()
-        } else {
-            return layout.buildDirectory.dir(DEFAULT_OUTPUT_DIR).get()
-        }
-    }
 }
 
 @ToString(includeNames=true)
Build cache unit test changes
Index: src/test/groovy/com/gorylenko/BuildCacheFunctionalTest.groovy
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/test/groovy/com/gorylenko/BuildCacheFunctionalTest.groovy b/src/test/groovy/com/gorylenko/BuildCacheFunctionalTest.groovy
--- a/src/test/groovy/com/gorylenko/BuildCacheFunctionalTest.groovy	(revision 80fd874532d027bc74e5b2e7acea3b1abfd3d577)
+++ b/src/test/groovy/com/gorylenko/BuildCacheFunctionalTest.groovy	(date 1659504348719)
@@ -32,8 +32,9 @@
 
         def runner = GradleRunner.create()
                 .withPluginClasspath()
-                .withArguments("generateGitProperties", "--build-cache")
+                .withArguments("assemble", "--build-cache", '--info')
                 .withProjectDir(projectDir)
+                .forwardOutput()
 
         def firstResult = runner.build()
         assertEquals(TaskOutcome.SUCCESS, firstResult.task(":generateGitProperties").outcome)

... the test fails because the task is not cached, and the following warning can be seen in the build output:

Caching disabled for task ':generateGitProperties' because:
  Gradle does not know how file 'build\resources\main\git.properties' was created (output property 'output'). Task output caching requires exclusive access to output paths to guarantee correctness (i.e. multiple tasks are not allowed to produce output in the same location).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant