public void execute()

in src/main/java/org/apache/sling/feature/maven/mojos/AnalyseFeaturesMojo.java [79:239]


    public void execute() throws MojoExecutionException, MojoFailureException {
        checkPreconditions();
        List<Scan> list = scans;
        if (list == null || list.isEmpty()) {
            // use default configuration
            final Scan a = new Scan();
            a.setFilesInclude("**/*.*");
            list = Collections.singletonList(a);
        }

        getLog().debug(MessageUtils.buffer()
                .a("Setting up the ")
                .strong("Scanner")
                .a("...")
                .toString());
        Scanner scanner;
        try {
            scanner = new Scanner(getArtifactProvider());
        } catch (final IOException e) {
            throw new MojoExecutionException(
                    "A fatal error occurred while setting up the Scanner, see error cause:", e);
        }
        getLog().debug(MessageUtils.buffer()
                .strong("Scanner")
                .a(" successfully set up")
                .toString());

        FeatureProvider featureProvider = getFeatureProvider();

        final Map<Feature, List<AnalyserResult>> results = new LinkedHashMap<>();
        for (final Scan an : list) {
            try {
                Map<String, Map<String, String>> taskConfiguration = an.getTaskConfiguration();

                getLog().debug(MessageUtils.buffer()
                        .a("Setting up the ")
                        .strong("analyser")
                        .a(" with following configuration:")
                        .toString());
                getLog().debug(" * Task Configuration = " + taskConfiguration);
                Set<String> includedTasks = an.getIncludeTasks();
                if (includedTasks == null) {
                    // use defaults
                    includedTasks = new HashSet<>();
                    includedTasks.add("bundle-packages");
                    includedTasks.add("requirements-capabilities");
                    includedTasks.add("apis-jar");
                    includedTasks.add("repoinit");
                    if (an.getExcludeTasks() != null) {
                        includedTasks.removeAll(an.getExcludeTasks());
                        if (includedTasks.isEmpty()) {
                            includedTasks = null;
                        }
                    }
                }
                getLog().debug(" * Include Tasks = " + includedTasks);
                getLog().debug(" * Exclude Tasks = " + an.getExcludeTasks());
                final Analyser analyser = new Analyser(scanner, taskConfiguration, includedTasks, an.getExcludeTasks());
                getLog().debug(MessageUtils.buffer()
                        .strong("Analyser")
                        .a(" successfully set up")
                        .toString());

                getLog().debug("Retrieving Feature files...");
                final Collection<Feature> features =
                        this.getSelectedFeatures(an).values();

                if (features.isEmpty()) {
                    getLog().debug(
                                    "There are no associated feature files to current project, plugin execution will be skipped");
                    continue;
                } else {
                    getLog().debug("Starting analysis of features...");
                }

                for (final Feature f : features) {
                    try {
                        getLog().debug(MessageUtils.buffer()
                                .a("Analyzing feature ")
                                .strong(f.getId().toMvnId())
                                .a(" ...")
                                .toString());
                        Dependency fwk = an.getFramework();
                        if (fwk == null) {
                            fwk = this.framework;
                        }
                        final AnalyserResult result =
                                analyser.analyse(f, ProjectHelper.toArtifactId(fwk), featureProvider);

                        results.computeIfAbsent(f, (key) -> new ArrayList<>()).add(result);
                    } catch (final Exception t) {
                        throw new MojoFailureException(
                                "Exception during analysing feature "
                                        + f.getId().toMvnId() + " : " + t.getMessage(),
                                t);
                    }
                }
            } catch (final IOException e) {
                throw new MojoExecutionException(
                        "A fatal error occurred while setting up the analyzer, see error cause:", e);
            } finally {
                getLog().debug("Features analysis complete");
            }
        }
        boolean hasErrors = false;
        for (final Map.Entry<Feature, List<AnalyserResult>> entry : results.entrySet()) {
            final Feature f = entry.getKey();
            final List<AnalyserResult> result = entry.getValue();

            final List<String> warnings =
                    result.stream().flatMap(r -> r.getWarnings().stream()).collect(Collectors.toList());
            final List<String> errors =
                    result.stream().flatMap(r -> r.getErrors().stream()).collect(Collectors.toList());

            if ((warnings.isEmpty() || !logWarnings) && errors.isEmpty()) {
                getLog().debug(MessageUtils.buffer()
                        .a("feature ")
                        .project(f.getId().toMvnId())
                        .a(" succesfully passed all analysis")
                        .toString());
            } else {
                final String message;
                if (!errors.isEmpty()) {
                    if (logWarnings && !warnings.isEmpty()) {
                        message = "errors and warnings";
                    } else {
                        message = "errors";
                    }
                    hasErrors = true;
                } else {
                    message = "warnings";
                }
                final String m = "Analyser detected "
                        .concat(message)
                        .concat(" on feature '")
                        .concat(f.getId().toMvnId())
                        .concat("'.");
                if (hasErrors) {
                    getLog().error(m);
                } else {
                    getLog().warn(m);
                }
                if (logWarnings) {
                    for (final String msg : warnings) {
                        getLog().warn(msg);
                    }
                }
                for (final String msg : errors) {
                    getLog().error(msg);
                }
            }
        }
        if (hasErrors) {
            if (failOnAnalyserErrors) {
                throw new MojoFailureException(
                        "One or more feature analyser(s) detected feature error(s), please read the plugin log for more details");
            }
            getLog().warn(
                            "Errors found during analyser run, but this plugin is configured to ignore errors and continue the build!");
        }
    }