private void writeComponentSpringFactorySource()

in tooling/camel-spring-boot-generator-maven-plugin/src/main/java/org/apache/camel/springboot/maven/SpringBootAutoConfigurationMojo.java [1890:1945]


    private void writeComponentSpringFactorySource(String packageName, String name) throws MojoFailureException {
        String lineToAdd = packageName + "." + name;

        String fileName = "META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports";
        File target = new File(new File(baseDir, "src/main/resources"), fileName);

        deleteFileOnMainArtifact(target);

        if (target.exists()) {
            try {
                // is the auto configuration already in the file
                boolean found = false;
                List<String> lines = Files.readAllLines(target.toPath(), StandardCharsets.UTF_8);
                for (String line : lines) {
                    if (line.contains(name)) {
                        found = true;
                        break;
                    }
                }

                if (found) {
                    getLog().debug("No changes to existing file: " + target);
                } else {
                    lines.add(lineToAdd);

                    StringBuilder code = new StringBuilder();
                    for (String line : lines) {
                        code.append(line).append("\n");
                    }

                    // update
                    FileUtils.write(target, code.toString(), StandardCharsets.UTF_8, false);
                    getLog().info("Updated existing file: " + target);
                }
            } catch (Exception e) {
                throw new MojoFailureException("IOError with file " + target, e);
            }
        } else {
            // create new file
            try (InputStream is = getClass().getClassLoader().getResourceAsStream("license-header.txt")) {
                String header = PackageHelper.loadText(is);
                String code = lineToAdd;
                // add empty new line after header
                code = header + "\n" + code;
                if (getLog().isDebugEnabled()) {
                    getLog().debug("Source code generated:\n" + code);
                }

                FileUtils.write(target, code, StandardCharsets.UTF_8);
                getLog().info("Created file: " + target);
                is.close();
            } catch (Exception e) {
                throw new MojoFailureException("IOError with file " + target, e);
            }
        }
    }