Is it possible to remove systemProperties from the input properties of a Test task?


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'systemProperties')

(The jettyHttp socket has a random port, created by

ServerSocket socket = new ServerSocket(0)


Thanks in advance!

There’s a hacky workaround for this. If you set the property in a doFirst() block, it won’t be considered as an input.

test {
  systemProperty “i-am-an-input”, “foo”
  doFirst {
    systemProperty “i-am-not-an-input”, “bar”

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 {
  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 << { {
  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… :frowning:

Ah, you’d have to remove the system property in a doLast() block. 

I did say it was a hack.

Oh, I see :slight_smile: 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:

test.doLast {'extraProp')

Although it is a bit hacky, I can live with this solution for now :slight_smile: Thanks for the very quick response!