Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make gradle compileJava dependsOn spotlessApply

Tags:

java

gradle

I have a Gradle project with several submodules. In my project there is a spotless task configured. Now I want to make a compileJava task dependent on spotlessApply task. I try it in this way:

subprojects {
    apply plugin: 'java'
    apply plugin: 'com.diffplug.gradle.spotless'

    spotless {
        java {
            target 'src/**/*.java'
            licenseHeaderFile  "$rootDir/buildSrc/CopyrightHeader.java"
            importOrder(['java', 'javax', 'org', 'com'])
            eclipseFormatFile "$rootDir/buildSrc/formatter.xml"
        }
        format 'misc', {
            target 'src/**/*.md', 'src/**/*.xml', 'src/**/*.xsd', 'src/**/*.xsl'
            indentWithSpaces()
            trimTrailingWhitespace()
            endWithNewline()
        }
    }

    compileJava.dependsOn spotlessApply
}

But it produces an error:

Could not get unknown property 'spotlessApply' for project (...) of type org.gradle.api.Project.

I also tried something like this:

compileJava.dependsOn project.tasks.findByName('spotlessApply')

But it doesn't work.

like image 922
wojtek1902 Avatar asked Aug 22 '18 06:08

wojtek1902


People also ask

How do you use spotless in gradle?

An IntelliJ plugin to allow running the spotless gradle task from within the IDE on the current file selected in the editor. You may find the spotless action via Code > Reformat Code with Spotless.

How does dependsOn work gradle?

dependsOn(jar) means that if you run assemble , then the jar task must be executed before. the task transitive dependencies, in which case we're not talking about tasks, but "publications". For example, when you need to compile project A , you need on classpath project B , which implies running some tasks of B .

What is compileJava in gradle?

compileJava — JavaCompile. Depends on: All tasks which contribute to the compilation classpath, including jar tasks from projects that are on the classpath via project dependencies. Compiles production Java source files using the JDK compiler.

How do I run spotless?

Running Spotless Spotless can be ran using ./gradlew spotlessApply which will apply all formatting options. You can also specify a specific task by just adding the name of formatter. An example is ./gradlew spotlessmiscApply . Spotless can also be used as a CI check.


1 Answers

The Spotless plugin creates its tasks in an project.afterEvaluate block to allow you to configure the extension before it creates the task(s) - see here

To solve this, simply depend on the task's name (i.e. as a string) instead and Gradle will resolve the task when its needed.

compileJava.dependsOn 'spotlessApply'
like image 146
Raniz Avatar answered Oct 11 '22 20:10

Raniz