final Set addGeneratedSourceDirectory()

in src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java [1578:1612]


    final Set<Path> addGeneratedSourceDirectory() throws IOException {
        Path generatedSourcesDirectory = getGeneratedSourcesDirectory();
        if (generatedSourcesDirectory == null) {
            return Set.of();
        }
        /*
         * Do not create an empty directory if this plugin is not going to generate new source files.
         * However, if a directory already exists, use it because maybe its content was generated by
         * another plugin executed before the compiler plugin.
         *
         * TODO: "none" become the default starting with Java 23.
         */
        if ("none".equalsIgnoreCase(proc) && Files.notExists(generatedSourcesDirectory)) {
            return Set.of();
        } else {
            // `createDirectories(Path)` does nothing if the directory already exists.
            generatedSourcesDirectory = Files.createDirectories(generatedSourcesDirectory);
        }
        ProjectScope scope = compileScope.projectScope();
        projectManager.addSourceRoot(project, scope, Language.JAVA_FAMILY, generatedSourcesDirectory.toAbsolutePath());
        if (logger.isDebugEnabled()) {
            var sb = new StringBuilder("Adding \"")
                    .append(generatedSourcesDirectory)
                    .append("\" to ")
                    .append(scope.id())
                    .append("-compile source roots. New roots are:");
            projectManager
                    .getEnabledSourceRoots(project, scope, Language.JAVA_FAMILY)
                    .forEach((p) -> {
                        sb.append(System.lineSeparator()).append("    ").append(p.directory());
                    });
            logger.debug(sb.toString());
        }
        return Set.of(generatedSourcesDirectory);
    }