public CacheState initialize()

in src/main/java/org/apache/maven/buildcache/xml/CacheConfigImpl.java [134:202]


    public CacheState initialize() {
        if (state == null) {
            synchronized (this) {
                if (state == null) {
                    final boolean enabled = getProperty(CACHE_ENABLED_PROPERTY_NAME, true);

                    if (!rtInfo.isMavenVersion("[3.9.0,)")) {
                        LOGGER.warn(
                                "Cache requires Maven >= 3.9, but version is {}. Disabling cache.",
                                rtInfo.getMavenVersion());
                        state = CacheState.DISABLED;
                    } else if (!enabled) {
                        LOGGER.info("Cache disabled by command line flag, project will be built fully and not cached");
                        state = CacheState.DISABLED;
                    } else {
                        Path configPath;

                        String configPathText = getProperty(CONFIG_PATH_PROPERTY_NAME, null);
                        if (StringUtils.isNotBlank(configPathText)) {
                            configPath = Paths.get(configPathText);
                        } else {
                            configPath =
                                    getMultimoduleRoot(session).resolve(".mvn").resolve("maven-build-cache-config.xml");
                        }

                        if (!Files.exists(configPath)) {
                            LOGGER.info(
                                    "Cache configuration is not available at configured path {}, "
                                            + "cache is enabled with defaults",
                                    configPath);
                            cacheConfig = new CacheConfig();
                        } else {
                            try {
                                LOGGER.info("Loading cache configuration from {}", configPath);
                                cacheConfig = xmlService.loadCacheConfig(configPath.toFile());
                            } catch (Exception e) {
                                throw new IllegalArgumentException(
                                        "Cannot initialize cache because xml config is not valid or not available", e);
                            }
                        }
                        fillWithDefaults(cacheConfig);

                        // `maven.build.cache.enabled` overrides the `enabled` of the XML file
                        // to allow a disabled configuration to be enabled on the command line
                        boolean cacheEnabled = getProperty(
                                CACHE_ENABLED_PROPERTY_NAME, getConfiguration().isEnabled());

                        if (!cacheEnabled) {
                            state = CacheState.DISABLED;
                        } else {
                            String hashAlgorithm = null;
                            try {
                                hashAlgorithm = getConfiguration().getHashAlgorithm();
                                hashFactory = HashFactory.of(hashAlgorithm);
                                LOGGER.info("Using {} hash algorithm for cache", hashAlgorithm);
                            } catch (Exception e) {
                                throw new IllegalArgumentException(
                                        "Unsupported hashing algorithm: " + hashAlgorithm, e);
                            }

                            excludePatterns = compileExcludePatterns();
                            state = CacheState.INITIALIZED;
                        }
                    }
                }
            }
        }
        return state;
    }