private Optional findBestMatchingBuildImpl()

in src/main/java/org/apache/maven/buildcache/LocalCacheRepositoryImpl.java [228:298]


    private Optional<Build> findBestMatchingBuildImpl(Pair<MavenSession, Dependency> dependencySession) {
        try {
            final MavenSession session = dependencySession.getLeft();
            final Dependency dependency = dependencySession.getRight();

            final Path artifactCacheDir =
                    artifactCacheDir(session, dependency.getGroupId(), dependency.getArtifactId());

            final Map<Pair<String, String>, Collection<Pair<Build, Path>>> filesByVersion = new HashMap<>();

            Files.walkFileTree(artifactCacheDir, new SimpleFileVisitor<Path>() {

                @Override
                public FileVisitResult visitFile(Path path, BasicFileAttributes basicFileAttributes) {
                    final File file = path.toFile();
                    if (file.getName().equals(BUILDINFO_XML)) {
                        try {
                            final org.apache.maven.buildcache.xml.build.Build dto = xmlService.loadBuild(file);
                            final Pair<Build, Path> buildInfoAndFile = Pair.of(new Build(dto, CacheSource.LOCAL), path);
                            final String cachedVersion = dto.getArtifact().getVersion();
                            final String cachedBranch = getScmRef(dto.getScm());
                            add(filesByVersion, Pair.of(cachedVersion, cachedBranch), buildInfoAndFile);
                            if (isNotBlank(cachedBranch)) {
                                add(filesByVersion, Pair.of(EMPTY, cachedBranch), buildInfoAndFile);
                            }
                            if (isNotBlank(cachedVersion)) {
                                add(filesByVersion, Pair.of(cachedVersion, EMPTY), buildInfoAndFile);
                            }
                        } catch (Exception e) {
                            // version is unusable nothing we can do here
                            LOGGER.info(
                                    "Build info is not compatible to current maven " + "implementation: {}", file, e);
                        }
                    }
                    return FileVisitResult.CONTINUE;
                }
            });

            if (filesByVersion.isEmpty()) {
                return Optional.empty();
            }

            final String currentRef = getScmRef(CacheUtils.readGitInfo(session));
            // first lets try by branch and version
            Collection<Pair<Build, Path>> bestMatched = new LinkedList<>();
            if (isNotBlank(currentRef)) {
                bestMatched = filesByVersion.get(Pair.of(dependency.getVersion(), currentRef));
            }
            if (bestMatched.isEmpty()) {
                // then by version
                bestMatched = filesByVersion.get(Pair.of(dependency.getVersion(), EMPTY));
            }
            if (bestMatched.isEmpty() && isNotBlank(currentRef)) {
                // then by branch
                bestMatched = filesByVersion.get(Pair.of(EMPTY, currentRef));
            }
            if (bestMatched.isEmpty()) {
                // ok lets take all
                bestMatched = filesByVersion.values().stream()
                        .flatMap(Collection::stream)
                        .collect(Collectors.toList());
            }

            return bestMatched.stream()
                    .max(Comparator.comparing(p -> lastModifiedTime(p.getRight())))
                    .map(Pair::getLeft);
        } catch (IOException e) {
            LOGGER.info("Cannot find dependency in cache", e);
            return Optional.empty();
        }
    }