Robert
Robert

Reputation: 8683

Why does Gradle always call an ant task first?

I use the OWASP Dependency Check from its ant task (no Gradle support yet) like this:

task checkDependencies() {
    ant.taskdef(name: 'checkDependencies',
        classname: 'org.owasp.dependencycheck.taskdefs.DependencyCheckTask',
        classpath: 'scripts/dependency-check-ant-1.2.5.jar')
    ant.checkDependencies(applicationname: "MyProject",
        reportoutputdirectory: "generated",
        dataDirectory: "generated/dependency-check-cache") {
        fileset(dir: 'WebContent/WEB-INF/lib') {
            include(name: '**.jar')
        }
    }
}

This works way too good. Even though nothing defines this ant task as dependency (neither in ant nor in Gradle), it is always executed first, even for a simple gradlew tasks. Why is that and how can I avoid this? (The dependency check is quite slow.)

Upvotes: 0

Views: 186

Answers (1)

Mark Vieira
Mark Vieira

Reputation: 13476

This is a very common confusion with Gradle. In your example above you are executing the Ant tasks during project configuration. What you really intended was for it to run during task execution. To fix this, your execution logic should be placed within a task action, either by using a doLast {...} configuration block or using the left shift (<<) operator.

task checkDependencies << {
    // put your execution logic here
}

See the Gradle docs for more information about the Gradle build lifecycle.

Upvotes: 2

Related Questions