Using @Option outside tasks

Hi to all,

we use in many scenarois the @Option annotation to delegate some parameters to our tasks. At one implementation I have a little problem:

We have the necessity to do some actions before running the integrationtests (e.g. creating database structure, import testdata etc.) This we have implemented by configuring a test task for our needs and add some pre and post actions.
(doFirst and doLast). This things are recommended when running the tests the first time. All the other test runs they are obsolete. So we want to invent a commandline option like “–skipPre” or “–skipPost” to skip these things. How can I access the information if this command line parameter is used?

Any help would be welcome

Just turn your doFirst/doLast actions into separate tasks and use task dependencies.


task preTask
task postTask
task test 
test.dependsOn preTask
test.finalizedBy postTask

Then you can either make preTask incremental (so it knows not to run), use onlyIf to force the preTask to run, or you can skip the preTask with -x (gradle test -x preTask).

If the tasks don’t have to run before the test (the test would work without ever running preTask), you could also just use test.mustRunAfter preTask and then it would only run if someone explicitly asks for it (gradle test preTask).