Search in sources :

Example 6 with Version

use of org.eclipse.aether.version.Version in project spf4j by zolyfarkas.

the class JDiffRunner method runDiffBetweenReleases.

public void runDiffBetweenReleases(final String groupId, final String artifactId, final String versionRange, final File destinationFolder, final int maxNrOFDiffs) throws DependencyResolutionException, VersionRangeResolutionException, IOException, ArtifactResolutionException, JavadocExecutionException {
    JDiffRunner jdiff = new JDiffRunner();
    List<Version> rangeVersions = MavenRepositoryUtils.getVersions(groupId, artifactId, versionRange, remoteRepos, repositorySystem, reposSession);
    int size = rangeVersions.size();
    if (size < 2) {
        return;
    }
    LinkedList<Version> versions = new LinkedList<>();
    versions.add(rangeVersions.get(size - 1));
    for (int i = size - 2, j = 1; i >= 0 && j < maxNrOFDiffs; i--) {
        Version ver = rangeVersions.get(i);
        if (ver.toString().contains("SNAPSHOT")) {
            continue;
        }
        versions.addFirst(ver);
        j++;
    }
    Version v = versions.get(0);
    File prevSourcesArtifact = MavenRepositoryUtils.resolveArtifact(groupId, artifactId, "sources", "jar", v.toString(), remoteRepos, repositorySystem, reposSession);
    File prevJavaDocArtifact = MavenRepositoryUtils.resolveArtifact(groupId, artifactId, "javadoc", "jar", v.toString(), remoteRepos, repositorySystem, reposSession);
    Path tempDir = Files.createTempDirectory("jdiff");
    try {
        Path sourceDestination = tempDir.resolve(artifactId).resolve(v.toString()).resolve("sources");
        Compress.unzip(prevSourcesArtifact.toPath(), sourceDestination);
        Set<File> deps = MavenRepositoryUtils.resolveArtifactAndDependencies("compile", groupId, artifactId, null, "jar", v.toString(), remoteRepos, repositorySystem, reposSession);
        String prevApiName = artifactId + '-' + v;
        Set<String> prevPackages = jdiff.generateJDiffXML(Collections.singletonList(sourceDestination.toFile()), deps, destinationFolder, prevApiName, null);
        for (int i = 1, l = versions.size(); i < l; i++) {
            v = versions.get(i);
            File sourceArtifact = MavenRepositoryUtils.resolveArtifact(groupId, artifactId, "sources", "jar", v.toString(), remoteRepos, repositorySystem, reposSession);
            File javadocArtifact = MavenRepositoryUtils.resolveArtifact(groupId, artifactId, "javadoc", "jar", v.toString(), remoteRepos, repositorySystem, reposSession);
            sourceDestination = tempDir.resolve(artifactId).resolve(v.toString()).resolve("sources");
            Compress.unzip(sourceArtifact.toPath(), sourceDestination);
            deps = MavenRepositoryUtils.resolveArtifactAndDependencies("compile", groupId, artifactId, null, "jar", v.toString(), remoteRepos, repositorySystem, reposSession);
            String apiName = artifactId + '-' + v;
            Set<String> packages = jdiff.generateJDiffXML(Collections.singletonList(sourceDestination.toFile()), deps, destinationFolder, apiName, null);
            prevPackages.addAll(packages);
            Path reportsDestination = destinationFolder.toPath().resolve(prevApiName + '_' + apiName);
            Compress.unzip(prevJavaDocArtifact.toPath(), reportsDestination.resolve(prevApiName));
            Compress.unzip(javadocArtifact.toPath(), reportsDestination.resolve(apiName));
            jdiff.generateReport(destinationFolder, prevApiName, apiName, "../" + prevApiName + '/', "../" + apiName + '/', prevPackages, reportsDestination.toFile());
            prevApiName = apiName;
            prevPackages = packages;
            prevJavaDocArtifact = javadocArtifact;
        }
    } finally {
        FileUtils.deleteDirectory(tempDir.toFile());
    }
}
Also used : Path(java.nio.file.Path) Version(org.eclipse.aether.version.Version) File(java.io.File) LinkedList(java.util.LinkedList)

Example 7 with Version

use of org.eclipse.aether.version.Version in project spf4j by zolyfarkas.

the class SchemaCompileMojo method loadPrevReleaseId2Map.

@SuppressWarnings("unchecked")
private void loadPrevReleaseId2Map() throws IOException {
    MavenProject mavenProject = getMavenProject();
    Log log = getLog();
    String versionRange = "[," + mavenProject.getVersion() + ')';
    String groupId = mavenProject.getGroupId();
    String artifactId = mavenProject.getArtifactId();
    List<RemoteRepository> remoteProjectRepositories = mavenProject.getRemoteProjectRepositories();
    RepositorySystem repoSystem = getRepoSystem();
    RepositorySystemSession repositorySession = getMavenSession().getRepositorySession();
    List<Version> rangeVersions;
    try {
        rangeVersions = MavenRepositoryUtils.getVersions(groupId, artifactId, versionRange, remoteProjectRepositories, repoSystem, repositorySession);
    } catch (VersionRangeResolutionException ex) {
        throw new RuntimeException("Invalid compatibiliy.versionRange = " + versionRange + " setting", ex);
    }
    rangeVersions = rangeVersions.stream().filter((v) -> !v.toString().endsWith("SNAPSHOT")).collect(Collectors.toList());
    int tSize = rangeVersions.size();
    rangeVersions = rangeVersions.subList(Math.max(tSize - 1, 0), tSize);
    log.info("Loading id 2 name map from " + rangeVersions);
    if (rangeVersions.isEmpty()) {
        return;
    }
    Version version = rangeVersions.get(0);
    Path targetPath = getTarget().toPath();
    File prevSchemaArchive;
    try {
        prevSchemaArchive = MavenRepositoryUtils.resolveArtifact(groupId, artifactId, schemaArtifactClassifier, schemaArtifactExtension, version.toString(), remoteProjectRepositories, repoSystem, repositorySession);
    } catch (ArtifactResolutionException ex) {
        throw new RuntimeException("Cannot resolve previous version " + version, ex);
    }
    Path dest = targetPath.resolve("prevSchema").resolve(version.toString());
    Files.createDirectories(dest);
    log.debug("Unzipping " + prevSchemaArchive + " to " + dest);
    List<Path> indexFiles = Compress.unzip2(prevSchemaArchive.toPath(), dest, (Path p) -> {
        Path fileName = p.getFileName();
        if (fileName == null) {
            return false;
        }
        return SCHEMA_INDEX_FILENAME.equals(fileName.toString());
    });
    Properties prevIndex = new Properties();
    if (indexFiles.size() != 1) {
        log.info("no index file or to many in previous version: " + indexFiles);
    } else {
        // load previous index file
        Path indexFile = indexFiles.get(0);
        try (BufferedReader br = Files.newBufferedReader(indexFile, StandardCharsets.UTF_8)) {
            prevIndex.load(br);
        }
    }
    for (Map.Entry<String, String> entry : (Set<Map.Entry<String, String>>) (Set) prevIndex.entrySet()) {
        String key = entry.getKey();
        if (SCHEMA_INDEX_PGK_KEY.equals(key)) {
            continue;
        }
        int idx = Integer.parseInt(key, 32);
        if (idx >= idSequence) {
            idSequence = idx + 1;
        }
        prevReleaseName2Index.put(entry.getValue(), idx);
    }
    log.debug("loaded existing mappings: " + prevReleaseName2Index);
    log.info("loaded existing mappings, new id sequence: " + idSequence);
}
Also used : Path(java.nio.file.Path) RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) FileSet(org.apache.maven.shared.model.fileset.FileSet) Set(java.util.Set) HashSet(java.util.HashSet) Log(org.apache.maven.plugin.logging.Log) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) Properties(java.util.Properties) RepositorySystem(org.eclipse.aether.RepositorySystem) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) MavenProject(org.apache.maven.project.MavenProject) Version(org.eclipse.aether.version.Version) BufferedReader(java.io.BufferedReader) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap) VersionRangeResolutionException(org.eclipse.aether.resolution.VersionRangeResolutionException)

Example 8 with Version

use of org.eclipse.aether.version.Version in project spf4j by zolyfarkas.

the class SchemaCompatibilityValidator method validate.

@Override
@SuppressFBWarnings("PRMC_POSSIBLY_REDUNDANT_METHOD_CALLS")
public Result validate(final Void nv, final ValidatorMojo mojo) throws IOException {
    // loop through dependencies.
    MavenProject mavenProject = mojo.getMavenProject();
    Map<String, String> validatorConfigs = mojo.getValidatorConfigs();
    Log log = mojo.getLog();
    String versionRange = validatorConfigs.get("versionRange");
    if (versionRange == null) {
        versionRange = "[," + mavenProject.getVersion() + ')';
    }
    int maxNrVersToCheck = 30;
    String strNrVer = validatorConfigs.get("maxNrOfVersionsToCheckForCompatibility");
    if (strNrVer != null) {
        maxNrVersToCheck = Integer.parseInt(strNrVer);
    }
    Instant instantToGoBack;
    String strNrDays = validatorConfigs.get("maxNrOfDaysBackCheckForCompatibility");
    if (strNrDays == null) {
        instantToGoBack = Instant.now().atOffset(ZoneOffset.UTC).minus(1, ChronoUnit.YEARS).toInstant();
    } else {
        instantToGoBack = Instant.now().atOffset(ZoneOffset.UTC).minus(Integer.parseInt(strNrDays), ChronoUnit.DAYS).toInstant();
    }
    String groupId = mavenProject.getGroupId();
    String artifactId = mavenProject.getArtifactId();
    List<RemoteRepository> remoteProjectRepositories = mavenProject.getRemoteProjectRepositories();
    RepositorySystem repoSystem = mojo.getRepoSystem();
    RepositorySystemSession repositorySession = mojo.getMavenSession().getRepositorySession();
    List<Version> rangeVersions;
    try {
        rangeVersions = MavenRepositoryUtils.getVersions(groupId, artifactId, versionRange, remoteProjectRepositories, repoSystem, repositorySession);
    } catch (VersionRangeResolutionException ex) {
        throw new RuntimeException("Invalid compatibiliy.versionRange = " + versionRange + " setting", ex);
    }
    rangeVersions = rangeVersions.stream().filter((v) -> !v.toString().endsWith("SNAPSHOT")).collect(Collectors.toList());
    int tSize = rangeVersions.size();
    rangeVersions = rangeVersions.subList(Math.max(tSize - maxNrVersToCheck, 0), tSize);
    log.info("Validating compatibility with previous versions " + rangeVersions + " newer than " + instantToGoBack);
    if (rangeVersions.isEmpty()) {
        return Result.valid();
    }
    String schemaArtifactClassifier = validatorConfigs.get("schemaArtifactClassifier");
    if (schemaArtifactClassifier != null && schemaArtifactClassifier.trim().isEmpty()) {
        schemaArtifactClassifier = null;
    }
    String schemaArtifactExtension = validatorConfigs.get("schemaArtifactExtension");
    if (schemaArtifactExtension == null || schemaArtifactExtension.trim().isEmpty()) {
        schemaArtifactExtension = "jar";
    }
    List<String> issues = new ArrayList<>(4);
    int size = rangeVersions.size();
    for (int i = size - 1; i >= 0; i--) {
        Version version = rangeVersions.get(i);
        validateCompatibility(groupId, artifactId, schemaArtifactClassifier, schemaArtifactExtension, version, remoteProjectRepositories, repoSystem, repositorySession, mojo, Boolean.parseBoolean(validatorConfigs.getOrDefault("deprecationRemoval", "true")), instantToGoBack, issues::add);
    }
    if (issues.isEmpty()) {
        return Result.valid();
    } else {
        return Result.failed("Schema compatibility issues:\n" + String.join("\n", issues));
    }
}
Also used : RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) Log(org.apache.maven.plugin.logging.Log) Instant(java.time.Instant) ArrayList(java.util.ArrayList) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) RepositorySystem(org.eclipse.aether.RepositorySystem) MavenProject(org.apache.maven.project.MavenProject) Version(org.eclipse.aether.version.Version) VersionRangeResolutionException(org.eclipse.aether.resolution.VersionRangeResolutionException) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 9 with Version

use of org.eclipse.aether.version.Version in project liferay-ide by liferay.

the class AetherUtil method getLatestVersion.

public static String getLatestVersion(String gavCoords, RepositorySystem system, RepositorySystemSession session) {
    String retval = null;
    String[] gav = gavCoords.split(":");
    if ((gav == null) || (gav.length != 3)) {
        throw new IllegalArgumentException("gavCoords should be group:artifactId:version");
    }
    Artifact artifact = new DefaultArtifact(gav[0] + ":" + gav[1] + ":[" + gav[2] + ",)");
    VersionRangeRequest rangeRequest = new VersionRangeRequest();
    rangeRequest.setArtifact(artifact);
    rangeRequest.addRepository(newCentralRepository());
    try {
        VersionRangeResult rangeResult = system.resolveVersionRange(session, rangeRequest);
        Version newestVersion = rangeResult.getHighestVersion();
        List<Version> versions = rangeResult.getVersions();
        if ((versions.size() > 1) && newestVersion.toString().endsWith("-SNAPSHOT")) {
            retval = versions.get(versions.size() - 2).toString();
        } else if (newestVersion != null) {
            retval = newestVersion.toString();
        }
    } catch (VersionRangeResolutionException vrre) {
        LiferayMavenCore.logError("Unable to get latest artifact version.", vrre);
    }
    if (retval == null) {
        retval = gav[2];
    }
    return retval;
}
Also used : VersionRangeResult(org.eclipse.aether.resolution.VersionRangeResult) Version(org.eclipse.aether.version.Version) VersionRangeRequest(org.eclipse.aether.resolution.VersionRangeRequest) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) VersionRangeResolutionException(org.eclipse.aether.resolution.VersionRangeResolutionException)

Example 10 with Version

use of org.eclipse.aether.version.Version in project revapi by revapi.

the class ArtifactResolver method resolveNewestMatching.

/**
 * Tries to find the newest version of the artifact that matches given regular expression.
 * The found version will be older than the {@code upToVersion} or newest available if {@code upToVersion} is null.
 *
 * @param gav the coordinates of the artifact. The version part is ignored
 * @param upToVersion the version up to which the versions will be matched
 * @param versionMatcher the matcher to match the version
 * @param remoteOnly true if only remotely available artifacts should be considered
 * @param upToInclusive whether the {@code upToVersion} should be considered inclusive or exclusive
 * @return the resolved artifact
 * @throws VersionRangeResolutionException
 */
public Artifact resolveNewestMatching(String gav, @Nullable String upToVersion, Pattern versionMatcher, boolean remoteOnly, boolean upToInclusive) throws VersionRangeResolutionException, ArtifactResolutionException {
    Artifact artifact = new DefaultArtifact(gav);
    artifact = artifact.setVersion(upToVersion == null ? "[,)" : "[," + upToVersion + (upToInclusive ? "]" : ")"));
    VersionRangeRequest rangeRequest = new VersionRangeRequest(artifact, repositories, null);
    RepositorySystemSession session = remoteOnly ? makeRemoteOnly(this.session) : this.session;
    VersionRangeResult result = repositorySystem.resolveVersionRange(session, rangeRequest);
    List<Version> versions = new ArrayList<>(result.getVersions());
    Collections.reverse(versions);
    for (Version v : versions) {
        if (versionMatcher.matcher(v.toString()).matches()) {
            return resolveArtifact(artifact.setVersion(v.toString()), session);
        }
    }
    throw new VersionRangeResolutionException(result) {

        @Override
        public String getMessage() {
            return "Failed to find a version of artifact '" + gav + "' that would correspond to an expression '" + versionMatcher + "'. The versions found were: " + versions;
        }
    };
}
Also used : AbstractForwardingRepositorySystemSession(org.eclipse.aether.AbstractForwardingRepositorySystemSession) RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) VersionRangeResult(org.eclipse.aether.resolution.VersionRangeResult) Version(org.eclipse.aether.version.Version) ArrayList(java.util.ArrayList) VersionRangeRequest(org.eclipse.aether.resolution.VersionRangeRequest) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) VersionRangeResolutionException(org.eclipse.aether.resolution.VersionRangeResolutionException)

Aggregations

Version (org.eclipse.aether.version.Version)17 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)9 VersionRangeResolutionException (org.eclipse.aether.resolution.VersionRangeResolutionException)9 Artifact (org.eclipse.aether.artifact.Artifact)8 VersionRangeRequest (org.eclipse.aether.resolution.VersionRangeRequest)8 VersionRangeResult (org.eclipse.aether.resolution.VersionRangeResult)8 File (java.io.File)7 ArrayList (java.util.ArrayList)6 RepositorySystemSession (org.eclipse.aether.RepositorySystemSession)4 Path (java.nio.file.Path)3 Map (java.util.Map)3 DefaultArtifactVersion (org.apache.maven.artifact.versioning.DefaultArtifactVersion)3 Model (org.apache.maven.model.Model)3 SPluginBundleVersion (org.bimserver.interfaces.objects.SPluginBundleVersion)3 RepositorySystem (org.eclipse.aether.RepositorySystem)3 RemoteRepository (org.eclipse.aether.repository.RemoteRepository)3 ArtifactResolutionException (org.eclipse.aether.resolution.ArtifactResolutionException)3 FileNotFoundException (java.io.FileNotFoundException)2 FileReader (java.io.FileReader)2 IOException (java.io.IOException)2