private synchronized void buildModel()

in src/main/java/org/apache/maven/buildcache/DefaultMultiModuleSupport.java [116:181]


    private synchronized void buildModel(MavenSession session) {
        if (built) {
            return;
        }

        Optional<Discovery> multiModuleDiscovery =
                Optional.ofNullable(cacheConfig.getMultiModule()).map(MultiModule::getDiscovery);

        // no discovery configuration, use only projects in session
        if (!multiModuleDiscovery.isPresent()) {
            projectMap = buildProjectMap(session.getProjects());
            return;
        }

        Set<String> scanProfiles = new TreeSet<>(
                multiModuleDiscovery.map(Discovery::getScanProfiles).orElse(Collections.emptyList()));
        MavenProject currentProject = session.getCurrentProject();
        File multiModulePomFile = getMultiModulePomFile(session);

        ProjectBuildingRequest projectBuildingRequest = currentProject.getProjectBuildingRequest();
        boolean profilesMatched = new HashSet<>(projectBuildingRequest.getActiveProfileIds()).containsAll(scanProfiles);

        // we are building from root with the same profiles, no need to re-scan the whole multi-module project
        if (currentProject.getFile().getAbsolutePath().equals(multiModulePomFile.getAbsolutePath())
                && profilesMatched) {
            projectMap = buildProjectMap(session.getProjects());
            return;
        }

        long t0 = System.currentTimeMillis();

        ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(projectBuildingRequest);
        // clear properties because after first build request some properties could be set to profiles
        // these properties could change effective pom when we try to rebuild whole multi module project again
        // for example the first model build process do not resolve ${os.detected.classifier}
        // but once build completed this property is set to profile
        // if we try to rebuild model for the whole project here string interpolator replaces this value
        // and effective pom could be different (depending on OS) if this property is used in pom.xml
        buildingRequest.setProfiles(buildingRequest.getProfiles().stream()
                .peek(it -> it.setProperties(new Properties()))
                .collect(Collectors.toList()));
        if (!profilesMatched) {
            Set<String> profiles = new LinkedHashSet<>(buildingRequest.getActiveProfileIds());
            // remove duplicates
            profiles.addAll(scanProfiles);
            buildingRequest.setActiveProfileIds(new ArrayList<>(profiles));
        }
        try {
            List<ProjectBuildingResult> buildingResults =
                    projectBuilder.build(Collections.singletonList(multiModulePomFile), true, buildingRequest);
            LOGGER.info(
                    "Multi module project model calculated [activeProfiles={}, time={} ms ",
                    buildingRequest.getActiveProfileIds(),
                    System.currentTimeMillis() - t0);

            List<MavenProject> projectList = buildingResults.stream()
                    .map(ProjectBuildingResult::getProject)
                    .collect(Collectors.toList());
            projectMap = buildProjectMap(projectList);

        } catch (ProjectBuildingException e) {
            LOGGER.error("Unable to build model", e);
        } finally {
            built = true;
        }
    }