Search in sources :

Example 1 with ArtifactSpec

use of org.wildfly.swarm.tools.ArtifactSpec in project wildfly-swarm by wildfly-swarm.

the class GradleDependencyAdapter method parseDependencies.

public DeclaredDependencies parseDependencies(Configuration configuration) {
    System.out.println(rootPath);
    GradleConnector connector = GradleConnector.newConnector().forProjectDirectory(rootPath.toFile());
    ProjectConnection connection = connector.connect();
    GradleProject project = connection.getModel(GradleProject.class);
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    connection.newBuild().withArguments("dependencies", "--configuration", configuration.literal).setStandardOutput(bout).run();
    connection.close();
    // parse
    DeclaredDependencies declaredDependencies = new DeclaredDependencies();
    String deps = new String(bout.toByteArray());
    Scanner scanner = new Scanner(deps);
    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        // top level deps
        if (line.startsWith(PREFIX1) || line.startsWith(PREFIX2)) {
            if (stack.size() > 0) {
                stack.pop();
            }
            // parse
            line = parseLine(line);
            String coord = parseCoordinate(line);
            ArtifactSpec parent = DeclaredDependencies.createSpec(coord);
            declaredDependencies.add(parent);
            stack.push(parent);
        } else if (line.contains(PREFIX)) {
            // transient
            line = parseLine(line);
            if (line.startsWith(PROJECT)) {
                // Always skip 'project' dependencies.
                continue;
            }
            String coord = parseCoordinate(line);
            declaredDependencies.add(stack.peek(), DeclaredDependencies.createSpec(coord));
        }
    }
    scanner.close();
    return declaredDependencies;
}
Also used : Scanner(java.util.Scanner) ArtifactSpec(org.wildfly.swarm.tools.ArtifactSpec) DeclaredDependencies(org.wildfly.swarm.tools.DeclaredDependencies) ProjectConnection(org.gradle.tooling.ProjectConnection) GradleProject(org.gradle.tooling.model.GradleProject) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GradleConnector(org.gradle.tooling.GradleConnector)

Example 2 with ArtifactSpec

use of org.wildfly.swarm.tools.ArtifactSpec in project wildfly-swarm by wildfly-swarm.

the class MavenDependencyDeclarationFactory method create.

@Override
public DeclaredDependencies create(FileSystemLayout ignored, ShrinkwrapArtifactResolvingHelper resolvingHelper) {
    final DeclaredDependencies declaredDependencies = new DeclaredDependencies();
    final PomEquippedResolveStage pom = MavenProfileLoader.loadPom(resolvingHelper.getResolver());
    // NonTransitiveStrategy
    final MavenResolvedArtifact[] explicitDeps = resolvingHelper.withResolver(r -> pom.importRuntimeAndTestDependencies().resolve().withoutTransitivity().asResolvedArtifact());
    // TransitiveStrategy
    for (MavenResolvedArtifact directDep : explicitDeps) {
        ArtifactSpec parent = new ArtifactSpec(directDep.getScope().toString(), directDep.getCoordinate().getGroupId(), directDep.getCoordinate().getArtifactId(), directDep.getCoordinate().getVersion(), directDep.getCoordinate().getPackaging().toString(), directDep.getCoordinate().getClassifier(), directDep.asFile());
        MavenResolvedArtifact[] bucket = resolvingHelper.withResolver(r -> {
            r.addDependency(resolvingHelper.createMavenDependency(parent));
            return pom.resolve().withTransitivity().asResolvedArtifact();
        });
        for (MavenResolvedArtifact dep : bucket) {
            ArtifactSpec child = new ArtifactSpec(dep.getScope().toString(), dep.getCoordinate().getGroupId(), dep.getCoordinate().getArtifactId(), dep.getCoordinate().getVersion(), dep.getCoordinate().getPackaging().toString(), dep.getCoordinate().getClassifier(), dep.asFile());
            declaredDependencies.add(parent, child);
        }
    }
    return declaredDependencies;
}
Also used : MavenResolvedArtifact(org.jboss.shrinkwrap.resolver.api.maven.MavenResolvedArtifact) ArtifactSpec(org.wildfly.swarm.tools.ArtifactSpec) DeclaredDependencies(org.wildfly.swarm.tools.DeclaredDependencies) PomEquippedResolveStage(org.jboss.shrinkwrap.resolver.api.maven.PomEquippedResolveStage)

Example 3 with ArtifactSpec

use of org.wildfly.swarm.tools.ArtifactSpec in project wildfly-swarm by wildfly-swarm.

the class MavenArtifactResolvingHelper method resolveAll.

@Override
public Set<ArtifactSpec> resolveAll(Collection<ArtifactSpec> specs, boolean transitive, boolean defaultExcludes) throws Exception {
    if (specs.isEmpty()) {
        return Collections.emptySet();
    }
    List<DependencyNode> nodes;
    if (transitive) {
        final CollectRequest request = new CollectRequest();
        request.setRepositories(this.remoteRepositories);
        // SWARM-1031
        if (this.dependencyManagement != null) {
            List<Dependency> managedDependencies = this.dependencyManagement.getDependencies().stream().map(mavenDep -> {
                DefaultArtifact artifact = new DefaultArtifact(mavenDep.getGroupId(), mavenDep.getArtifactId(), mavenDep.getType(), mavenDep.getVersion());
                return new Dependency(artifact, mavenDep.getScope());
            }).collect(Collectors.toList());
            request.setManagedDependencies(managedDependencies);
        }
        specs.forEach(spec -> request.addDependency(new Dependency(new DefaultArtifact(spec.groupId(), spec.artifactId(), spec.classifier(), spec.type(), spec.version()), "compile")));
        RepositorySystemSession tempSession = new RepositorySystemSessionWrapper(this.session, new ConflictResolver(new NearestVersionSelector(), new JavaScopeSelector(), new SimpleOptionalitySelector(), new JavaScopeDeriver()), defaultExcludes);
        CollectResult result = this.system.collectDependencies(tempSession, request);
        PreorderNodeListGenerator gen = new PreorderNodeListGenerator();
        result.getRoot().accept(gen);
        nodes = gen.getNodes();
    } else {
        nodes = new ArrayList<>();
        for (ArtifactSpec spec : specs) {
            Dependency dependency = new Dependency(new DefaultArtifact(spec.groupId(), spec.artifactId(), spec.classifier(), spec.type(), spec.version()), "compile");
            DefaultDependencyNode node = new DefaultDependencyNode(dependency);
            nodes.add(node);
        }
    }
    List<DependencyNode> extraDependencies = ExtraArtifactsHandler.getExtraDependencies(nodes);
    nodes.addAll(extraDependencies);
    resolveDependenciesInParallel(nodes);
    return nodes.stream().filter(node -> !"system".equals(node.getDependency().getScope())).map(node -> {
        final Artifact artifact = node.getArtifact();
        return new ArtifactSpec(node.getDependency().getScope(), artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getExtension(), artifact.getClassifier(), null);
    }).map(this::resolve).filter(Objects::nonNull).collect(Collectors.toSet());
}
Also used : LocalArtifactRequest(org.eclipse.aether.repository.LocalArtifactRequest) Proxy(org.eclipse.aether.repository.Proxy) Dependency(org.eclipse.aether.graph.Dependency) DependencyManagement(org.apache.maven.model.DependencyManagement) SimpleOptionalitySelector(org.eclipse.aether.util.graph.transformer.SimpleOptionalitySelector) RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) ArtifactRepositoryPolicy(org.apache.maven.artifact.repository.ArtifactRepositoryPolicy) ArrayList(java.util.ArrayList) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) ConflictResolver(org.eclipse.aether.util.graph.transformer.ConflictResolver) JavaScopeSelector(org.eclipse.aether.util.graph.transformer.JavaScopeSelector) NearestVersionSelector(org.eclipse.aether.util.graph.transformer.NearestVersionSelector) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) CollectRequest(org.eclipse.aether.collection.CollectRequest) CollectResult(org.eclipse.aether.collection.CollectResult) DependencyNode(org.eclipse.aether.graph.DependencyNode) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) RepositoryPolicy(org.eclipse.aether.repository.RepositoryPolicy) Collection(java.util.Collection) Authentication(org.apache.maven.artifact.repository.Authentication) Artifact(org.eclipse.aether.artifact.Artifact) JavaScopeDeriver(org.eclipse.aether.util.graph.transformer.JavaScopeDeriver) PreorderNodeListGenerator(org.eclipse.aether.util.graph.visitor.PreorderNodeListGenerator) Set(java.util.Set) LocalArtifactResult(org.eclipse.aether.repository.LocalArtifactResult) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult) DefaultDependencyNode(org.eclipse.aether.graph.DefaultDependencyNode) Collectors(java.util.stream.Collectors) ArtifactResolvingHelper(org.wildfly.swarm.tools.ArtifactResolvingHelper) Objects(java.util.Objects) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) AuthenticationBuilder(org.eclipse.aether.util.repository.AuthenticationBuilder) List(java.util.List) ArtifactResolver(org.eclipse.aether.impl.ArtifactResolver) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) Collections(java.util.Collections) RepositorySystem(org.eclipse.aether.RepositorySystem) ArtifactSpec(org.wildfly.swarm.tools.ArtifactSpec) RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) ConflictResolver(org.eclipse.aether.util.graph.transformer.ConflictResolver) CollectResult(org.eclipse.aether.collection.CollectResult) JavaScopeDeriver(org.eclipse.aether.util.graph.transformer.JavaScopeDeriver) Dependency(org.eclipse.aether.graph.Dependency) CollectRequest(org.eclipse.aether.collection.CollectRequest) PreorderNodeListGenerator(org.eclipse.aether.util.graph.visitor.PreorderNodeListGenerator) SimpleOptionalitySelector(org.eclipse.aether.util.graph.transformer.SimpleOptionalitySelector) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) NearestVersionSelector(org.eclipse.aether.util.graph.transformer.NearestVersionSelector) ArtifactSpec(org.wildfly.swarm.tools.ArtifactSpec) JavaScopeSelector(org.eclipse.aether.util.graph.transformer.JavaScopeSelector) DependencyNode(org.eclipse.aether.graph.DependencyNode) DefaultDependencyNode(org.eclipse.aether.graph.DefaultDependencyNode) DefaultDependencyNode(org.eclipse.aether.graph.DefaultDependencyNode) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Example 4 with ArtifactSpec

use of org.wildfly.swarm.tools.ArtifactSpec in project wildfly-swarm by wildfly-swarm.

the class PackageMojo method executeSpecific.

@SuppressWarnings("deprecation")
@Override
public void executeSpecific() throws MojoExecutionException, MojoFailureException {
    if (this.skip) {
        getLog().info("Skipping packaging");
        return;
    }
    if (this.project.getPackaging().equals("pom")) {
        getLog().info("Not processing project with pom packaging");
        return;
    }
    initProperties(false);
    final Artifact primaryArtifact = this.project.getArtifact();
    final String finalName = this.project.getBuild().getFinalName();
    final String type = primaryArtifact.getType();
    final File primaryArtifactFile = divineFile();
    if (primaryArtifactFile == null) {
        throw new MojoExecutionException("Cannot package without a primary artifact; please `mvn package` prior to invoking wildfly-swarm:package from the command-line");
    }
    final DeclaredDependencies declaredDependencies = new DeclaredDependencies();
    final BuildTool tool = new BuildTool(mavenArtifactResolvingHelper()).projectArtifact(primaryArtifact.getGroupId(), primaryArtifact.getArtifactId(), primaryArtifact.getBaseVersion(), type, primaryArtifactFile, finalName.endsWith("." + type) ? finalName : String.format("%s.%s", finalName, type)).properties(this.properties).mainClass(this.mainClass).bundleDependencies(this.bundleDependencies).executable(executable).executableScript(executableScript).fractionDetectionMode(fractionDetectMode).hollow(hollow).logger(new SimpleLogger() {

        @Override
        public void debug(String msg) {
            getLog().debug(msg);
        }

        @Override
        public void info(String msg) {
            getLog().info(msg);
        }

        @Override
        public void error(String msg) {
            getLog().error(msg);
        }

        @Override
        public void error(String msg, Throwable t) {
            getLog().error(msg, t);
        }
    });
    this.additionalFractions.stream().map(f -> FractionDescriptor.fromGav(FractionList.get(), f)).map(ArtifactSpec::fromFractionDescriptor).forEach(tool::fraction);
    Map<ArtifactSpec, Set<ArtifactSpec>> buckets = createBuckets(this.project.getArtifacts(), this.project.getDependencies());
    for (ArtifactSpec directDep : buckets.keySet()) {
        if (!(directDep.scope.equals("compile") || directDep.scope.equals("runtime"))) {
            // ignore anything but compile and runtime
            continue;
        }
        Set<ArtifactSpec> transientDeps = buckets.get(directDep);
        if (transientDeps.isEmpty()) {
            declaredDependencies.add(directDep);
        } else {
            for (ArtifactSpec transientDep : transientDeps) {
                declaredDependencies.add(directDep, transientDep);
            }
        }
    }
    tool.declaredDependencies(declaredDependencies);
    this.project.getResources().forEach(r -> tool.resourceDirectory(r.getDirectory()));
    Path uberjarResourcesDir = null;
    if (this.uberjarResources == null) {
        uberjarResourcesDir = Paths.get(this.project.getBasedir().toString()).resolve("src").resolve("main").resolve("uberjar");
    } else {
        uberjarResourcesDir = Paths.get(this.uberjarResources);
    }
    tool.uberjarResourcesDirectory(uberjarResourcesDir);
    this.additionalModules.stream().map(m -> new File(this.project.getBuild().getOutputDirectory(), m)).filter(File::exists).map(File::getAbsolutePath).forEach(tool::additionalModule);
    try {
        File jar = tool.build(finalName + (this.hollow ? "-hollow" : ""), Paths.get(this.projectBuildDir));
        ArtifactHandler handler = new DefaultArtifactHandler("jar");
        Artifact swarmJarArtifact = new DefaultArtifact(primaryArtifact.getGroupId(), primaryArtifact.getArtifactId(), primaryArtifact.getBaseVersion(), primaryArtifact.getScope(), "jar", (this.hollow ? "hollow" : "") + "swarm", handler);
        swarmJarArtifact.setFile(jar);
        this.project.addAttachedArtifact(swarmJarArtifact);
        if (this.project.getPackaging().equals("war")) {
            tool.repackageWar(primaryArtifactFile);
        }
    } catch (Exception e) {
        throw new MojoFailureException("Unable to create -swarm.jar", e);
    }
}
Also used : Path(java.nio.file.Path) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact) DefaultArtifactHandler(org.apache.maven.artifact.handler.DefaultArtifactHandler) SimpleLogger(org.wildfly.swarm.spi.meta.SimpleLogger) ArtifactHandler(org.apache.maven.artifact.handler.ArtifactHandler) Files(java.nio.file.Files) DeclaredDependencies(org.wildfly.swarm.tools.DeclaredDependencies) Set(java.util.Set) FractionDescriptor(org.wildfly.swarm.fractions.FractionDescriptor) BuildTool(org.wildfly.swarm.tools.BuildTool) Parameter(org.apache.maven.plugins.annotations.Parameter) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) File(java.io.File) FractionList(org.wildfly.swarm.fractions.FractionList) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Mojo(org.apache.maven.plugins.annotations.Mojo) Paths(java.nio.file.Paths) Map(java.util.Map) Artifact(org.apache.maven.artifact.Artifact) LifecyclePhase(org.apache.maven.plugins.annotations.LifecyclePhase) ResolutionScope(org.apache.maven.plugins.annotations.ResolutionScope) Path(java.nio.file.Path) ArtifactSpec(org.wildfly.swarm.tools.ArtifactSpec) Set(java.util.Set) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) DeclaredDependencies(org.wildfly.swarm.tools.DeclaredDependencies) MojoFailureException(org.apache.maven.plugin.MojoFailureException) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact) Artifact(org.apache.maven.artifact.Artifact) SimpleLogger(org.wildfly.swarm.spi.meta.SimpleLogger) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) DefaultArtifactHandler(org.apache.maven.artifact.handler.DefaultArtifactHandler) ArtifactHandler(org.apache.maven.artifact.handler.ArtifactHandler) ArtifactSpec(org.wildfly.swarm.tools.ArtifactSpec) DefaultArtifactHandler(org.apache.maven.artifact.handler.DefaultArtifactHandler) BuildTool(org.wildfly.swarm.tools.BuildTool) File(java.io.File) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact)

Example 5 with ArtifactSpec

use of org.wildfly.swarm.tools.ArtifactSpec in project wildfly-swarm by wildfly-swarm.

the class StartMojo method findNeededFractions.

List<Path> findNeededFractions(final Set<Artifact> existingDeps, final Path source, final boolean scanDeps) throws MojoFailureException {
    getLog().info("Scanning for needed WildFly Swarm fractions with mode: " + fractionDetectMode);
    final Set<String> existingDepGASet = existingDeps.stream().map(d -> String.format("%s:%s", d.getGroupId(), d.getArtifactId())).collect(Collectors.toSet());
    final Set<FractionDescriptor> fractions;
    final FractionUsageAnalyzer analyzer = new FractionUsageAnalyzer(FractionList.get()).source(source);
    if (scanDeps) {
        existingDeps.forEach(d -> analyzer.source(d.getFile()));
    }
    final Predicate<FractionDescriptor> notExistingDep = d -> !existingDepGASet.contains(String.format("%s:%s", d.getGroupId(), d.getArtifactId()));
    try {
        fractions = analyzer.detectNeededFractions().stream().filter(notExistingDep).collect(Collectors.toSet());
    } catch (IOException e) {
        throw new MojoFailureException("failed to scan for fractions", e);
    }
    getLog().info("Detected fractions: " + String.join(", ", fractions.stream().map(FractionDescriptor::av).sorted().collect(Collectors.toList())));
    fractions.addAll(this.additionalFractions.stream().map(f -> FractionDescriptor.fromGav(FractionList.get(), f)).collect(Collectors.toSet()));
    final Set<FractionDescriptor> allFractions = new HashSet<>(fractions);
    allFractions.addAll(fractions.stream().flatMap(f -> f.getDependencies().stream()).filter(notExistingDep).collect(Collectors.toSet()));
    getLog().info("Using fractions: " + String.join(", ", allFractions.stream().map(FractionDescriptor::gavOrAv).sorted().collect(Collectors.toList())));
    final Set<ArtifactSpec> specs = new HashSet<>();
    specs.addAll(existingDeps.stream().map(this::artifactToArtifactSpec).collect(Collectors.toList()));
    specs.addAll(allFractions.stream().map(ArtifactSpec::fromFractionDescriptor).collect(Collectors.toList()));
    try {
        return mavenArtifactResolvingHelper().resolveAll(specs).stream().map(s -> s.file.toPath()).collect(Collectors.toList());
    } catch (Exception e) {
        throw new MojoFailureException("failed to resolve fraction dependencies", e);
    }
}
Also used : DependencyManager(org.wildfly.swarm.tools.DependencyManager) FractionUsageAnalyzer(org.wildfly.swarm.fractions.FractionUsageAnalyzer) Parameter(org.apache.maven.plugins.annotations.Parameter) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Mojo(org.apache.maven.plugins.annotations.Mojo) StringTokenizer(java.util.StringTokenizer) Artifact(org.apache.maven.artifact.Artifact) ResolutionScope(org.apache.maven.plugins.annotations.ResolutionScope) Path(java.nio.file.Path) SwarmProcess(org.wildfly.swarm.tools.exec.SwarmProcess) Files(java.nio.file.Files) BootstrapProperties(org.wildfly.swarm.bootstrap.util.BootstrapProperties) Predicate(java.util.function.Predicate) DeclaredDependencies(org.wildfly.swarm.tools.DeclaredDependencies) SwarmExecutor(org.wildfly.swarm.tools.exec.SwarmExecutor) Set(java.util.Set) FractionDescriptor(org.wildfly.swarm.fractions.FractionDescriptor) IOException(java.io.IOException) BuildTool(org.wildfly.swarm.tools.BuildTool) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Collectors(java.util.stream.Collectors) File(java.io.File) FractionList(org.wildfly.swarm.fractions.FractionList) MojoFailureException(org.apache.maven.plugin.MojoFailureException) TimeUnit(java.util.concurrent.TimeUnit) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) List(java.util.List) Paths(java.nio.file.Paths) TempFileManager(org.wildfly.swarm.bootstrap.util.TempFileManager) SwarmProperties(org.wildfly.swarm.spi.api.SwarmProperties) ArtifactSpec(org.wildfly.swarm.tools.ArtifactSpec) FractionUsageAnalyzer(org.wildfly.swarm.fractions.FractionUsageAnalyzer) MojoFailureException(org.apache.maven.plugin.MojoFailureException) IOException(java.io.IOException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) ArtifactSpec(org.wildfly.swarm.tools.ArtifactSpec) FractionDescriptor(org.wildfly.swarm.fractions.FractionDescriptor) HashSet(java.util.HashSet)

Aggregations

ArtifactSpec (org.wildfly.swarm.tools.ArtifactSpec)8 Set (java.util.Set)5 DeclaredDependencies (org.wildfly.swarm.tools.DeclaredDependencies)5 File (java.io.File)4 Collectors (java.util.stream.Collectors)4 Path (java.nio.file.Path)3 ArrayList (java.util.ArrayList)3 Collection (java.util.Collection)3 Collections (java.util.Collections)3 ArtifactResolvingHelper (org.wildfly.swarm.tools.ArtifactResolvingHelper)3 BuildTool (org.wildfly.swarm.tools.BuildTool)3 Files (java.nio.file.Files)2 Paths (java.nio.file.Paths)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Map (java.util.Map)2 Artifact (org.apache.maven.artifact.Artifact)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 MojoFailureException (org.apache.maven.plugin.MojoFailureException)2 Mojo (org.apache.maven.plugins.annotations.Mojo)2