All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.toomuchcoding.uptodate.finder.maven.MavenNewVersionFinderFactory.groovy Maven / Gradle / Ivy

package com.toomuchcoding.uptodate.finder.maven

import com.toomuchcoding.uptodate.UptodatePluginExtension
import com.toomuchcoding.uptodate.dependency.Dependency
import com.toomuchcoding.uptodate.dependency.Version
import com.toomuchcoding.uptodate.dependency.VersionPatternMatcher
import com.toomuchcoding.uptodate.finder.NewVersionFinder
import com.toomuchcoding.uptodate.finder.NewVersionFinderFactory
import com.toomuchcoding.uptodate.finder.http.HTTPBuilderProvider
import com.toomuchcoding.uptodate.finder.FinderConfiguration
import com.toomuchcoding.uptodate.finder.RepositorySettings
import groovy.transform.PackageScope
import groovy.util.logging.Slf4j
import groovyx.net.http.HTTPBuilder

import java.util.concurrent.Future

import static com.toomuchcoding.uptodate.UrlEspaceUtils.escape
import static com.toomuchcoding.uptodate.finder.http.HTTPBuilderProvider.FailureHandlers.logOnlyFailureHandler

@Slf4j
class MavenNewVersionFinderFactory implements NewVersionFinderFactory {

    public static final String MAVEN_CENTRAL_REPO_URL = "https://search.maven.org/solrsearch/select"

    @Override
    NewVersionFinder create(UptodatePluginExtension uptodatePluginExtension, List dependencies) {
        FinderConfiguration finderConfiguration = new FinderConfiguration(
                new RepositorySettings(repoUrl: uptodatePluginExtension.mavenRepo, ignoreRepo: uptodatePluginExtension.ignoreMavenCentral),
                uptodatePluginExtension,
                dependencies.size())
        return new NewVersionFinder(
                mavenLatestVersionsCollector(finderConfiguration),
                finderConfiguration)
    }

    private Closure mavenLatestVersionsCollector(FinderConfiguration finderConfiguration) {
        HTTPBuilder httpBuilder = new HTTPBuilderProvider(finderConfiguration.httpConnectionSettings).get()
        return getLatestFromMavenCentralRepo.curry(httpBuilder, finderConfiguration.excludedVersionPatterns)
    }

    private Closure getLatestFromMavenCentralRepo = { HTTPBuilder httpBuilder, List versionToExcludePatterns, Dependency dependency ->
        httpBuilder.handler.failure = logOnlyFailureHandler(log, dependency.name)
        httpBuilder.get(queryString: buildQueryString(dependency)) { resp, json ->
            if (!json) {
                return []
            }
            Version latestNonExcludedVersion = json.response.docs.findAll { doc ->
                new VersionPatternMatcher(doc.v).matchesNoneOf(versionToExcludePatterns)
            }.collect { new Version(it.v) }.max()
            return [dependency, new Dependency(dependency, latestNonExcludedVersion)]
        }
    }

    private String buildQueryString(Dependency dependency) {
        String groupCriteria = escape("g:\"$dependency.group\"")
        String artifactCriteria = escape("a:\"$dependency.name\"")
        return "q=${groupCriteria}+AND+${artifactCriteria}&core=gav&rows10&wt=json"
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy