From b8b34dce24c42c7577c9572b68651125b8628bc0 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Sun, 24 Mar 2024 23:41:59 +0100 Subject: [PATCH] Project copy/copyTransitive --- .../maven/toolbox/shared/ToolboxCommando.java | 26 ++++++++- .../shared/internal/ToolboxCommandoImpl.java | 24 ++++++++ .../maven/toolbox/plugin/mp/CopyMojo.java | 45 +++++++++++++++ .../toolbox/plugin/mp/CopyTransitiveMojo.java | 56 +++++++++++++++++++ 4 files changed, 149 insertions(+), 2 deletions(-) create mode 100644 toolbox/src/main/java/eu/maveniverse/maven/toolbox/plugin/mp/CopyMojo.java create mode 100644 toolbox/src/main/java/eu/maveniverse/maven/toolbox/plugin/mp/CopyTransitiveMojo.java diff --git a/shared/src/main/java/eu/maveniverse/maven/toolbox/shared/ToolboxCommando.java b/shared/src/main/java/eu/maveniverse/maven/toolbox/shared/ToolboxCommando.java index 516c4486..a645e437 100644 --- a/shared/src/main/java/eu/maveniverse/maven/toolbox/shared/ToolboxCommando.java +++ b/shared/src/main/java/eu/maveniverse/maven/toolbox/shared/ToolboxCommando.java @@ -11,7 +11,7 @@ import eu.maveniverse.maven.mima.context.Context; import eu.maveniverse.maven.mima.context.Runtime; -import eu.maveniverse.maven.toolbox.shared.internal.ToolboxCommandoImpl; +import eu.maveniverse.maven.toolbox.shared.internal.*; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; @@ -52,7 +52,27 @@ default String getVersion() { boolean dump(boolean verbose, Output output); - // Resolver related commands: they target current context contained RemoteRepository + // Parsers + + /** + * Parses artifact mapper string into {@link ArtifactMapper}. + */ + ArtifactMapper parseArtifactMapperSpec(String spec); + + /** + * Parses artifact matcher string into {@link ArtifactMatcher}. + */ + ArtifactMatcher parseArtifactMatcherSpec(String spec); + + /** + * Parses artifact name mapper string into {@link ArtifactNameMapper}. + */ + ArtifactNameMapper parseArtifactNameMapperSpec(String spec); + + /** + * Parses dependency matcher string into {@link DependencyMatcher}. + */ + DependencyMatcher parseDependencyMatcherSpec(String spec); /** * Parses remote repository string into {@link RemoteRepository}. It may be {@code url} only, {@code id::url} or @@ -60,6 +80,8 @@ default String getVersion() { */ RemoteRepository parseRemoteRepository(String spec); + // Resolver related commands: they target current context contained RemoteRepository + /** * Provides {@link ArtifactSink} according to spec. */ diff --git a/shared/src/main/java/eu/maveniverse/maven/toolbox/shared/internal/ToolboxCommandoImpl.java b/shared/src/main/java/eu/maveniverse/maven/toolbox/shared/internal/ToolboxCommandoImpl.java index ff38a1f5..74e8ca61 100644 --- a/shared/src/main/java/eu/maveniverse/maven/toolbox/shared/internal/ToolboxCommandoImpl.java +++ b/shared/src/main/java/eu/maveniverse/maven/toolbox/shared/internal/ToolboxCommandoImpl.java @@ -220,6 +220,30 @@ public boolean dump(boolean verbose, Output output) { return true; } + @Override + public ArtifactMapper parseArtifactMapperSpec(String spec) { + // TODO: do it + throw new RuntimeException("not yet implemented"); + } + + @Override + public ArtifactMatcher parseArtifactMatcherSpec(String spec) { + // TODO: do it + throw new RuntimeException("not yet implemented"); + } + + @Override + public ArtifactNameMapper parseArtifactNameMapperSpec(String spec) { + // TODO: do it + throw new RuntimeException("not yet implemented"); + } + + @Override + public DependencyMatcher parseDependencyMatcherSpec(String spec) { + // TODO: do it + throw new RuntimeException("not yet implemented"); + } + @Override public RemoteRepository parseRemoteRepository(String spec) { return toolboxResolver.parseRemoteRepository(spec); diff --git a/toolbox/src/main/java/eu/maveniverse/maven/toolbox/plugin/mp/CopyMojo.java b/toolbox/src/main/java/eu/maveniverse/maven/toolbox/plugin/mp/CopyMojo.java new file mode 100644 index 00000000..a4629739 --- /dev/null +++ b/toolbox/src/main/java/eu/maveniverse/maven/toolbox/plugin/mp/CopyMojo.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2023-2024 Maveniverse Org. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-v20.html + */ +package eu.maveniverse.maven.toolbox.plugin.mp; + +import eu.maveniverse.maven.toolbox.plugin.MPMojoSupport; +import eu.maveniverse.maven.toolbox.shared.Output; +import eu.maveniverse.maven.toolbox.shared.ToolboxCommando; +import java.util.stream.Collectors; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.eclipse.aether.graph.Dependency; + +/** + * Resolves selected dependencies and copies resulting artifacts to target. + */ +@Mojo(name = "copy", threadSafe = true) +public final class CopyMojo extends MPMojoSupport { + /** + * The target spec. + */ + @Parameter(property = "targetSpec", required = true) + private String targetSpec; + + /** + * The dependency matcher spec. + */ + @Parameter(property = "depSpec", required = true) + private String depSpec; + + @Override + protected boolean doExecute(Output output, ToolboxCommando toolboxCommando) throws Exception { + return toolboxCommando.copy( + projectAsResolutionRoot().getDependencies().stream() + .filter(toolboxCommando.parseDependencyMatcherSpec(depSpec)) + .map(Dependency::getArtifact) + .collect(Collectors.toList()), + toolboxCommando.artifactSink(output, targetSpec), + output); + } +} diff --git a/toolbox/src/main/java/eu/maveniverse/maven/toolbox/plugin/mp/CopyTransitiveMojo.java b/toolbox/src/main/java/eu/maveniverse/maven/toolbox/plugin/mp/CopyTransitiveMojo.java new file mode 100644 index 00000000..9b4d586c --- /dev/null +++ b/toolbox/src/main/java/eu/maveniverse/maven/toolbox/plugin/mp/CopyTransitiveMojo.java @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2023-2024 Maveniverse Org. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-v20.html + */ +package eu.maveniverse.maven.toolbox.plugin.mp; + +import eu.maveniverse.maven.toolbox.plugin.MPMojoSupport; +import eu.maveniverse.maven.toolbox.shared.Output; +import eu.maveniverse.maven.toolbox.shared.ResolutionRoot; +import eu.maveniverse.maven.toolbox.shared.ResolutionScope; +import eu.maveniverse.maven.toolbox.shared.ToolboxCommando; +import java.util.stream.Collectors; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; + +/** + * Resolves selected dependencies transitively and copies all of them to target. + */ +@Mojo(name = "copy-transitive", threadSafe = true) +public final class CopyTransitiveMojo extends MPMojoSupport { + /** + * The target spec. + */ + @Parameter(property = "targetSpec", required = true) + private String targetSpec; + + /** + * The resolution scope to resolve (default is 'runtime'). + */ + @Parameter(property = "scope", defaultValue = "runtime", required = true) + private String scope; + + /** + * The dependency matcher spec. + */ + @Parameter(property = "depSpec", required = true) + private String depSpec; + + @Override + protected boolean doExecute(Output output, ToolboxCommando toolboxCommando) throws Exception { + ResolutionRoot project = projectAsResolutionRoot(); + return toolboxCommando.copyTransitive( + ResolutionScope.parse(scope), + projectAsResolutionRoot().getDependencies().stream() + .filter(toolboxCommando.parseDependencyMatcherSpec(depSpec)) + .map(d -> ResolutionRoot.ofLoaded(d.getArtifact()) + .withManagedDependencies(project.getManagedDependencies()) + .build()) + .collect(Collectors.toList()), + toolboxCommando.artifactSink(output, targetSpec), + output); + } +}