4

I have a Gradle-driven project to which I want to add a simple Java task. This task is very specific to the project and if it can be helped I don't want to develop it as a separate plugin. So the question is can I define such custom task within the same build.gradle I'm using for my project? Or is it inevitable that I need to package it as a separate project (plugin) and install to the local repo?

Also it's probably important to note that the original project is not Java related (no other Java code needs to be build)

P.S. Based on comments below:

I would like to add src/main/java/SomeUsefulStuff.java to the existing project and have that file compiled and used as a custom task. I do understand that it needs to be compiled each time I run the build but again - the code will be small. However it will have some external dependencies such as Commons IO

Bostone
  • 36,858
  • 39
  • 167
  • 227
  • 2
    Yes, you can wrap a java invocation into a custom task. Have you looked at JavaExec? https://docs.gradle.org/current/dsl/org.gradle.api.tasks.JavaExec.html – RaGe Mar 17 '16 at 22:47
  • Thanks for pointing this out. Investigating right now – Bostone Mar 18 '16 at 16:46
  • Actually as I understand this JavaExec can execute some code in some jar but what I'm asking is packaging few Java files within the *same* project (one build.gradle) and use these to execute some useful task. So my project tree will include src/main/java that would contain the Java code and that code needs to be compiled before the main task is run – Bostone Mar 18 '16 at 17:22
  • 1
    Which is still within the purview of JavaExec. You just have to ensure that your source is compiled and assembled first, and your JavaExec can point to the class files or the jar files in your build directory. – RaGe Mar 18 '16 at 18:36
  • 1
    In fact Gradle java plugin detects whether your java files changed and will skip compiling if the binaries are up-to-date, so you don't even have to worry about that cost. – RaGe Mar 18 '16 at 18:40
  • You right - I just posted the solution. Too bad you didn't write this as the answer so I can accept it – Bostone Mar 18 '16 at 18:59

1 Answers1

3

Thanks to RaGe who pointed to JavaExec this turned out to be pretty simple. Here's what you do:

  1. Put your Java code in /src/main/java just as you would in the regular Gradle-driven Java project. Make sure it has main method in the file you are going to call
  2. Add apply plugin: 'java' to the build.gradle
  3. If your Java code has any dependencies on 3rd party libs add these to dependencies section
  4. Add new task section to build.gradle like so:
task usefulStuff(type: JavaExec) {
      classpath = sourceSets.main.runtimeClasspath
      main = 'com.me.gradle.UsefulStuff'
      // arguments to pass to the application
      args 'OhmyGod!'
    }
  1. Now you can refer to that task as any task in your build. For example imporantTask.dependsOn usefulStuff
Community
  • 1
  • 1
Bostone
  • 36,858
  • 39
  • 167
  • 227