I need to pass a random port number to a Test task in a system property, but it ruins the incremental behaviour of the test. I can see that systemProperties are listed in the input properties of the task, so I understand why it is (almost) never up-to-date. Is there any way to remove all system properties (or just a property with a specific name) from the inputs of a task? Because a test should still be up-to-date for me if only the port number of its web container changes.
I tried the following, but it does not do anything (using Gradle 2.1):
test {
systemProperty 'jettyHttpPort', project.sockets.jettyHttp.localPort
test.inputs.properties.remove('systemProperties')
}
(The jettyHttp socket has a random port, created by
Unfortunately it does not solve the incremental problem for me. Indeed, the “i-am-not-an-input” property does not show up in the input properties. But if I set a system property in test.doFirst, the test task will never be up-to-date.
Here is a small working code that I created. First, I generated a project with “gradle init --type java-library”, I removed the comments and added “test” settings and the “debug” task.
apply plugin: 'java'
repositories {
jcenter()
}
dependencies {
compile 'org.slf4j:slf4j-api:1.7.5'
testCompile 'junit:junit:4.11'
}
test {
systemProperty 'foo', 'bar'
}
test.doFirst {
systemProperty 'extraProp', '10' // does not even have to be a changing value
}
task debug << {
tasks.test.inputs.properties.systemProperties.each {
println it
}
}
If I call “gradle debug”, I only see the “foo” property. But if I call “gradle build” several times, the “test” task is always executed, and never up-to-date.
If I call “gradle build --debug” then I can see that the systemProperties have changed.
07:59:07.093 [INFO] [org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter] Executing task ‘:test’ (up-to-date check took 0.091 secs) due to:
Value of input property ‘systemProperties’ has changed for task ‘:test’
So I still don’t understand incremental behaviour in this case…
Oh, I see The input hashes before and after running the test task were not the same, because I explicitly added a new property after the first hash generation. But if I also remove that property before the task finishes, the after-task hash will be the same as the before-task hash. And this really helps to fix the incremental build.
Just for the record, here is the part that was missing: