'How to properly set up and use multiple generated source directories for an annotation processor in Graddle

I'm failty new to Java and Gradle. I'm experimenting with building a multi-project with a custom annotation processor. Following this answer I was able to generate the new source files I want in the gen/main/java folder of the annotation-user project. Now I would like to also generate unit tests for these classes and put them in the gen/test/java, but I'm really struggling to find a way to set up multiple source output paths in order to do this.

I tried using StandardFileManager for this:

    StandardJavaFileManager fileManager = javaCompiler.getStandardFileManager(null, null, null);
    Location genMainLocation = StandardLocation.locationFor("GENERATED_MAIN_OUTPUT");
    Location genTestLocation = StandardLocation.locationFor("GENERATED_TEST_OUTPUT");

    String genMainOutput;
    String genTestOutput;

    @Override
    public void init(ProcessingEnvironment processingEnvironment) {
        super.init(processingEnvironment);
        Properties prop = new Properties();
        InputStream inputStream = getClass().getClassLoader().getResourceAsStream("project.properties");
        try {
            prop.load(inputStream);
            genMainOutput = prop.getProperty("generated.main.output");
            genTestOutput = prop.getProperty("generated.test.output");
            fileManager.setLocation(genMainLocation, Arrays.asList(new File(genMainOutput)));
            fileManager.setLocation(genTestLocation, Arrays.asList(new File(genTestOutput)));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

and then doing

        JavaFileObject newSourceFile = fileManager.getJavaFileForOutput(genMainLocation,
                packageName + "." + simpleClassName, JavaFileObject.Kind.SOURCE, null);
        try (PrintWriter out = new PrintWriter(newSourceFile.openWriter())) {

in the corresponding method to write the .java file. But even though this works for generating the new source files, when I try to run the gradle build command, the :annotation-user:compileJava task gives en error as it can't find the new classes, which are referenced in the original ones.

Before wanting to create the unit test files I was using this:

        JavaFileObject newSourceFile = processingEnv.getFiler().createSourceFile(packageName + "." + simpleClassName);
        try (PrintWriter out = new PrintWriter(newSourceFile.openWriter())) {

and everything worked fine, but I found no way to use this with multiple output directories. I tried setting the source output directory as just "$projectDir" in the build.gradle and adding gen/main/java before the packageName when calling createSourceFile, but I just got hit with a Module: gen does not exist.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source