How to override inputs and outputs of a dynamically created plugin task?


(Michal S) #1

Hi there. I’m using gradle-node-plugin to trigger yarn build in my frontend subproject. The only trouble is that the dynamically created plugin task yarn_build does not declare any inputs and outputs, so it’s always run. Is there any easy way to override these for this specific task?


(Alexander Volanis) #2

This is one of the things Gradle does best.
No matter how a task is created it is always possible to configure it externally.

If I understand you correctly this plugin creates a task named yarn_build. If this name is fixed the configuration is simply this:

yarn_build {
    inputs.files fileTree('src/nodestuff')
    outputs.dir file('@node_modules/mypackagename')
}

This is from a basic understanding of the way node works. the file method creates a java File so do not mistake this to imply this is a single file, it may be a directory as well as a file.

One point that you must always be sure to remember is that the inputs must not be altered between task executions that have no source changes. So, if somehow your yarn build is dropping build artifacts inside your source tree you have a little more work to teach Gradle to ignore these generated artifacts.

You would have to specifically exclude the generated files leaving only pure source files in the input fileTree.

   inputs.files fileTree('src/nodestuff').matching {
                             exclude 'generated/**'
                       }

where generated is a subdirectory within src/nodestuff


(Michal S) #4

Hi Alex, that worked for me. Thanks, much appreciated!


(Michal S) #5

@Alex_Vol do you know if it’s possible to override path sensitivity (relative, absolute, etc…) for inputs and outputs defined in such a way?

I’m struggling with incremental builds on Heroku. One thing they do is they always create a new directory for the sources, e.g. /tmp/build_f38dde09192aab3f0652a52d139bea45. I reinstate some directories from Heroku build cache like so:

remote: Linking /app/tmp/cache/.gradle-additional-dirs/build to /tmp/build_f38dde09192aab3f0652a52d139bea45/build
remote: Linking /app/tmp/cache/.gradle-additional-dirs/src/main/frontend/node_modules to /tmp/build_f38dde09192aab3f0652a52d139bea45/src/main/frontend/node_modules
remote: Linking /app/tmp/cache/.gradle-additional-dirs/src/main/frontend/dist to /tmp/build_f38dde09192aab3f0652a52d139bea45/src/main/frontend/dist 

Somehow this is enough to convince Java compileTask that everything is up to date. I understand how that works for inputs, as from what I know the compile task only looks at file names. (not sure how that works for the outputs). However for my yarn tasks I’m seeing:

remote:        > Task :frontend:yarn_install
remote:        Task ':frontend:yarn_install' is not up-to-date because:
remote:          Input property '$1' file /tmp/build_07c973b110452fc1ab45d380b2b8f83c/src/main/frontend/package.json has been removed.
remote:          Input property '$1' file /tmp/build_f38dde09192aab3f0652a52d139bea45/src/main/frontend/package.json has been added.
remote:        Starting process 'command '/app/tmp/cache/gradle-build/yarn/yarn-v1.9.4/bin/yarn''. Working directory: /tmp/build_f38dde09192aab3f0652a52d139bea45/src/main/frontend Command: /app/tmp/cache/gradle-build/yarn/yarn-v1.9.4/bin/yarn install
remote:        Successfully started process 'command '/app/tmp/cache/gradle-build/yarn/yarn-v1.9.4/bin/yarn''
remote:        yarn install v1.9.4
remote:        [1/4] Resolving packages...
remote:        success Already up-to-date.
remote:        Done in 1.10s.
remote:        :frontend:yarn_install (Thread[main,5,main]) completed. Took 9.565 secs.
remote:        :frontend:yarn_build (Thread[Task worker for ':',5,main]) started.
remote:
remote:        > Task :frontend:yarn_build
remote:        Task ':frontend:yarn_build' is not up-to-date because:
remote:          Output property '$1' file /tmp/build_07c973b110452fc1ab45d380b2b8f83c/src/main/frontend/dist/result has been removed.
remote:          Output property '$1' file /tmp/build_07c973b110452fc1ab45d380b2b8f83c/src/main/frontend/dist/result/css has been removed.
remote:          Output property '$1' file /tmp/build_07c973b110452fc1ab45d380b2b8f83c/src/main/frontend/dist/result/css/app.7542f025.css has been removed.

Does it mean that in order to fix this I would need to change the sources of these dynamically created yarn tasks?