I hava a java project with a customtask implemented. I am trying to run it in gradle. From the docs (ref chatpter 38) I understand there are 3 ways of writing customtask. 1)in the build script if i can write the code in groovy.2)by defining the customtask in you project under buildSrc 3)Stand alone project. I am trying to do it through the stand alone process but after following the doc and running the task gradle doesnt recognize it. I tried putting it on its classpath but still gradle somewhow doesnt recognize my task. I cannot find much documentation around implementing java customtask and using it in your build. Also I dont use maven and most of the examples seem to refer files in maven repositiory.All my files are saved locally. Any help around this will be much appreciated.
Please share more information on what you are trying to do, including your project structure and build script. Without that no one will be able to tell where the problem is.
the java file is under
–gradleJavaTest
—src/main/java/GreetingTask.java
build.gradle
GreetingTask.java
import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.TaskAction;
class GreetingTask extends DefaultTask {
@TaskAction
public void greet() {
System.out.println (“hello from gradle”);
}
}
build.gradle
apply plugin: ‘java’
dependencies {
compile gradleApi()
compile files(‘/gradleJavaTest.jar’)
}
task hello(type: GreetingTask)
On running
gradle hello
FAILURE: Build failed with an exception.
-
Where:
Build file ‘gradleJavaTest/build.gradle’ line: 20 -
What went wrong:
A problem occurred evaluating root project ‘gradleJavaTest’.
Could not find property ‘GreetingTask’ on root project ‘gradleJavaTest’.
- Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
We tried a couple of options such as
// runtime files(‘libs/gradleproject.jar’)
// runtime fileTree(dir: ‘build/libs’, include: ‘*’)
But still getting the same error.
If you want to write a binary Task or Plugin and use it in the same build, you have to put it in the buildSrc
project, as outlined in the documentation.
If you define a Task/Plugin in a standalone project, you cannot use the Task/Plugin in the same project. You can only use it in other projects that have it on their buildScript classpath.
Hi Stefan,
Thanks for the reply. We tried the buildSrc option before and it worked,
We also created another project to run the stand alone java task from with a new build.gradle which tries to use the java task described above. However we are getting the same error.
Can you tell us the syntax for adding the jar to the buildScript classpath pointing to a local jar and not in any repository?
Also can we define task in the src instead of buildSrc and use it by changing any settings?
Thanks again!
Can you tell us the syntax for adding the jar to the buildScript classpath pointing to a local jar?
buildscript {
dependencies {
classpath files("path/to/your.jar")
}
}
Also can we define task in the src instead of buildSrc and use it by changing any settings?
In the same build? No, because that would create a chicken-and-egg problem. You need all tasks/plugins before you can run the build. You can’t add tasks/plugins while the build is already in the execution phase. That’s why we created the special buildSrc
project, which is built before the main build runs.
Hi Stefan,
Really appreciate the help. It works now:)
we did try something similar before
dependencies {
classpath files(“path/to/your.jar”)
}
but didnt have it inside
buildscript{ …}