Search in sources :

Example 6 with RepositorySystemSession

use of org.eclipse.aether.RepositorySystemSession in project spf4j by zolyfarkas.

the class MavenRepositoryUtils method resolveArtifactAndDependencies.

public static Set<File> resolveArtifactAndDependencies(final List<RemoteRepository> repos, final File localRepo, final String scope, final String groupId, final String artifactId, final String classifier, final String extension, final String versionExpr) throws DependencyResolutionException {
    RepositorySystem repositorySystem = getRepositorySystem();
    RepositorySystemSession session = getRepositorySystemSession(repositorySystem, localRepo);
    return resolveArtifactAndDependencies(scope, groupId, artifactId, classifier, extension, versionExpr, repos, repositorySystem, session);
}
Also used : RepositorySystem(org.eclipse.aether.RepositorySystem) RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession)

Example 7 with RepositorySystemSession

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

the class LiferayMavenProjectProvider method getData.

@Override
public <T> List<T> getData(String key, Class<T> type, Object... params) {
    List<T> retval = null;
    if ("profileIds".equals(key)) {
        List<T> profileIds = new ArrayList<>();
        try {
            IMaven maven = MavenPlugin.getMaven();
            List<Profile> profiles = maven.getSettings().getProfiles();
            for (Profile profile : profiles) {
                if (profile.getActivation() != null) {
                    if (profile.getActivation().isActiveByDefault()) {
                        continue;
                    }
                }
                profileIds.add(type.cast(profile.getId()));
            }
            if ((params[0] != null) && params[0] instanceof File) {
                File locationDir = (File) params[0];
                File pomFile = new File(locationDir, IMavenConstants.POM_FILE_NAME);
                if (FileUtil.notExists(pomFile) && FileUtil.exists(locationDir.getParentFile())) {
                    // try one level up for when user is adding new module
                    pomFile = new File(locationDir.getParentFile(), IMavenConstants.POM_FILE_NAME);
                }
                if (FileUtil.exists(pomFile)) {
                    Model model = maven.readModel(pomFile);
                    File parentDir = pomFile.getParentFile();
                    while (model != null) {
                        for (org.apache.maven.model.Profile p : model.getProfiles()) {
                            profileIds.add(type.cast(p.getId()));
                        }
                        parentDir = parentDir.getParentFile();
                        if (FileUtil.exists(parentDir)) {
                            try {
                                model = maven.readModel(new File(parentDir, IMavenConstants.POM_FILE_NAME));
                            } catch (Exception e) {
                                model = null;
                            }
                        }
                    }
                }
            }
        } catch (CoreException ce) {
            LiferayMavenCore.logError(ce);
        }
        retval = profileIds;
    } else if ("liferayVersions".equals(key)) {
        List<T> possibleVersions = new ArrayList<>();
        RepositorySystem system = AetherUtil.newRepositorySystem();
        RepositorySystemSession session = AetherUtil.newRepositorySystemSession(system);
        String groupId = params[0].toString();
        String artifactId = params[1].toString();
        String coords = groupId + ":" + artifactId + ":[6,)";
        Artifact artifact = new DefaultArtifact(coords);
        VersionRangeRequest rangeRequest = new VersionRangeRequest();
        rangeRequest.setArtifact(artifact);
        rangeRequest.addRepository(AetherUtil.newCentralRepository());
        rangeRequest.addRepository(AetherUtil.newLiferayRepository());
        try {
            VersionRangeResult rangeResult = system.resolveVersionRange(session, rangeRequest);
            List<Version> versions = rangeResult.getVersions();
            for (Version version : versions) {
                String val = version.toString();
                if (!"6.2.0".equals(val) && !val.contains("7.0.0")) {
                    possibleVersions.add(type.cast(val));
                }
            }
            retval = possibleVersions;
        } catch (VersionRangeResolutionException vrre) {
        }
    } else if ("parentVersion".equals(key)) {
        List<T> version = new ArrayList<>();
        File locationDir = (File) params[0];
        File parentPom = new File(locationDir, IMavenConstants.POM_FILE_NAME);
        if (FileUtil.exists(parentPom)) {
            try {
                IMaven maven = MavenPlugin.getMaven();
                Model model = maven.readModel(parentPom);
                version.add(type.cast(model.getVersion()));
                retval = version;
            } catch (CoreException ce) {
                LiferayMavenCore.logError("unable to get parent version", ce);
            }
        }
    } else if ("parentGroupId".equals(key)) {
        List<T> groupId = new ArrayList<>();
        File locationDir = (File) params[0];
        File parentPom = new File(locationDir, IMavenConstants.POM_FILE_NAME);
        if (FileUtil.exists(parentPom)) {
            try {
                IMaven maven = MavenPlugin.getMaven();
                Model model = maven.readModel(parentPom);
                groupId.add(type.cast(model.getGroupId()));
                retval = groupId;
            } catch (CoreException ce) {
                LiferayMavenCore.logError("unable to get parent groupId", ce);
            }
        }
    } else if ("archetypeGAV".equals(key)) {
        String frameworkType = (String) params[0];
        String value = LiferayMavenCore.getPreferenceString("archetype-gav-" + frameworkType, "");
        retval = Collections.singletonList(type.cast(value));
    }
    return retval;
}
Also used : RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) VersionRangeResult(org.eclipse.aether.resolution.VersionRangeResult) ArrayList(java.util.ArrayList) NewLiferayProfile(com.liferay.ide.project.core.model.NewLiferayProfile) Profile(org.apache.maven.settings.Profile) CoreException(org.eclipse.core.runtime.CoreException) VersionRangeResolutionException(org.eclipse.aether.resolution.VersionRangeResolutionException) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) RepositorySystem(org.eclipse.aether.RepositorySystem) CoreException(org.eclipse.core.runtime.CoreException) Version(org.eclipse.aether.version.Version) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) Model(org.apache.maven.model.Model) ArrayList(java.util.ArrayList) List(java.util.List) VersionRangeRequest(org.eclipse.aether.resolution.VersionRangeRequest) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IMaven(org.eclipse.m2e.core.embedder.IMaven) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) VersionRangeResolutionException(org.eclipse.aether.resolution.VersionRangeResolutionException)

Example 8 with RepositorySystemSession

use of org.eclipse.aether.RepositorySystemSession in project spring-cloud-deployer by spring-cloud.

the class MavenArtifactResolver method resolve.

/**
 * Resolve an artifact and return its location in the local repository. Aether performs the normal
 * Maven resolution process ensuring that the latest update is cached to the local repository.
 * In addition, if the {@link MavenProperties#resolvePom} flag is <code>true</code>,
 * the POM is also resolved and cached.
 * @param resource the {@link MavenResource} representing the artifact
 * @return a {@link FileSystemResource} representing the resolved artifact in the local repository
 * @throws IllegalStateException if the artifact does not exist or the resolution fails
 */
Resource resolve(MavenResource resource) {
    Assert.notNull(resource, "MavenResource must not be null");
    validateCoordinates(resource);
    RepositorySystemSession session = newRepositorySystemSession(this.repositorySystem, this.properties.getLocalRepository());
    ArtifactResult resolvedArtifact;
    try {
        List<ArtifactRequest> artifactRequests = new ArrayList<>(2);
        if (properties.isResolvePom()) {
            artifactRequests.add(new ArtifactRequest(toPomArtifact(resource), this.remoteRepositories, JavaScopes.RUNTIME));
        }
        artifactRequests.add(new ArtifactRequest(toJarArtifact(resource), this.remoteRepositories, JavaScopes.RUNTIME));
        List<ArtifactResult> results = this.repositorySystem.resolveArtifacts(session, artifactRequests);
        resolvedArtifact = results.get(results.size() - 1);
    } catch (ArtifactResolutionException e) {
        ChoiceFormat pluralizer = new ChoiceFormat(new double[] { 0d, 1d, ChoiceFormat.nextDouble(1d) }, new String[] { "repositories: ", "repository: ", "repositories: " });
        MessageFormat messageFormat = new MessageFormat("Failed to resolve MavenResource: {0}. Configured remote {1}: {2}");
        messageFormat.setFormat(1, pluralizer);
        String repos = properties.getRemoteRepositories().isEmpty() ? "none" : StringUtils.collectionToDelimitedString(properties.getRemoteRepositories().keySet(), ",", "[", "]");
        throw new IllegalStateException(messageFormat.format(new Object[] { resource, properties.getRemoteRepositories().size(), repos }), e);
    }
    return toResource(resolvedArtifact);
}
Also used : RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) MessageFormat(java.text.MessageFormat) ChoiceFormat(java.text.ChoiceFormat) ArrayList(java.util.ArrayList) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult)

Example 9 with RepositorySystemSession

use of org.eclipse.aether.RepositorySystemSession in project launcher by runelite.

the class ArtifactResolver method resolveArtifacts.

public List<ArtifactResult> resolveArtifacts(Artifact artifact) throws DependencyResolutionException {
    RepositorySystem system = newRepositorySystem();
    RepositorySystemSession session = newRepositorySystemSession(system);
    DependencyFilter classpathFlter = DependencyFilterUtils.classpathFilter(JavaScopes.COMPILE, JavaScopes.RUNTIME);
    CollectRequest collectRequest = new CollectRequest();
    collectRequest.setRoot(new Dependency(artifact, JavaScopes.COMPILE));
    collectRequest.setRepositories(repositories);
    DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, classpathFlter);
    List<ArtifactResult> results = system.resolveDependencies(session, dependencyRequest).getArtifactResults();
    // check to see if they're from the right repository
    validate(results);
    return results;
}
Also used : RepositorySystem(org.eclipse.aether.RepositorySystem) RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) DependencyRequest(org.eclipse.aether.resolution.DependencyRequest) DependencyFilter(org.eclipse.aether.graph.DependencyFilter) Dependency(org.eclipse.aether.graph.Dependency) CollectRequest(org.eclipse.aether.collection.CollectRequest) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult)

Example 10 with RepositorySystemSession

use of org.eclipse.aether.RepositorySystemSession in project wildfly-swarm by wildfly-swarm.

the class MavenArtifactResolvingHelperTest method propagateRepositoryPolicies.

@Test
public // SWARM-1173: swarm-plugin trying to download SNAPSHOTs from Maven Central
void propagateRepositoryPolicies() {
    RepositorySystemSession sessionMock = Mockito.mock(RepositorySystemSession.class);
    MirrorSelector mirrorSelectorMock = Mockito.mock(MirrorSelector.class);
    Mockito.when(sessionMock.getMirrorSelector()).thenReturn(mirrorSelectorMock);
    ProxySelector proxySelectorMock = Mockito.mock(ProxySelector.class);
    Mockito.when(sessionMock.getProxySelector()).thenReturn(proxySelectorMock);
    MavenArtifactResolvingHelper artifactResolverHelper = new MavenArtifactResolvingHelper(null, null, sessionMock, null);
    ArtifactRepositoryPolicy testSnapshotPolicy = new ArtifactRepositoryPolicy(false, "hourly", "warn");
    ArtifactRepositoryPolicy testReleasesPolicy = new ArtifactRepositoryPolicy(true, "never", "warn");
    ArtifactRepository testingRepo = new MavenArtifactRepository("testing-repo", "http://testing-repo.org", new DefaultRepositoryLayout(), testSnapshotPolicy, testReleasesPolicy);
    artifactResolverHelper.remoteRepository(testingRepo);
    List<RemoteRepository> remoteRepos = artifactResolverHelper.getRemoteRepositories();
    Assert.assertTrue("Remote repositories " + remoteRepos + " do not contain expected testing repo " + testingRepo, remoteRepos.stream().anyMatch(remoteRepo -> {
        RepositoryPolicy snapshotsPolicy = remoteRepo.getPolicy(true);
        RepositoryPolicy releasesPolicy = remoteRepo.getPolicy(false);
        return remoteRepo.getId().equals(testingRepo.getId()) && !snapshotsPolicy.isEnabled() && snapshotsPolicy.getUpdatePolicy().equals(testSnapshotPolicy.getUpdatePolicy()) && snapshotsPolicy.getChecksumPolicy().equals(testSnapshotPolicy.getChecksumPolicy()) && releasesPolicy.isEnabled() && releasesPolicy.getUpdatePolicy().equals(testReleasesPolicy.getUpdatePolicy()) && releasesPolicy.getChecksumPolicy().equals(testReleasesPolicy.getChecksumPolicy());
    }));
}
Also used : ProxySelector(org.eclipse.aether.repository.ProxySelector) RepositoryPolicy(org.eclipse.aether.repository.RepositoryPolicy) DefaultRepositoryLayout(org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout) ProxySelector(org.eclipse.aether.repository.ProxySelector) Test(org.junit.Test) RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) ArtifactRepositoryPolicy(org.apache.maven.artifact.repository.ArtifactRepositoryPolicy) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) Mockito(org.mockito.Mockito) List(java.util.List) MirrorSelector(org.eclipse.aether.repository.MirrorSelector) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) Assert(org.junit.Assert) MavenArtifactRepository(org.apache.maven.artifact.repository.MavenArtifactRepository) RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) MirrorSelector(org.eclipse.aether.repository.MirrorSelector) ArtifactRepositoryPolicy(org.apache.maven.artifact.repository.ArtifactRepositoryPolicy) RepositoryPolicy(org.eclipse.aether.repository.RepositoryPolicy) ArtifactRepositoryPolicy(org.apache.maven.artifact.repository.ArtifactRepositoryPolicy) DefaultRepositoryLayout(org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout) MavenArtifactRepository(org.apache.maven.artifact.repository.MavenArtifactRepository) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) MavenArtifactRepository(org.apache.maven.artifact.repository.MavenArtifactRepository) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) Test(org.junit.Test)

Aggregations

RepositorySystemSession (org.eclipse.aether.RepositorySystemSession)38 DefaultRepositorySystemSession (org.eclipse.aether.DefaultRepositorySystemSession)19 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)14 RepositorySystem (org.eclipse.aether.RepositorySystem)13 Artifact (org.eclipse.aether.artifact.Artifact)13 ArtifactResult (org.eclipse.aether.resolution.ArtifactResult)13 ArtifactRequest (org.eclipse.aether.resolution.ArtifactRequest)12 RemoteRepository (org.eclipse.aether.repository.RemoteRepository)11 ArrayList (java.util.ArrayList)10 ArtifactResolutionException (org.eclipse.aether.resolution.ArtifactResolutionException)10 File (java.io.File)8 ComponentLookupException (org.codehaus.plexus.component.repository.exception.ComponentLookupException)7 List (java.util.List)5 Artifact (org.apache.maven.artifact.Artifact)5 MavenSession (org.apache.maven.execution.MavenSession)5 IOException (java.io.IOException)4 DefaultMaven (org.apache.maven.DefaultMaven)4 Maven (org.apache.maven.Maven)4 InvalidRepositoryException (org.apache.maven.artifact.InvalidRepositoryException)4 ArtifactRepository (org.apache.maven.artifact.repository.ArtifactRepository)4