Gradle: Make task from custom plugin run first - plugins

I have a custom gradle plugin, say myPlugin, with a task, say myTask, that has to validate and create a file before the build starts. How do I make sure that gradle runs myTask before any other task from any other plugin?

You can't use a task for that. You can use a hook such as project.gradle.buildStarted { ... }.

Related

gradle include scala version in jar artifact name using kotlin DSL

How can I rename the produced jar by the gradle scala plugin using gradle build task to include the scala version name? I believe this property is called: archivesBaseName. It is made available from the java plugin (https://docs.gradle.org/current/dsl/org.gradle.api.Project.html#org.gradle.api.Project:archivesBaseName).
NOTE: I would want to accomplish this task using the kotlin DSL.
For the shadow jar:
id("com.github.johnrengelman.shadow") version "5.2.0" apply false
val shadowJar: com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar by tasks
shadowJar.apply {
archiveBaseName.set("${project.name}_2.12-all")
}
tasks {
build {
dependsOn(shadowJar)
}
already works fine, but I would want to change the regular output as well.
I.e.:
archivesBaseName = "asdf"//"${project.name}_${sparkVersion}_${scalaVersionShort}"
usually works. But I have not figured out how to get this to work in the Kotlin DSL
base.archivesBaseName = "gradle"
https://chromium.googlesource.com/external/github.com/gradle/gradle/+/cf6d0508131b0ff9d6354b3d46f83bcf5ca8f8a4/build.gradle.kts#45
gets the job done

How do I set up my JavaFX project as a Gradle build?

I've created a new JavaFX project via the e(fx)clipse plugin, then staged and committed to Git. Following that I tried to run it and got a 'class not found' error - needed to add JavaFX to the --module-path in VM arguments. But it still won't run:
Error: Could not find or load main class Files\Java\AdoptOpenJDK\javafx-sdk-13.0.2\lib
Caused by: java.lang.ClassNotFoundException: Files\Java\AdoptOpenJDK\javafx-sdk-13.0.2\lib
Then I noticed that I have no build files in the project tree - no pom.xml or build.gradle, etc.
I decided to build with Gradle (previously only used Maven). After a lot of hunting on the net I did the following:
Project context menu -> Configure -> Add Gradle nature (this didn't visibly do anything, but the option is no longer available).
In the Gradle Tasks view, run init. (This ran fine until it got to Execute setupProjectLayout for :init. This got stuck and I let it sit for about 20 mins, but there was no activity (checked task manager to confirm)).
There must be something fundamental that I'm missing here, surely it shouldn't be this difficult. How can I get this project to build in Gradle?
The Gradle init task is an interactive task to generate a new Gradle project. If you run it from the IDE but have no way to provide answers to the prompts from Gradle, it will indeed wait forever on an input.
You do not need a CLI for input.
After you run the gradle task in the "Gradle Tasks" view you switch to the "Console" view and you are presented with the questions by gradle init. You answer those questions and finish the task.
Working Directory: /path/to/your/project/thenewgradleproject
Gradle user home: /home/user/.gradle
Gradle Distribution: Gradle wrapper from target build
Gradle Version: 6.8
Java Home: /usr/lib/jvm/java-11-openjdk-amd64
JVM Arguments: None
Program Arguments: None
Build Scans Enabled: false
Offline Mode Enabled: false
Gradle Tasks: init
> Task :wrapper UP-TO-DATE
Select type of project to generate:
1: basic
2: application
3: library
4: Gradle plugin
Enter selection (default: basic) [1..4]
> Task :init
Select build script DSL:
1: Groovy
2: Kotlin
Enter selection (default: Groovy) [1..2]
Project name (default: thenewgradleproject):
> Task :init
Get more help with your project: Learn more about Gradle by exploring our samples at https://docs.gradle.org/6.8/samples
BUILD SUCCESSFUL in 53s
2 actionable tasks: 1 executed, 1 up-to-date

what's the gradle equivalent of sbt's "testQuick"

Scala's sbt has the option to run only tests that previously failed. Is there a gradle equivalent?
https://www.scala-sbt.org/1.x/docs/Testing.html#testQuick
gradle does this automatically with all update-to-date tests marked as such, based on code changes since latest test passing.
You can also force re-run of tests if need be: How to run Gradle test when all tests are UP-TO-DATE?

sbt ignore test failures

For SonarQube jobs in Jenkins we'd like to proceed even though some tests might fail. Currently the Sonar Runner is not kicked off, because a test fails.
In Maven you'd just add -DtestFailureIgnore = true, but I cannot find anything similar for SBT.
I did find a onFailure thing for sbt, but have not found any examples anywhere how to use this. Could this be used to ignore test failures so the build job continues so the Sonar Runner gets started afterwards?
Or is there a setting in Jenkins to ignore the result of the build?
We use 'sbt clean coverage test coverageReport' as build command and have Sonar Runner in a post-build step.
Finally found a solution myself.
In SBT you can define a new task A which captures the result of another task B. This dependency ensures that task B is run when the new task A is started. By capturing the result, the result of task B is not the result of task A so if B fails, A does not (have to) fail.
So in this case, I added created a new 'ciTests' tasks to the 'build.sbt'
// Define a special test task which does not fail when any test fails,
// so sequential tasks (like SonarQube analysis) will be performed no matter the test result.
lazy val ciTests = taskKey[Unit]("Run tests for CI")
ciTests := {
// Capture the test result
val testResult = (test in Test).result.value
}
Now in the Jenkins job it build the project using SBT with commands (using SCoverage SBT plugin):
update coverage ciTests coverageReport
This build will succeed ignoring any failing tests. Therefore a next build step to start SonarRunner will start the analysis of the Scala project and put the results in SonarQube.
Thanks to #hugo-zwaal for pointing me to this answer which helped me solving my issue.

Gradle Spock test after execution of a task

Hi I want to write a Spock test for my Gradle plugin to test if a report is being generated after the execution of a task from the plugin, so
private ProjectInternal project
...
public void 'check tasks'(){
given:
project.gradle.startParameter.taskNames = ["myTaskName"]
project.gradle.buildListenerBroadcaster.projectsLoaded(project.gradle)
when:
project.plugins.apply(MYPlugin.class)
project.?????
then:
...
But the "then:" section has to check the existance of a file but for this "myTaskName" has to be executed, how to make Ggradle to execute my task? There is no such method
afterExecution ??
This kind of test is suitable for testing plugins, but not for testing tasks. Applying a plugin only configures tasks, it doesn't execute them. In order to execute tasks, you'll have to kick off a "real" build from your test. The recommended way to do this is via the Gradle tooling API.
If you are prior Gradle 5 you can use project.your_task.execute()