diff options
author | Bjørn Christian Seime <bjorncs@verizonmedia.com> | 2020-07-08 13:24:41 +0200 |
---|---|---|
committer | Bjørn Christian Seime <bjorncs@verizonmedia.com> | 2020-07-08 13:24:41 +0200 |
commit | 9b8f0756ecc1a038fbaab4388f74886965a5c02f (patch) | |
tree | cdcc27f7300017635ca200441a62106447fa5aae /bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo | |
parent | 92e778cf7cb3a41c516becd4b2e8cf5cc295b865 (diff) |
Move helper classes from 'mojo' to 'util'
Diffstat (limited to 'bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo')
8 files changed, 10 insertions, 225 deletions
diff --git a/bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo/Artifacts.java b/bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo/Artifacts.java deleted file mode 100644 index 0dbed043de0..00000000000 --- a/bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo/Artifacts.java +++ /dev/null @@ -1,82 +0,0 @@ -// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.container.plugin.mojo; - -import org.apache.maven.artifact.Artifact; -import org.apache.maven.project.MavenProject; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - -/** - * @author Tony Vaagenes - * @author ollivir - */ -class Artifacts { - interface ScopeTranslator { - String scopeOf(Artifact artifact); - } - - static class NoopScopeTranslator implements ScopeTranslator { - @Override public String scopeOf(Artifact artifact) { return artifact.getScope(); } - } - - static class ArtifactSet { - - private final List<Artifact> jarArtifactsToInclude; - private final List<Artifact> jarArtifactsProvided; - private final List<Artifact> nonJarArtifacts; - - private ArtifactSet( - List<Artifact> jarArtifactsToInclude, - List<Artifact> jarArtifactsProvided, - List<Artifact> nonJarArtifacts) { - this.jarArtifactsToInclude = jarArtifactsToInclude; - this.jarArtifactsProvided = jarArtifactsProvided; - this.nonJarArtifacts = nonJarArtifacts; - } - - List<Artifact> getJarArtifactsToInclude() { - return jarArtifactsToInclude; - } - - List<Artifact> getJarArtifactsProvided() { - return jarArtifactsProvided; - } - - List<Artifact> getNonJarArtifacts() { - return nonJarArtifacts; - } - } - - static ArtifactSet getArtifacts(MavenProject project) { return getArtifacts(project, new NoopScopeTranslator()); } - - static ArtifactSet getArtifacts(MavenProject project, ScopeTranslator scopeTranslator) { - List<Artifact> jarArtifactsToInclude = new ArrayList<>(); - List<Artifact> jarArtifactsProvided = new ArrayList<>(); - List<Artifact> nonJarArtifactsToInclude = new ArrayList<>(); - List<Artifact> nonJarArtifactsProvided = new ArrayList<>(); - for (Artifact artifact : project.getArtifacts()) { - String scope = scopeTranslator.scopeOf(artifact); - if ("jar".equals(artifact.getType())) { - if (Artifact.SCOPE_COMPILE.equals(scope)) { - jarArtifactsToInclude.add(artifact); - } else if (Artifact.SCOPE_PROVIDED.equals(scope)) { - jarArtifactsProvided.add(artifact); - } - } else { - if (Artifact.SCOPE_COMPILE.equals(scope)) { - nonJarArtifactsToInclude.add(artifact); - } else if (Artifact.SCOPE_PROVIDED.equals(scope)) { - nonJarArtifactsProvided.add(artifact); - } - } - } - nonJarArtifactsToInclude.addAll(nonJarArtifactsProvided); - return new ArtifactSet(jarArtifactsToInclude, jarArtifactsProvided, nonJarArtifactsToInclude); - } - - static Collection<Artifact> getArtifactsToInclude(MavenProject project) { - return getArtifacts(project, new NoopScopeTranslator()).getJarArtifactsToInclude(); - } -} diff --git a/bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo/AssembleContainerPluginMojo.java b/bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo/AssembleContainerPluginMojo.java index bed7610e82f..441d7b5a7b8 100644 --- a/bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo/AssembleContainerPluginMojo.java +++ b/bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo/AssembleContainerPluginMojo.java @@ -1,6 +1,7 @@ // Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.container.plugin.mojo; +import com.yahoo.container.plugin.util.Artifacts; import org.apache.maven.model.Build; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugins.annotations.Component; diff --git a/bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo/AssembleTestBundleMojo.java b/bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo/AssembleTestBundleMojo.java index ddf382b02dc..98176e62b93 100644 --- a/bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo/AssembleTestBundleMojo.java +++ b/bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo/AssembleTestBundleMojo.java @@ -1,6 +1,8 @@ // Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.container.plugin.mojo; +import com.yahoo.container.plugin.util.Artifacts; +import com.yahoo.container.plugin.util.TestBundleDependencyScopeTranslator; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; @@ -10,8 +12,8 @@ import org.codehaus.plexus.archiver.jar.JarArchiver; import java.nio.file.Path; import java.nio.file.Paths; -import static com.yahoo.container.plugin.mojo.TestBundleUtils.archiveFile; -import static com.yahoo.container.plugin.mojo.TestBundleUtils.manifestFile; +import static com.yahoo.container.plugin.util.TestBundleUtils.archiveFile; +import static com.yahoo.container.plugin.util.TestBundleUtils.manifestFile; /** * @author bjorncs diff --git a/bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo/GenerateBundleClassPathMappingsMojo.java b/bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo/GenerateBundleClassPathMappingsMojo.java index 4d5b0e327d5..e94e05512aa 100644 --- a/bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo/GenerateBundleClassPathMappingsMojo.java +++ b/bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo/GenerateBundleClassPathMappingsMojo.java @@ -5,6 +5,7 @@ import com.google.common.base.Preconditions; import com.yahoo.container.plugin.bundle.AnalyzeBundle; import com.yahoo.container.plugin.osgi.ProjectBundleClassPaths; import com.yahoo.container.plugin.osgi.ProjectBundleClassPaths.BundleClasspathMapping; +import com.yahoo.container.plugin.util.Artifacts; import org.apache.maven.artifact.Artifact; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; diff --git a/bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo/GenerateOsgiManifestMojo.java b/bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo/GenerateOsgiManifestMojo.java index be71c9c149f..892eb9aac05 100644 --- a/bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo/GenerateOsgiManifestMojo.java +++ b/bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo/GenerateOsgiManifestMojo.java @@ -8,6 +8,7 @@ import com.yahoo.container.plugin.classanalysis.PackageTally; import com.yahoo.container.plugin.osgi.ExportPackages; import com.yahoo.container.plugin.osgi.ExportPackages.Export; import com.yahoo.container.plugin.osgi.ImportPackages.Import; +import com.yahoo.container.plugin.util.Artifacts; import org.apache.maven.artifact.Artifact; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugins.annotations.Mojo; diff --git a/bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo/GenerateTestBundleOsgiManifestMojo.java b/bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo/GenerateTestBundleOsgiManifestMojo.java index 0d4b4dbc8a4..819b47aa417 100644 --- a/bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo/GenerateTestBundleOsgiManifestMojo.java +++ b/bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo/GenerateTestBundleOsgiManifestMojo.java @@ -6,6 +6,8 @@ import com.yahoo.container.plugin.classanalysis.ClassFileMetaData; import com.yahoo.container.plugin.classanalysis.PackageTally; import com.yahoo.container.plugin.osgi.ExportPackages.Export; import com.yahoo.container.plugin.osgi.ImportPackages; +import com.yahoo.container.plugin.util.Artifacts; +import com.yahoo.container.plugin.util.TestBundleDependencyScopeTranslator; import org.apache.maven.artifact.Artifact; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugins.annotations.Mojo; @@ -18,7 +20,7 @@ import java.util.Map; import java.util.stream.Stream; import static com.yahoo.container.plugin.bundle.AnalyzeBundle.exportedPackagesAggregated; -import static com.yahoo.container.plugin.mojo.TestBundleUtils.outputDirectory; +import static com.yahoo.container.plugin.util.TestBundleUtils.outputDirectory; import static com.yahoo.container.plugin.osgi.ExportPackages.exportsByPackageName; import static com.yahoo.container.plugin.osgi.ImportPackages.calculateImports; import static com.yahoo.container.plugin.util.Files.allDescendantFiles; diff --git a/bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo/TestBundleDependencyScopeTranslator.java b/bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo/TestBundleDependencyScopeTranslator.java deleted file mode 100644 index 92023df8b23..00000000000 --- a/bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo/TestBundleDependencyScopeTranslator.java +++ /dev/null @@ -1,115 +0,0 @@ -// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.container.plugin.mojo; - -import org.apache.maven.artifact.Artifact; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.logging.Logger; - -import static java.util.stream.Collectors.toList; - -/** - * Translates the scope of dependencies when constructing a test bundle. - * Used by {@link Artifacts} to determine which artifacts that are provided by the runtime or must be included in the bundle. - * - * Dependencies of scope 'test' are by default translated to 'compile'. Dependencies of other scopes are kept as is. - * - * Default scope translation for 'test' scope dependencies can be overridden through a comma-separated configuration string. - * Each substring is a triplet on the form [groupId]:[artifactId]:[scope]. - * Scope translation overrides affects all transitive dependencies. - * The ordering of the triplets determines the priority - only the first matching override will affect a given dependency. - * - * @author bjorncs - */ -class TestBundleDependencyScopeTranslator implements Artifacts.ScopeTranslator { - - private static final Logger log = Logger.getLogger(TestBundleDependencyScopeTranslator.class.getName()); - - private final Map<Artifact, String> dependencyScopes; - - private TestBundleDependencyScopeTranslator(Map<Artifact, String> dependencyScopes) { - this.dependencyScopes = dependencyScopes; - } - - @Override public String scopeOf(Artifact artifact) { return Objects.requireNonNull(dependencyScopes.get(artifact)); } - - static TestBundleDependencyScopeTranslator from(Map<String, Artifact> dependencies, String rawConfig) { - List<DependencyOverride> dependencyOverrides = toDependencyOverrides(rawConfig); - Map<Artifact, String> dependencyScopes = new HashMap<>(); - for (Artifact dependency : dependencies.values()) { - dependencyScopes.put(dependency, getScopeForDependency(dependency, dependencyOverrides, dependencies)); - } - return new TestBundleDependencyScopeTranslator(dependencyScopes); - } - - private static List<DependencyOverride> toDependencyOverrides(String rawConfig) { - if (rawConfig == null || rawConfig.isBlank()) return List.of(); - return Arrays.stream(rawConfig.split(",")) - .map(String::strip) - .filter(s -> !s.isBlank()) - .map(TestBundleDependencyScopeTranslator::toDependencyOverride) - .collect(toList()); - } - - private static DependencyOverride toDependencyOverride(String overrideString) { - String[] elements = overrideString.split(":"); - if (elements.length != 3) { - throw new IllegalArgumentException("Invalid dependency override: " + overrideString); - } - return new DependencyOverride(elements[0], elements[1], elements[2]); - } - - private static String stripVersionAndScope(String idInDependencyTrail) { - int firstDelimiter = idInDependencyTrail.indexOf(':'); - int secondDelimiter = idInDependencyTrail.indexOf(':', firstDelimiter + 1); - return idInDependencyTrail.substring(0, secondDelimiter); - } - - private static String getScopeForDependency( - Artifact dependency, List<DependencyOverride> overrides, Map<String, Artifact> otherArtifacts) { - String oldScope = dependency.getScope(); - if (!oldScope.equals(Artifact.SCOPE_TEST)) return oldScope; - for (DependencyOverride override : overrides) { - for (Artifact dependent : dependencyTrailOf(dependency, otherArtifacts)) { - if (override.isForArtifact(dependent)) { - log.fine(() -> String.format( - "Overriding scope of '%s'; scope '%s' overridden to '%s'", - dependency.getId(), oldScope, override.scope)); - return override.scope; - } - } - } - log.fine(() -> String.format( - "Using default scope translation for '%s'; scope 'test' translated to 'compile'", - dependency.getId())); - return Artifact.SCOPE_COMPILE; - } - - private static List<Artifact> dependencyTrailOf(Artifact artifact, Map<String, Artifact> otherArtifacts) { - return artifact.getDependencyTrail().stream() - .skip(1) // Maven project itself is the first entry - .map(parentId -> otherArtifacts.get(stripVersionAndScope(parentId))) - .filter(Objects::nonNull) - .collect(toList()); - } - - private static class DependencyOverride { - final String groupId; - final String artifactId; - final String scope; - - DependencyOverride(String groupId, String artifactId, String scope) { - this.groupId = groupId; - this.artifactId = artifactId; - this.scope = scope; - } - - boolean isForArtifact(Artifact artifact) { - return artifact.getGroupId().equals(groupId) && artifact.getArtifactId().equals(artifactId); - } - } -} diff --git a/bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo/TestBundleUtils.java b/bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo/TestBundleUtils.java deleted file mode 100644 index 9a3fc89bbd5..00000000000 --- a/bundle-plugin/src/main/java/com/yahoo/container/plugin/mojo/TestBundleUtils.java +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.container.plugin.mojo; - -import org.apache.maven.project.MavenProject; - -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.jar.JarFile; - -/** - * @author bjorncs - */ -class TestBundleUtils { - private TestBundleUtils() {} - - static Path outputDirectory(MavenProject project) { return targetDirectory(project).resolve("test-bundle/"); } - - static Path manifestFile(MavenProject project) { return outputDirectory(project).resolve(JarFile.MANIFEST_NAME); } - - static Path archiveFile(MavenProject project) { - return targetDirectory(project).resolve(project.getBuild().getFinalName() + "-tests.jar"); - } - - private static Path targetDirectory(MavenProject project) { return Paths.get(project.getBuild().getDirectory()); } -} |