Search in sources :

Example 1 with MavenWorkingSession

use of org.jboss.shrinkwrap.resolver.api.maven.MavenWorkingSession in project component-runtime by Talend.

the class Dependencies method findRepositories.

private static List<MavenRemoteRepository> findRepositories() {
    final List<MavenRemoteRepository> repositories = new ArrayList<>();
    final String repos = System.getProperty("talend.component.junit.maven.repositories");
    if (repos != null && !repos.isEmpty()) {
        for (final String repo : repos.split(",")) {
            final String[] parts = repo.split("::");
            final MavenRemoteRepository repository = MavenRemoteRepositories.createRemoteRepository(parts.length == 1 ? "repo_" + parts[0].replace(':', '_').replace('/', '_') : parts[0], parts.length == 1 ? parts[0] : parts[1], parts.length < 3 ? "default" : parts[2]);
            repository.setUpdatePolicy(MavenUpdatePolicy.UPDATE_POLICY_DAILY);
            repository.setChecksumPolicy(MavenChecksumPolicy.CHECKSUM_POLICY_IGNORE);
            repositories.add(repository);
        }
    } else if (!Boolean.getBoolean("talend.component.junit.maven.skipPomReading")) {
        // try to grab from the root pom the repos - simplifying maven parsing for speed reasons and enough
        // generally
        File current = new File(".").getAbsoluteFile();
        while (new File(current, "pom.xml").exists()) {
            final File parent = current.getParentFile();
            if (parent != null && new File(parent, "pom.xml").exists()) {
                current = parent;
                continue;
            }
            break;
        }
        final File pom = new File(current, "pom.xml");
        if (pom.exists()) {
            final PomEquippedResolveStage localPom = Maven.configureResolver().loadPomFromFile(pom);
            final MavenWorkingSession mavenWorkingSession = PomEquippedResolveStageBaseImpl.class.cast(localPom).getMavenWorkingSession();
            try {
                final Field remoteRepositories = mavenWorkingSession.getClass().getDeclaredField("remoteRepositories");
                if (!remoteRepositories.isAccessible()) {
                    remoteRepositories.setAccessible(true);
                }
                final List<RemoteRepository> pomRepos = List.class.cast(remoteRepositories.get(mavenWorkingSession));
                if (pomRepos != null && !pomRepos.isEmpty()) {
                    pomRepos.forEach(r -> {
                        final MavenRemoteRepository repository = MavenRemoteRepositories.createRemoteRepository(r.getId(), r.getUrl(), "default");
                        repository.setUpdatePolicy(MavenUpdatePolicy.UPDATE_POLICY_DAILY);
                        repository.setChecksumPolicy(MavenChecksumPolicy.CHECKSUM_POLICY_IGNORE);
                        repositories.add(repository);
                    });
                }
            } catch (final Exception e) {
                throw new IllegalStateException("Check your shrinkwrap maven version", e);
            }
        }
    }
    if (!Boolean.getBoolean("talend.component.junit.maven.central.skip")) {
        final MavenRemoteRepository central = MavenRemoteRepositories.createRemoteRepository("central_test_default", "http://repo.maven.apache.org/maven2/", "default");
        central.setChecksumPolicy(MavenChecksumPolicy.CHECKSUM_POLICY_WARN);
        central.setUpdatePolicy(MavenUpdatePolicy.UPDATE_POLICY_NEVER);
        repositories.add(central);
    }
    return repositories;
}
Also used : AcceptScopesStrategy(org.jboss.shrinkwrap.resolver.api.maven.strategy.AcceptScopesStrategy) URL(java.net.URL) MavenResolutionStrategy(org.jboss.shrinkwrap.resolver.api.maven.strategy.MavenResolutionStrategy) MavenChecksumPolicy(org.jboss.shrinkwrap.resolver.api.maven.repository.MavenChecksumPolicy) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) Maven(org.jboss.shrinkwrap.resolver.api.maven.Maven) MavenRemoteRepository(org.jboss.shrinkwrap.resolver.api.maven.repository.MavenRemoteRepository) PomEquippedResolveStageBaseImpl(org.jboss.shrinkwrap.resolver.impl.maven.PomEquippedResolveStageBaseImpl) MavenDependency(org.jboss.shrinkwrap.resolver.api.maven.coordinate.MavenDependency) MavenWorkingSession(org.jboss.shrinkwrap.resolver.api.maven.MavenWorkingSession) ScopeType(org.jboss.shrinkwrap.resolver.api.maven.ScopeType) MavenRemoteRepositories(org.jboss.shrinkwrap.resolver.api.maven.repository.MavenRemoteRepositories) MalformedURLException(java.net.MalformedURLException) PomEquippedResolveStage(org.jboss.shrinkwrap.resolver.api.maven.PomEquippedResolveStage) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Field(java.lang.reflect.Field) File(java.io.File) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) List(java.util.List) Stream(java.util.stream.Stream) ConfigurableMavenResolverSystem(org.jboss.shrinkwrap.resolver.api.maven.ConfigurableMavenResolverSystem) MavenUpdatePolicy(org.jboss.shrinkwrap.resolver.api.maven.repository.MavenUpdatePolicy) PRIVATE(lombok.AccessLevel.PRIVATE) NoArgsConstructor(lombok.NoArgsConstructor) MavenWorkingSession(org.jboss.shrinkwrap.resolver.api.maven.MavenWorkingSession) ArrayList(java.util.ArrayList) PomEquippedResolveStage(org.jboss.shrinkwrap.resolver.api.maven.PomEquippedResolveStage) MalformedURLException(java.net.MalformedURLException) Field(java.lang.reflect.Field) MavenRemoteRepository(org.jboss.shrinkwrap.resolver.api.maven.repository.MavenRemoteRepository) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File)

Example 2 with MavenWorkingSession

use of org.jboss.shrinkwrap.resolver.api.maven.MavenWorkingSession in project syndesis by syndesisio.

the class RepackageExtensionMojo method obtainBomDependencies.

protected Set<MavenDependency> obtainBomDependencies(String urlLocation) throws IOException, MojoExecutionException {
    Artifact artifact = downloadAndInstallArtifact(urlLocation).getArtifact();
    File tempPom = new File(outputDirectory, ".syndesis-extension-plugin-temp-pom");
    try (BufferedWriter out = Files.newBufferedWriter(tempPom.toPath(), StandardCharsets.UTF_8)) {
        Dependency bom = new Dependency();
        bom.setGroupId(artifact.getGroupId());
        bom.setArtifactId(artifact.getArtifactId());
        bom.setVersion(artifact.getVersion());
        bom.setType(artifact.getExtension());
        bom.setScope("import");
        Model bomModel = new Model();
        bomModel.setDependencyManagement(new DependencyManagement());
        bomModel.getDependencyManagement().addDependency(bom);
        bomModel.setRepositories(project.getRepositories());
        MavenProject bomProject = new MavenProject();
        bomProject.setModel(bomModel);
        bomProject.setModelVersion(project.getModelVersion());
        bomProject.setGroupId(project.getGroupId());
        bomProject.setArtifactId(project.getArtifactId() + "-temp-bom");
        bomProject.setVersion(project.getVersion());
        ModelWriter modelWriter = new DefaultModelWriter();
        modelWriter.write(out, Collections.emptyMap(), bomProject.getModel());
        MavenResolverSystem resolver = Maven.resolver();
        resolver.loadPomFromFile(tempPom).importCompileAndRuntimeDependencies();
        MavenWorkingSession session = ((MavenWorkingSessionContainer) resolver).getMavenWorkingSession();
        return session.getDependencyManagement();
    } finally {
        if (!tempPom.delete()) {
            getLog().warn("Cannot delete file " + tempPom);
        }
    }
}
Also used : DefaultModelWriter(org.apache.maven.model.io.DefaultModelWriter) MavenWorkingSession(org.jboss.shrinkwrap.resolver.api.maven.MavenWorkingSession) MavenWorkingSessionContainer(org.jboss.shrinkwrap.resolver.impl.maven.MavenWorkingSessionContainer) MavenProject(org.apache.maven.project.MavenProject) Model(org.apache.maven.model.Model) MavenDependency(org.jboss.shrinkwrap.resolver.api.maven.coordinate.MavenDependency) Dependency(org.apache.maven.model.Dependency) File(java.io.File) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) DependencyManagement(org.apache.maven.model.DependencyManagement) DefaultModelWriter(org.apache.maven.model.io.DefaultModelWriter) ModelWriter(org.apache.maven.model.io.ModelWriter) MavenResolverSystem(org.jboss.shrinkwrap.resolver.api.maven.MavenResolverSystem) BufferedWriter(java.io.BufferedWriter)

Aggregations

File (java.io.File)2 MavenWorkingSession (org.jboss.shrinkwrap.resolver.api.maven.MavenWorkingSession)2 MavenDependency (org.jboss.shrinkwrap.resolver.api.maven.coordinate.MavenDependency)2 BufferedWriter (java.io.BufferedWriter)1 Field (java.lang.reflect.Field)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 Stream (java.util.stream.Stream)1 PRIVATE (lombok.AccessLevel.PRIVATE)1 NoArgsConstructor (lombok.NoArgsConstructor)1 Dependency (org.apache.maven.model.Dependency)1 DependencyManagement (org.apache.maven.model.DependencyManagement)1 Model (org.apache.maven.model.Model)1 DefaultModelWriter (org.apache.maven.model.io.DefaultModelWriter)1 ModelWriter (org.apache.maven.model.io.ModelWriter)1 MavenProject (org.apache.maven.project.MavenProject)1