I’m writing a JavaCompile task that compiles some files that are generated by another task. `public class CompileGeneratedTask extends JavaCompile {
private static final String COMPILE_GENERATED_INTERNAL = “pojoGenerateCode”;
private PojoGeneratorPluginExtension extension;
private Project project;
@Inject
public CompileGeneratedTask(PojoGeneratorPluginExtension extension) {
this.extension = extension;
}
private void addCompileGeneratedAction(JavaCompile javaCompile) {
javaCompile.setSource(new File(extension.getWorkDirectory()));
javaCompile.setDestinationDir(new File(extension.getGeneratedClassesPath() + extension.getPackageName()));
}
@TaskAction
public void execute() {
String generatedClassesDirectoryName = extension.getGeneratedClassesPath() + extension.getPackageName();
File generatedClassesDirectory = new File(generatedClassesDirectoryName);
if(! generatedClassesDirectory.exists()) {
generatedClassesDirectory.mkdirs();
}
project = getProject();
JavaCompile pojoCompileGenerated = project.getTasks().create(COMPILE_GENERATED_INTERNAL, JavaCompile.class, this::addCompileGeneratedAction);
pojoCompileGenerated.getActions().forEach(action -> action.execute(pojoCompileGenerated));
}
}`
Then in the plugin, I register this task as follows:
TaskProvider<CompileGeneratedTask> compileGeneratedCode = tasks.register("compileGenerated", CompileGeneratedTask.class, drwpPojoGen); compileGeneratedCode.configure((task) -> task.dependsOn(generateCode));
However, when I test this task, the test shows a Nullpointer exception on the task actions. My test is as follows:
`public class GenerateCodeTaskTest {
@TempDir
Path projectDir;
private Project project;
private PluginContainer plugins;
private TestSupport testSupport;
private TestSupport.ProjectConfig configureProject;
private PojoGeneratorPluginExtension extension;
@BeforeEach
void setUp() {
testSupport = TestSupport.create();
project = testSupport.withProjectDir(projectDir).createProject("PojoGeneratorTestApp", "1.0.0");
plugins = project.getPlugins();
plugins.apply(PojoGeneratorPlugin.class);
extension = project.getExtensions().findByType(PojoGeneratorPluginExtension.class);
configureProject = testSupport.configureProject();
}
@Test
void test_Code_is_generated() {
configureProject = configureProject.executeActionsFor("generateCode");
String[] sourceFiles = {"example1.java", "example2.java"};
int numberOfSourceFiles = sourceFiles.length;
String filePath;
for (int i=0; i<numberOfSourceFiles; i++)
{
filePath = extension.getWorkDirectory() + extension.getPackageName() + sourceFiles[i];
Assertions.assertTrue(new File(filePath).exists());
}
}
}`