Run my task before a plugin's task? - gradle

We're using a gradle file to build a Java WAR file. I know very little about gradle. At the top of build.gradle:
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'war'
We run the gradle with gradle clean install. I'm not sure where these tasks are defined but I assume they're in one of the plugins (I'd guess war).
When I run gradle clean install it seems to print the tasks that it are run:
:clean
:compileJava
:processResources
:classes
:war
:install
Correct me if I'm wrong, but it seems that the task install dependsOn compileJava, processResources, classes, and war.
I need a task I've written to run sometime after clean but sometime before war. Preferably without modifying the plugin.
I've tried indicating that my task mustRunAfter processResources but it doesn't work that way.
How can I inject my task as a dependency on install before the dependency war?

You can declare task dependencies explicitly.
Add following code to your build.gradle file
tasks.war.dependsOn("yourTaskNameHere")
tasks["yourTaskNameHere"].dependsOn("clean")

Related

Gradle build doesn't generate jar

I would like to build my project as a jar file which only contains my code. This project jar is used as a library to other java project.
Here is my build.gradle:
plugins {
id 'java'
}
group 'com.my.lib'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
task sourcesJar(type: Jar) {
archiveClassifier = 'sources'
from sourceSets.main.allJava
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
After I build the project (I am using IntelliJ, I clicked "Build" ==> "Rebuild project"). IntelliJ told me build successfully But I don't see any jar created. Why?
18:30:18: Executing tasks ':classes :testClasses'...
> Task :compileJava
> Task :processResources NO-SOURCE
> Task :classes
> Task :compileTestJava
> Task :processTestResources NO-SOURCE
> Task :testClasses
BUILD SUCCESSFUL in 0s
2 actionable tasks: 2 executed
18:30:18: Tasks execution finished ':classes :testClasses'.
I figured out that even I have those settings, when I choose Build->Build project, Intellij still not run Gradle build that's why in my original question there is no "sourceJar" showing. I have to open "Gradle tool" window, click on that little elephant icon to run gradle task. Then, type "Gradle build", it then run the source Jar task.
The "Build Project" form IntelliJ IDEA will compile your code for usage in the IDE, it is not directly tied to the build lifecycle task of Gradle.
For Gradle to build the JAR, you need to run ./gradlew assemble, which will have the expected outcome.
Note that this will not run tests or static analysis. If you want that to run as well, you can use ./gradlew build.
Both of these Gradle tasks can be executed from the Gradle tool window in IntelliJ IDEA.
Open the project in your file system and you might find it under the build/libs/.. folder

Using Gradle multi-project build apply plugin is applicable to master project

In a multi-project build using Gradle v5.5.1 I am trying to apply the ear plugin only to certain subprojects (as described in using plugins). You can see here that I'm trying to apply it for subprojects ending in EAR:
subprojects { Project proj ->
afterEvaluate {
if(proj.projectDir.name.endsWith('EAR')){
logger.debug "{} looks like an EAR subproject", proj.name
apply plugin: 'ear'
defaultTasks 'ear'
}
}
However, it applies it to my master project instead (output log):
10:53:36.366 [DEBUG] [org.gradle.api.Project] MAG820PAYMENTRECONEAR looks like an EAR subproject
10:53:36.367 [DEBUG] [org.gradle.internal.operations.DefaultBuildOperationExecutor] Build operation 'Apply plugin org.gradle.ear to root project 'master'' started
Then when it gets to the execution of my subproject it says the ear task is not found:
10:53:36.375 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] Task 'ear' not found in project ':MAG820PAYMENTRECON:MAG820PAYMENTRECONEAR'.
I know this works because I have other projects doing similar but I can't figure out why this one is working this way.
What am I doing wrong??
Simply use proj.apply plugin: 'ear' to call the method on the sub-project

Gradle: What is the difference between the tasks 'build' and 'buildSearchableOptions'?

I am building a plugin in IntelliJ and Gradle, and have the following question:
What is the difference between the predefined tasks build and buildSearchableOptions in Gradle?
I can see that :buildSearchableOptions is called as part of :build and that it produces its own JAR file.
They come from two different plugins.
Assuming a Java project, the build task comes from the java plugin which in turn comes from the life cycle/base plugin:
https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/java/org/gradle/api/plugins/JavaBasePlugin.java#L74
https://docs.gradle.org/current/userguide/base_plugin.html
The buildSearchableOptions task comes from the org.jetbrains.intellij plugin:
https://github.com/JetBrains/gradle-intellij-plugin/blob/master/src/main/groovy/org/jetbrains/intellij/IntelliJPlugin.groovy#L350..L360
https://github.com/JetBrains/gradle-intellij-plugin#tasks

Gradle plugin task ordering

What I have?
Java source file with Main class (MainApp)
gradle build script
apply plugin: 'application'
mainClassName = "MainApp"
sourceSets.main.java.srcDirs = ['.']
So when I do gradle run, it executes main method and everything works just perfectly.
C:\tmp\gradle-fun>gradle run
:compileJava
:processResources UP-TO-DATE
:classes
:run
Hello MainApp !!
BUILD SUCCESSFUL
What I want to do?
Now I was wondering about clean task (common build tasks) to clean the build directory before run task executes.
There is reason behind that, I want to make sure that every time gradle should compile the java files and all .class file should be refreshed (its some requirement)
What I have tried?
Added a wrapper task which executes clean task and run task in order.
apply plugin: 'application'
mainClassName = "MainApp"
sourceSets.main.java.srcDirs = ['.']
task exec(dependsOn: ['clean', 'run'])
So when I run gradle exec, it does it job properly. However I feel that its patch work when you have extra tasks just for ordering execution.
C:\tmp\gradle-fun>gradle run
:clean
:compileJava
:processResources UP-TO-DATE
:classes
:run
Hello MainApp !!
:exec
BUILD SUCCESSFUL
What I would like to know?
Is there any way to avoid writing wrapper task and do some Gradle magic to achieve the requirement?
Just have the run task depend on clean. This will ensure your project is cleaned before every run. If you want to be more specific in regards to your use case, you can simply clean the compileJava task.
run.dependsOn 'cleanCompileJava'
Edit: To avoid deleting your classes before the run add:
classes.mustRunAfter 'cleanCompileJava'
You could create your own task, with clean and run as dependencies:
task cleanRun(dependsOn: [clean, run])
Or, you could follow Mark Vieira's answer and change the wiring:
run.dependsOn 'clean'
classes.mustRunAfter 'clean'
The second line makes sure that it doesn't clean the compiled classes.
Hope this helps =)

Why is uploadArchives not listed at the tasks list?

i thought uploadArchives is a task which is provided by the java plugin.
In my build.gradle i use the java plugin:
apply plugin: 'java'
But if i invoke gradle tasks on command line, i can't see the uploadArchives task.
Even not with gradle gradle tasks --all
The uploadArchives task is listed in the gradle java plugin documentation
see http://www.gradle.org/java_plugin (table 11).
I use gradle version 1.0-milestone-6.
I can invoke gradle uploadArchives without error, but the task is not listed.
The uploadArchives task is added as a Rule to your build script and not explicitly by name. In the output of "gradle tasks" you should see this line:
Pattern: upload<ConfigurationName>: Assembles and uploads the artifacts belonging to a configuration.
This means, that for each configuration in your build file, an according uploadTask exist. The java plugin adds an configuration named archives to your build script. By adding the configuration "archives" to your build script explicitly by the java plugin, the uploadArchives task is added implicitly too.
There are scenarios, where gradle can't know what tasks need to be materialized by a rule.
E.g.
tasks.addRule("Pattern: ping<ID>") { String taskName ->
if (taskName.startsWith("ping")) {
task(taskName) << {
println "Pinging: " + (taskName - 'ping')
}
}
}
There is no way to figure out which ping tasks should be shown as they are just materialized when triggered from commandline via 'gradle pingServer1 pingServer2 pingServer3'
regards,
René
The uploadArchives task is a part of the maven-plugin. You have to add:
apply plugin: 'maven'

Resources