How do I run a custom task before the "build" gradle? - gradle

How do I automatically run a custom task before gradle build in build.gradle

Related

gradle: Task :jar SKIPPED while I get my jar with gradlew build

My Question is: Why does the jar-creation work with gradlew build, while I see "Task :jar SKIPPED" when I click on jar in intellij's gradle window ? And how can I fix it in IntelliJ ?
Just created something with spring initializer and loaded the project in intellij as it is.
( it is org.springframework.boot, .. 'org.springframework.boot:spring-boot-starter-web')
I wonder about Task :jar SKIPPED ( nor jar created )
and than I discovered that I get the jar when I start from console.
( and the jar runs fine, it finds the main class - even without jar manifest attribute in build.gradle)
( yesterday I failed in maven with "no main manifest attribute in .... .jar )
This is because Springboot Gradle plugin will create a bootJar task and by default will disable jar and war tasks, as described here: https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/html/#packaging-executable-and-normal
So you need to execute bootJar task , from the IDE. When executing gradlew build, the tasks bootJar gets automatically executed, due to tasks dependencies created by the plugin.
When running task build (from console or IDE), you can see the tasks executed by Gradle depending on tasks dependencies, e.g.:
> Task :backend:compileJava
> Task :backend:processResources
> Task :backend:classes
> Task :backend:bootJar ## <== this is the task register by Springboot plugin, which produces the "Fat/executable" jar
> Task :backend:jar SKIPPED ## <== task disabled by Springboot plugin
> Task :backend:assemble
> Task :backend:processTestResources
> Task :backend:testClasses
> Task :backend:test
> Task :backend:check
> Task :backend:build
For your remark
the jar runs fine, it finds the main class - even without jar manifest
attribute in build.gradle
The Springboot plugin will automatically configure this for you, see : https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/html/#packaging-executable-configuring-main-class
EDIT 27-05-2021
Starting from Springboot 2.5, the jaris not disabled by default anymore. see more details in release notes here
You can enble this, with add code below in projectName.gradle
it works for me :
spring-boot : 2.0.8.RELEASE
Gradle : 4.5 or >
jar {
baseName = 'projectName'
enabled=true
manifest {
....
}
}

Execute a Task of a sub-module of a Gradle multiproject

I have the following multiproject structure:
settings.gradle
rootProject.name = 'toolbox-backend'
include 'toolbox-components-rest'
include 'toolbox-components-executor'
include 'toolbox-components-toolsyncer'
I'd love to create a task in my root build.gradle which will call the clean, build, install (application) and finally the run task of the toolbox-components-rest submodule.
task startREST() {
dependsOn ':toolbox-components-rest:clean'
dependsOn ':toolbox-components-rest:build'
dependsOn ':toolbox-components-rest:bootRun'
println "[Toolbox $version] Starting REST interface..."
}
This does work - BUT the bootRun task is running before build which runs before clean. I'd like to have it exactly the other way around
Fixed the above with
bootRun.mustRunAfter build
build.mustRunAfter clean
in the gradle.build of the toolbox-components-rest submodule

Execute task with build task

I have a traditional java project using gradle build system.
I would like to add jacoco code coverage report generation. So I applied jacoco plugin and everything works as expected when I call gradle build jacocoTestReport
I was wondering how can I define in my build.gradle script that jacocoTestReport task should run automatically after buildtask has been finished.
The goal is to only run gradle build from command line and this will automatically execute test and jacocoTestReport (so that I don't have to pass jacocoTestReport as commandline parameter explicitly).
I would suggest
build.finalizedBy(jacocoTestReport)
This way, the jacocoTestReport task is only executed after the build task, as you specified. In the accepted answer, the build task depends on the test report task, which means build will be executed after your custom task.
Add this to the end of your buildscript
build.dependsOn jacocoTestReport

Gradle - run testReport task after test phase of all submodules

I have a simple project with subprojects and I want to generate aggregate report for all tests when I execute gradle test command.
I have followed the gradle documentation and added following:
task testReport(type: TestReport) {
// make sure this task is run after all subproject test tasks
mustRunAfter subprojects*.test
destinationDir = file("$buildDir/reports/allTests")
// Include the results from the `test` task in all subprojects
reportOn subprojects*.test
}
This works when I execute gradle test testReport, but when I execute gradle test or gradle build in the root project - the task testReport is not run.
How do make gradle to run the task without specifying it every time?
Add: test.finalizedBy 'testReport' to your build.gradle; just at the root level, doesn't have to be inside any closure.
taskX.finalizedBy taskY
Will run taskY everytime taskX completes execution successfully.

How do I run or create a single Gradle task to run all subprojects and root in a multi-project build?

I have multiple projects configured using build.gradle for each and settings.gradle at the top level.
I want to define or use a single Gradle task that will build all of the subprojects and the root.
How do I run or create a single Gradle task to run all subprojects and root in a multi-project build?
It depends on what all your projects are.
You can call gradle(w) build from the root dir if all your subprojects extend the javaplugin.
Other project types (like the ear plugin) need to be attached to the build task manually. The way I do this is by creating the build task like: task build and in the ear project: build.dependsOn ear

Resources