简体   繁体   中英

Custom Task needs to be executed when gradle build is executed

I am working on Java/JNI project. Under parent, I have different modules for java and C codebase.Java module needs to generate JNI header file. Since, I couldn't find any existing task/plugin that supported javah, so I wrote my custom task for javah to generate JNI interface. So, when I do gradle javah

I am getting the desired output.

  1. However, I want that whenever I invoke gradle build , it should also execute javah task once either after classes are generated or build task ends.
  2. Also, is it possible to generate the shared libraries in such a mixed project when I just do gradle build ? Currently, I have to do gradle <project-name>SharedLibrary

Part 1 : You can use finalizedBy on task1 to execute task2 after task1 completes. For your case, you can try the following:

// 'javah' will be executed after 'classes' task executes.
classes.finalizedBy javah 

// Make 'javah' task depend on 'classes' task,
// so that when 'classes' task fails, 'javah' is not executed.  
javah.dependsOn classes

// Make 'build' depend on 'javah' task,
// so that executing 'build' task automatically executes 'javah'
build.dependsOn javah

Part 2 : You can use dependsOn to wire up the <project-name>SharedLibrary task as follows:

build.dependsOn "${project.name}SharedLibrary"

// Assuming the SharedLibrary task needs to depend on 'javah' task
tasks["${project.name}SharedLibrary"].dependsOn javah  

I'm assuming that the the SharedLibarary task name is dynamic, hence using a slightly different syntax for that task.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM