Task.java

The task file TestSampleTask.java file is output to the ./src/main/java/synapticloop/plugin/sample directory. This is the object that actually does all of the work. It uses the extension object to determine the parameters for its action.

Looking at the file, you will see the contents:

package synapticloop.plugin.sample;

// - - - - thoughtfully generated by synapticloop gradle-plugin-java-create - - - - 
//            with the use of synapticloop templar templating language
//                            (task.java.templar)


import org.gradle.api.DefaultTask;
import org.gradle.api.logging.Logger;
import org.gradle.api.tasks.TaskAction;

public class TestSampleTask extends DefaultTask {
	private Logger logger;
	private TestSamplePluginExtension extension;

	public TestSampleTask() {
		setGroup("Sample");
		setDescription("a simple test plugin");

		this.logger = getProject().getLogger();
	}

	@TaskAction
	public void generate() {
		extension = getProject().getExtensions().findByType(TestSamplePluginExtension.class);

		if (extension == null) {
			extension = new TestSamplePluginExtension();
		}

		logger.info("This will only be logged when the task is invoked with the --info command line argument.");
		logger.debug("This will only be logged when the task is invoked with the --debug command line argument.");
		logger.lifecycle("This will be logged as output on standard out.");

		logger.lifecycle("Logging out the boolean value of 'booleanValue' as '{}'", extension.getBooleanValue());
		logger.lifecycle("Logging out the string value of 'stringValue' as '{}'", extension.getStringValue());

		for (String stringListValue : extension.getStringList()) {
			logger.lifecycle("Logging out the string value from the list 'stringList' as '{}'", stringListValue);
		}
	}
}

Variables used in the generation

TestSampleTask.java uses the following variables:

  • {package} - the command line argument -p or --package
    • This is split on the '.' character to create the output directory structure.
    • This becomes the package {package}; declaration at the top of the file
  • {name} - the command line argument -n or --name
    • This becomes the file name {name}Task.java
    • This becomes the class declaration public class {name}Task implements DefaultTask {

For the following code:

	public TestSampleTask() {
		setGroup("Sample");
		setDescription("a simple test plugin");

		this.logger = getProject().getLogger();
	}
  • {group} - this is generated from the -g or --group command line argument.
    • this is used to set the grouping for the task setGroup("Sample");
  • {desc} - this is generated from the -d or --desc command line argument.
    • this is used to set the description for the task setDescription("a simple test plugin");

Things of note

This is the class that you will need to change the most - it does all of the work associated with the plugin.

  1. If you want to add additional tasks and extensions, this is the place to add them. You may have as many tasks as you require - optionally with additional extensions for each task. Alternatively you may only have one extension which can service all of the tasks.
  2. The name of the file is important - this must match the fully qualified class name that is referenced in the .properties file (in this case the synapticloop.plugin.sample.TestSample.properties file)