Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Design dynamic tasks

sebersole edited this page Sep 14, 2010 · 2 revisions

One thing I’d really like to accomplish here is to have the plugin generate tasks based on user input. So the tasks are dynamically created based on the users settings. An example will help illustrate.

One aspect of JDocBook is handling translations, via GNU gettext. As the first step in the build we must take as inputs the (master translation) source document and the translation PO files and generate “translated XML”. Now the user needs to tell us which translations to process. So for example a user might say:

jdocbook.configure {
    translations: ["de-DE", "es-ES", "fr-FR" ]
}

Ideally, the plugin would create 3 separate “apply translation” task instances, one for each of the languages referenced above. For example:

public void apply(Project project) {
    ...
    for ( String translationLanguage : configuration.getTranslations() ) {
        final translateTaskName = "translate_" + translationLanguage;
        ApplyTranslationTask applyTranslationTask = project.getTasks().add(  translateTaskName, ApplyTranslationTask.class );
        ...
    }
    ...
}

The issue is getting access to the user-supplied translation languages during Plugin#apply since that is the same time when the convention object is being mixed in. So it appears that this Plugin#apply API was designed with the premise that the tasks and other setup needing to be done would be known up front.

Changes to gradle itself aside, the potential options I see:

  1. Have the tasks manage the multiple “sub tasks”; e.g. ApplyTranslationTask would handle applying the translations for all languages, looping over each when executed
  2. Have the convention object be the trigger to register the tasks (on execution of its JDocBookConvention#configure method)
  3. task rules
Clone this wiki locally