Best way to define javah task in gradle

I am new to gradle and groovy. Here is my task that generates JNI headers:

apply plugin: 'java' apply plugin: 'application' mainClassName = "com.my.example.HelloG" task GenerateJniHeaders(dependsOn: 'classes') << { def classpath = "build/classes/main" def nativeIncludes = "src/native/include" "javah -d ${nativeIncludes} -classpath ${classpath} ${mainClassName}".execute() } classes.finalizedBy GenerateJniHeaders build.dependsOn GenerateJniHeaders 

It works fine, but I feel it is a little ugly. How can I improve it? I appreciate any suggestions, especially those that help me remove variables that are hard-coded by me. I would also like to make this task more universal - now it only generates a JNI header for the main class, but I want to run it for all java files. Moreover, when this task fails (for example, the path to the class is incorrect), it does not print error messages and does not create errors, which is misleading.

+6
source share
1 answer
  • Gradle has an Exec type task type with a command line property, so it would be more appropriate to use it:

     task generateJniHeaders(type:Exec) { def classpath = sourceSets.main.output.classesDir def nativeIncludes = "src/native/include" commandLine "javah", "-d", nativeIncludes, "-classpath", classpath, "$mainClassName" dependsOn classes } 

    Note that in this way everything in this task is a configuration, not an action (if you are not familiar with the Gradle build life cycle, then the recommended reading will be this in the user guide.

  • build.dependsOn GenerateJniHeaders should be replaced with jar.dependsOn GenerateJniHeaders

  • classes.finalizedBy is not required at all. Note that finalizedBy usually used to clean up when it is executed, even if the task does not work.

+12
source

Source: https://habr.com/ru/post/986850/


All Articles