public void afterProjectsRead()

in maven-enforcer-extension/src/main/java/org/apache/maven/extensions/enforcer/EnforceExtension.java [60:126]


    public void afterProjectsRead(MavenSession session) throws MavenExecutionException {
        Xpp3Dom configuration;
        Path config = Paths.get(session.getExecutionRootDirectory(), ENFORCER_EXTENSION_XML);
        if (Files.isRegularFile(config)) {
            try (Reader reader = Files.newBufferedReader(config, StandardCharsets.UTF_8)) {
                configuration = Xpp3DomBuilder.build(reader);
            } catch (XmlPullParserException | IOException e) {
                throw new MavenExecutionException("Failed to read " + ENFORCER_EXTENSION_XML, e);
            }
        } else {
            return;
        }

        List<PluginExecution> executions = null;
        Xpp3Dom executionsDom = configuration.getChild("executions");
        if (executionsDom != null) {
            executions = new ArrayList<>();
            for (Xpp3Dom executionDom : executionsDom.getChildren("execution")) {
                executions.add(getPluginExecution(executionDom));
            }
        }

        if (executions == null) {
            return;
        }

        for (MavenProject project : session.getProjects()) {
            Plugin enforcerPlugin = null;
            for (Plugin plugin : project.getBuildPlugins()) {
                if ("maven-enforcer-plugin".equals(plugin.getArtifactId())
                        && "org.apache.maven.plugins".equals(plugin.getGroupId())) {
                    enforcerPlugin = plugin;
                }
            }

            if (enforcerPlugin == null) {
                enforcerPlugin = new Plugin();
                enforcerPlugin.setGroupId("org.apache.maven.plugins");
                enforcerPlugin.setArtifactId("maven-enforcer-plugin");

                try (InputStream is = EnforceExtension.class.getResourceAsStream(POM_PROPERTIES)) {
                    Properties properties = new Properties();
                    properties.load(is);
                    enforcerPlugin.setVersion(properties.getProperty("version"));
                } catch (IOException e) {
                    // noop
                }

                if (project.getBuildPlugins().isEmpty()) {
                    Build build = project.getBuild();
                    if (build == null) {
                        build = new Build();
                        project.setBuild(build);
                    }
                    build.setPlugins(Collections.singletonList(enforcerPlugin));
                } else {
                    List<Plugin> buildPlugins = new ArrayList<>(project.getBuildPlugins());
                    buildPlugins.add(enforcerPlugin);
                    project.getBuild().setPlugins(buildPlugins);
                }
            }

            for (PluginExecution pe : executions) {
                enforcerPlugin.addExecution(pe);
            }
        }
    }