简体   繁体   中英

How do I make an alias for a Gradle task?

As a part of a TDD workflow, I want to be able to check if my Java codebase compiles, but not if the tests pass.

Currently, if I run gradle build it runs the compile tasks (for source and tests) and then also executes the test task (and returns a non-zero exit code since the tests fail).

So I find that I have to run gradle build -x test to exclude the test task, and get a successful zero exit code.

What do I add to my build.gradle to define a new task, say compile that is an alias for build x test ?

So far I have this, but it doesn't seem like dependsOn takes any arguments to customize the build task I want to execute:

task compile {
    dependsOn build
}

I've been reading the docs here , I see different kinds of dependency chaining mechanisms, but not to disable/exclude a particular task. How does the -x flag even work then? I assumed there would be a way to control it programmatically too.


Thanks to Bjørn Vester's answer and reading the docs, I have implemented my task as follows:

task compile {
    dependsOn classes
    dependsOn testClasses
}

There are lots of different tasks you can run individually. For instance:

  • gradle classes : Will compile your "main" code.
  • gradle testClasses : Will compile your "main" code as well as test code.
  • gradle jar : Will compile your "main" code and make an executable of it.

None of the above will run your unit tests. On the other hand, the build task depends on all of the above, as well as the test task and more.

In general, if you like to run a particular set of tasks, you do that by defining a new task and then make dependencies to those other tasks you like to run with it. You tried that already, but instead of build you should have used something like compileJava or classes whatever other tasks you need. But always check if there isn't one already that satisfies your needs, like there are in this case. You can read about what tasks are available in Java projects in the documentation for the Gradle java plugin .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM