From 186e84651c35535b9583fec7282a26aed0120a2d Mon Sep 17 00:00:00 2001 From: Jared Stehler Date: Sun, 10 Mar 2024 13:56:49 -0400 Subject: [PATCH 1/3] upgrade basepom, apply prettier formatting updates --- .build-jdk11 | 0 pom.xml | 102 +++--- .../hubspot/slimfast/hadoop/HadoopHelper.java | 48 ++- .../hadoop/SlimfastHadoopConfiguration.java | 28 +- slimfast-plugin/pom.xml | 56 ++-- .../plugins/slimfast/ArtifactHelper.java | 75 +++-- .../plugins/slimfast/BaseFileUploader.java | 35 ++- .../slimfast/ClasspathConfiguration.java | 1 + .../maven/plugins/slimfast/CopyJarsMojo.java | 23 +- .../slimfast/DefaultFileDownloader.java | 67 +++- .../plugins/slimfast/DefaultFileUploader.java | 19 +- .../slimfast/DownloadConfiguration.java | 16 +- .../plugins/slimfast/DownloadJarsMojo.java | 79 +++-- .../plugins/slimfast/FileDownloader.java | 6 +- .../maven/plugins/slimfast/FileHelper.java | 13 +- .../maven/plugins/slimfast/FileUploader.java | 6 +- .../slimfast/FromManifestUploader.java | 8 +- .../maven/plugins/slimfast/JsonHelper.java | 25 +- .../maven/plugins/slimfast/LocalArtifact.java | 1 + .../slimfast/LocalArtifactWrapper.java | 1 + .../plugins/slimfast/PreparedArtifact.java | 25 +- .../slimfast/PreparedArtifactWrapper.java | 25 +- .../maven/plugins/slimfast/S3Artifact.java | 13 +- .../plugins/slimfast/S3ArtifactWrapper.java | 1 + .../maven/plugins/slimfast/S3Factory.java | 19 +- .../plugins/slimfast/UploadConfiguration.java | 17 +- .../slimfast/UploadFromManifestMojo.java | 293 ++++++++++-------- .../plugins/slimfast/UploadJarsMojo.java | 120 ++++--- .../plugins/slimfast/WriteManifestMojo.java | 104 ++++--- spotbugs-exclude-filter.xml | 12 + 30 files changed, 783 insertions(+), 455 deletions(-) delete mode 100644 .build-jdk11 create mode 100644 spotbugs-exclude-filter.xml diff --git a/.build-jdk11 b/.build-jdk11 deleted file mode 100644 index e69de29..0000000 diff --git a/pom.xml b/pom.xml index bfcc5d5..3c54b82 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.hubspot basepom - 25.3 + 59.2-SNAPSHOT com.hubspot.slimfast @@ -13,97 +13,99 @@ 0.22-SNAPSHOT pom - - 3 - - slimfast-plugin slimfast-hadoop - 1.11.461 - 4.5.5 - 4.4.9 + 3.6.3 + + com.amazonaws + aws-java-sdk-s3 + ${aws.sdk.version} + + + com.googlecode.json-simple + json-simple + 1.1.1 + + + com.hubspot + failsafe-s3-decorator + 0.7 + + + org.apache.hadoop + hadoop-common + 2.7.2 + + + commons-collections + commons-collections + + + org.apache.maven maven-archiver - 3.0.0 + 3.5.0 org.apache.maven maven-artifact - 3.3.3 + ${dep.maven.version} org.apache.maven maven-core - 3.3.3 + ${dep.maven.version} org.apache.maven maven-model - 3.3.3 + ${dep.maven.version} org.apache.maven maven-plugin-api - 3.3.3 + ${dep.maven.version} org.apache.maven.plugin-tools maven-plugin-annotations - 3.4 + 3.11.0 org.codehaus.plexus plexus-interpolation - 1.22 - - - com.amazonaws - aws-java-sdk-s3 - ${aws.sdk.version} - - - com.hubspot - failsafe-s3-decorator - 0.7 - - - com.googlecode.json-simple - json-simple - 1.1.1 - - - org.apache.hadoop - hadoop-common - 2.7.2 - - - commons-collections - commons-collections - - + 1.25 - - - - org.apache.maven.plugins - maven-plugin-plugin - 3.4 - - - + + + com.github.spotbugs + spotbugs-maven-plugin + + ${project.parent.basedir}/spotbugs-exclude-filter.xml + + + + + check + + package + + + + diff --git a/slimfast-hadoop/src/main/java/com/hubspot/slimfast/hadoop/HadoopHelper.java b/slimfast-hadoop/src/main/java/com/hubspot/slimfast/hadoop/HadoopHelper.java index f27043e..4097b77 100644 --- a/slimfast-hadoop/src/main/java/com/hubspot/slimfast/hadoop/HadoopHelper.java +++ b/slimfast-hadoop/src/main/java/com/hubspot/slimfast/hadoop/HadoopHelper.java @@ -1,12 +1,5 @@ package com.hubspot.slimfast.hadoop; -import org.apache.hadoop.conf.Configuration; -import org.apache.hadoop.fs.FileStatus; -import org.apache.hadoop.fs.FileSystem; -import org.apache.hadoop.fs.Path; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; @@ -15,20 +8,33 @@ import java.util.LinkedHashSet; import java.util.Set; import java.util.jar.Manifest; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FileStatus; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class HadoopHelper { + private static final Logger LOG = LoggerFactory.getLogger(HadoopHelper.class); - public static void writeJarsToHdfsAndAddToClasspath(SlimfastHadoopConfiguration slimfastConfiguration) { + public static void writeJarsToHdfsAndAddToClasspath( + SlimfastHadoopConfiguration slimfastConfiguration + ) { try { FileSystem hdfs = FileSystem.get(slimfastConfiguration.getConfiguration()); for (String jar : findClasspathJars(slimfastConfiguration.getJarDirectory())) { - Path destination = new Path(slimfastConfiguration.getHdfsArtifactRoot().resolve(jar).toString()); + Path destination = new Path( + slimfastConfiguration.getHdfsArtifactRoot().resolve(jar).toString() + ); if (exists(hdfs, destination)) { LOG.info("Path already exists {}", destination); } else { - Path source = new Path(slimfastConfiguration.getJarDirectory().resolve(jar).toString()); + Path source = new Path( + slimfastConfiguration.getJarDirectory().resolve(jar).toString() + ); hdfs.copyFromLocalFile(source, destination); LOG.info("Successfully uploaded path {}", destination); } @@ -40,15 +46,20 @@ public static void writeJarsToHdfsAndAddToClasspath(SlimfastHadoopConfiguration } } - private static void addJarToJobConfiguration(Path jarPath, Configuration configuration) throws IOException { + private static void addJarToJobConfiguration(Path jarPath, Configuration configuration) + throws IOException { String jar = jarPath.toString(); String existingClasspath = configuration.get("mapreduce.job.classpath.files"); - String updatedClasspath = existingClasspath == null ? jar : existingClasspath + "," + jar; + String updatedClasspath = existingClasspath == null + ? jar + : existingClasspath + "," + jar; configuration.set("mapreduce.job.classpath.files", updatedClasspath); jar = FileSystem.get(configuration).makeQualified(jarPath).toUri().toString(); String existingCacheFiles = configuration.get("mapreduce.job.cache.files"); - String updatedCacheFiles = existingCacheFiles == null ? jar : existingCacheFiles + "," + jar; + String updatedCacheFiles = existingCacheFiles == null + ? jar + : existingCacheFiles + "," + jar; configuration.set("mapreduce.job.cache.files", updatedCacheFiles); } @@ -61,9 +72,12 @@ private static boolean exists(FileSystem fileSystem, Path path) throws IOExcepti } } - private static Set findClasspathJars(java.nio.file.Path jarDirectory) throws IOException { + private static Set findClasspathJars(java.nio.file.Path jarDirectory) + throws IOException { Set classpathJars = new LinkedHashSet<>(); - for (URL url : Collections.list(getClassLoader().getResources("META-INF/MANIFEST.MF"))) { + for (URL url : Collections.list( + getClassLoader().getResources("META-INF/MANIFEST.MF") + )) { try (InputStream manifestStream = url.openStream()) { Manifest manifest = new Manifest(manifestStream); String classPath = manifest.getMainAttributes().getValue("Class-Path"); @@ -82,6 +96,8 @@ private static Set findClasspathJars(java.nio.file.Path jarDirectory) th private static ClassLoader getClassLoader() { ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); - return contextClassLoader == null ? HadoopHelper.class.getClassLoader() : contextClassLoader; + return contextClassLoader == null + ? HadoopHelper.class.getClassLoader() + : contextClassLoader; } } diff --git a/slimfast-hadoop/src/main/java/com/hubspot/slimfast/hadoop/SlimfastHadoopConfiguration.java b/slimfast-hadoop/src/main/java/com/hubspot/slimfast/hadoop/SlimfastHadoopConfiguration.java index eadaaff..93e2884 100644 --- a/slimfast-hadoop/src/main/java/com/hubspot/slimfast/hadoop/SlimfastHadoopConfiguration.java +++ b/slimfast-hadoop/src/main/java/com/hubspot/slimfast/hadoop/SlimfastHadoopConfiguration.java @@ -3,15 +3,19 @@ import java.net.URL; import java.nio.file.Path; import java.nio.file.Paths; - import org.apache.hadoop.conf.Configuration; public class SlimfastHadoopConfiguration { + private final Path jarDirectory; private final Path hdfsArtifactRoot; private final Configuration configuration; - private SlimfastHadoopConfiguration(Path jarDirectory, Path hdfsArtifactRoot, Configuration configuration) { + private SlimfastHadoopConfiguration( + Path jarDirectory, + Path hdfsArtifactRoot, + Configuration configuration + ) { this.jarDirectory = jarDirectory; this.hdfsArtifactRoot = hdfsArtifactRoot; this.configuration = configuration; @@ -34,22 +38,30 @@ public Configuration getConfiguration() { } public static class Builder { + private Path jarDirectory; private Path hdfsArtifactRoot = Paths.get("jars"); private Configuration configuration; public Builder setJarByClass(Class jarClass) { - URL url = jarClass.getResource("/" + jarClass.getName().replace('.', '/') + ".class"); + URL url = jarClass.getResource( + "/" + jarClass.getName().replace('.', '/') + ".class" + ); if (url == null) { throw new IllegalStateException("Could not find resource " + jarClass); } String qualifiedPath = url.toString(); if (!qualifiedPath.startsWith("jar:file:")) { - throw new IllegalStateException("Class doesn't appear to be in a JAR, are you running from a JAR?"); + throw new IllegalStateException( + "Class doesn't appear to be in a JAR, are you running from a JAR?" + ); } - String jarPath = qualifiedPath.substring("jar:file:".length(), qualifiedPath.indexOf('!')); + String jarPath = qualifiedPath.substring( + "jar:file:".length(), + qualifiedPath.indexOf('!') + ); return setJarDirectory(Paths.get(jarPath).getParent()); } @@ -77,7 +89,11 @@ public SlimfastHadoopConfiguration build() { throw new IllegalStateException("configuration must be set"); } - return new SlimfastHadoopConfiguration(jarDirectory, hdfsArtifactRoot, configuration); + return new SlimfastHadoopConfiguration( + jarDirectory, + hdfsArtifactRoot, + configuration + ); } } } diff --git a/slimfast-plugin/pom.xml b/slimfast-plugin/pom.xml index 5516a0f..1ce9203 100644 --- a/slimfast-plugin/pom.xml +++ b/slimfast-plugin/pom.xml @@ -14,55 +14,67 @@ - org.apache.maven - maven-archiver + com.amazonaws + aws-java-sdk-core - org.apache.maven - maven-artifact + com.amazonaws + aws-java-sdk-s3 - org.apache.maven - maven-core + com.google.code.findbugs + annotations - org.apache.maven - maven-plugin-api + com.google.guava + guava - org.apache.maven.plugin-tools - maven-plugin-annotations - provided + com.googlecode.json-simple + json-simple + + + org.apache.maven + maven-archiver org.codehaus.plexus plexus-interpolation - com.google.guava - guava + org.apache.maven + maven-artifact + provided - com.amazonaws - aws-java-sdk-core + org.apache.maven + maven-core + provided - com.amazonaws - aws-java-sdk-s3 + org.apache.maven + maven-model + provided - com.googlecode.json-simple - json-simple + org.apache.maven + maven-plugin-api + provided - org.slf4j - jcl-over-slf4j - runtime + org.apache.maven.plugin-tools + maven-plugin-annotations + provided joda-time joda-time runtime + + org.slf4j + jcl-over-slf4j + runtime + diff --git a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/ArtifactHelper.java b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/ArtifactHelper.java index 655ab9c..be22245 100644 --- a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/ArtifactHelper.java +++ b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/ArtifactHelper.java @@ -14,7 +14,6 @@ import java.util.List; import java.util.Properties; import java.util.Set; - import org.apache.maven.archiver.ManifestConfiguration; import org.apache.maven.archiver.MavenArchiveConfiguration; import org.apache.maven.artifact.Artifact; @@ -35,14 +34,23 @@ import org.codehaus.plexus.interpolation.ValueSource; public class ArtifactHelper { - private static final List ARTIFACT_EXPRESSION_PREFIXES = Collections.singletonList("artifact."); - public static LocalArtifactWrapper getArtifactPaths(BeanConfigurator beanConfigurator, - MavenProject project) throws MojoExecutionException { - ManifestConfiguration manifestConfiguration = parseManifestConfiguration(beanConfigurator, project); + private static final List ARTIFACT_EXPRESSION_PREFIXES = + Collections.singletonList("artifact."); + + public static LocalArtifactWrapper getArtifactPaths( + BeanConfigurator beanConfigurator, + MavenProject project + ) throws MojoExecutionException { + ManifestConfiguration manifestConfiguration = parseManifestConfiguration( + beanConfigurator, + project + ); if (!manifestConfiguration.isAddClasspath()) { - throw new MojoExecutionException("maven-jar-plugin is not configured to add classpath"); + throw new MojoExecutionException( + "maven-jar-plugin is not configured to add classpath" + ); } Set artifacts = new HashSet<>(); @@ -67,13 +75,20 @@ public static LocalArtifactWrapper getArtifactPaths(BeanConfigurator beanConfigu return new LocalArtifactWrapper(prefix, artifacts); } - private static ManifestConfiguration parseManifestConfiguration(BeanConfigurator beanConfigurator, - MavenProject project) throws MojoExecutionException { + private static ManifestConfiguration parseManifestConfiguration( + BeanConfigurator beanConfigurator, + MavenProject project + ) throws MojoExecutionException { MavenArchiveConfiguration archiveConfiguration = new MavenArchiveConfiguration(); BeanConfigurationRequest beanConfiguration = new DefaultBeanConfigurationRequest() - .setBean(archiveConfiguration) - .setConfiguration(project.getModel(), "org.apache.maven.plugins", "maven-jar-plugin", "default-jar"); + .setBean(archiveConfiguration) + .setConfiguration( + project.getModel(), + "org.apache.maven.plugins", + "maven-jar-plugin", + "default-jar" + ); beanConfiguration.setConfiguration(beanConfiguration.getConfiguration(), "archive"); @@ -86,7 +101,8 @@ private static ManifestConfiguration parseManifestConfiguration(BeanConfigurator return archiveConfiguration.getManifest(); } - private static List classpathElements(MavenProject project) throws MojoExecutionException { + private static List classpathElements(MavenProject project) + throws MojoExecutionException { try { return project.getRuntimeClasspathElements(); } catch (DependencyResolutionRequiredException e) { @@ -94,7 +110,8 @@ private static List classpathElements(MavenProject project) throws MojoE } } - private static Path computePath(Artifact artifact, ManifestConfiguration config) throws MojoExecutionException { + private static Path computePath(Artifact artifact, ManifestConfiguration config) + throws MojoExecutionException { String layoutType = config.getClasspathLayoutType(); String layout = config.getCustomClasspathLayout(); @@ -102,8 +119,16 @@ private static Path computePath(Artifact artifact, ManifestConfiguration config) List valueSources = new ArrayList<>(); - valueSources.add(new PrefixedObjectValueSource(ARTIFACT_EXPRESSION_PREFIXES, artifact, true)); - valueSources.add(new PrefixedObjectValueSource(ARTIFACT_EXPRESSION_PREFIXES, artifact.getArtifactHandler(), true)); + valueSources.add( + new PrefixedObjectValueSource(ARTIFACT_EXPRESSION_PREFIXES, artifact, true) + ); + valueSources.add( + new PrefixedObjectValueSource( + ARTIFACT_EXPRESSION_PREFIXES, + artifact.getArtifactHandler(), + true + ) + ); Properties extraExpressions = new Properties(); if (!artifact.isSnapshot()) { @@ -118,13 +143,21 @@ private static Path computePath(Artifact artifact, ManifestConfiguration config) extraExpressions.setProperty("dashClassifier", ""); extraExpressions.setProperty("dashClassifier?", ""); } - valueSources.add(new PrefixedPropertiesValueSource(ARTIFACT_EXPRESSION_PREFIXES, extraExpressions, true)); + valueSources.add( + new PrefixedPropertiesValueSource( + ARTIFACT_EXPRESSION_PREFIXES, + extraExpressions, + true + ) + ); for (ValueSource vs : valueSources) { interpolator.addValueSource(vs); } - RecursionInterceptor recursionInterceptor = new PrefixAwareRecursionInterceptor(ARTIFACT_EXPRESSION_PREFIXES); + RecursionInterceptor recursionInterceptor = new PrefixAwareRecursionInterceptor( + ARTIFACT_EXPRESSION_PREFIXES + ); try { boolean useUniqueVersionsLayout = config.isUseUniqueVersions(); @@ -132,16 +165,20 @@ private static Path computePath(Artifact artifact, ManifestConfiguration config) final String resolvedLayout; switch (layoutType) { case ManifestConfiguration.CLASSPATH_LAYOUT_TYPE_SIMPLE: - resolvedLayout = useUniqueVersionsLayout ? SIMPLE_LAYOUT : SIMPLE_LAYOUT_NONUNIQUE; + resolvedLayout = + useUniqueVersionsLayout ? SIMPLE_LAYOUT : SIMPLE_LAYOUT_NONUNIQUE; break; case ManifestConfiguration.CLASSPATH_LAYOUT_TYPE_REPOSITORY: - resolvedLayout = useUniqueVersionsLayout ? REPOSITORY_LAYOUT : REPOSITORY_LAYOUT_NONUNIQUE; + resolvedLayout = + useUniqueVersionsLayout ? REPOSITORY_LAYOUT : REPOSITORY_LAYOUT_NONUNIQUE; break; case ManifestConfiguration.CLASSPATH_LAYOUT_TYPE_CUSTOM: resolvedLayout = layout; break; default: - throw new MojoExecutionException("Unknown classpath layout type: " + layoutType); + throw new MojoExecutionException( + "Unknown classpath layout type: " + layoutType + ); } return Paths.get(interpolator.interpolate(resolvedLayout, recursionInterceptor)); diff --git a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/BaseFileUploader.java b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/BaseFileUploader.java index 4a334a8..5867da4 100644 --- a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/BaseFileUploader.java +++ b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/BaseFileUploader.java @@ -6,12 +6,12 @@ import java.util.Collections; import java.util.LinkedHashSet; import java.util.Set; - import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugin.logging.Log; public abstract class BaseFileUploader implements FileUploader, FromManifestUploader { + private Set s3Artifacts; private Path prefix; private Path outputFile; @@ -24,7 +24,8 @@ public final void init(UploadConfiguration config, Log log) { doInit(config, log); } - protected abstract void doUpload(String bucket, String key, Path path) throws MojoFailureException, MojoExecutionException; + protected abstract void doUpload(String bucket, String key, Path path) + throws MojoFailureException, MojoExecutionException; protected void doInit(UploadConfiguration config, Log log) { // empty by default @@ -35,19 +36,22 @@ protected void doDestroy() throws MojoFailureException { } @Override - public void upload(UploadConfiguration config, LocalArtifact artifact) throws MojoExecutionException, MojoFailureException { + public void upload(UploadConfiguration config, LocalArtifact artifact) + throws MojoExecutionException, MojoFailureException { upload(config, artifact.getTargetPath(), artifact.getLocalPath()); } @Override - public void uploadFromManifest(UploadConfiguration config, PreparedArtifact artifact) throws MojoExecutionException, MojoFailureException { + public void uploadFromManifest(UploadConfiguration config, PreparedArtifact artifact) + throws MojoExecutionException, MojoFailureException { Path targetPath = Paths.get(artifact.getTargetPath()); Path localPath = Paths.get(artifact.getLocalPath()); upload(config, targetPath, localPath); } - private void upload(UploadConfiguration config, Path targetPath, Path localPath) throws MojoExecutionException, MojoFailureException { + private void upload(UploadConfiguration config, Path targetPath, Path localPath) + throws MojoExecutionException, MojoFailureException { String file = targetPath.toString(); boolean isUnresolvedSnapshot = file.toUpperCase().endsWith("-SNAPSHOT.JAR"); @@ -57,7 +61,11 @@ private void upload(UploadConfiguration config, Path targetPath, Path localPath) String start = file.substring(0, file.length() - ".JAR".length()); String end = file.substring(file.length() - ".JAR".length()); String md5 = FileHelper.md5(localPath); - s3Key = Paths.get(config.getS3ArtifactRoot()).resolve(start + "-" + md5 + end).toString(); + s3Key = + Paths + .get(config.getS3ArtifactRoot()) + .resolve(start + "-" + md5 + end) + .toString(); } else { throw new MojoExecutionException("Encountered unresolved snapshot: " + file); } @@ -68,13 +76,24 @@ private void upload(UploadConfiguration config, Path targetPath, Path localPath) doUpload(config.getS3Bucket(), s3Key, localPath); String targetPathAsString = prefix.resolve(targetPath).toString(); - s3Artifacts.add(new S3Artifact(config.getS3Bucket(), s3Key, targetPathAsString, FileHelper.md5(localPath), FileHelper.size(localPath))); + s3Artifacts.add( + new S3Artifact( + config.getS3Bucket(), + s3Key, + targetPathAsString, + FileHelper.md5(localPath), + FileHelper.size(localPath) + ) + ); } @Override public final void destroy() throws MojoFailureException { try { - JsonHelper.writeArtifactsToJson(outputFile.toFile(), new S3ArtifactWrapper(prefix.toString(), s3Artifacts)); + JsonHelper.writeArtifactsToJson( + outputFile.toFile(), + new S3ArtifactWrapper(prefix.toString(), s3Artifacts) + ); } catch (IOException e) { throw new MojoFailureException("Error writing dependencies json to file", e); } diff --git a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/ClasspathConfiguration.java b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/ClasspathConfiguration.java index a43df09..5a72657 100644 --- a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/ClasspathConfiguration.java +++ b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/ClasspathConfiguration.java @@ -4,6 +4,7 @@ import java.util.Set; public class ClasspathConfiguration { + private final Path prefix; private final Set entries; diff --git a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/CopyJarsMojo.java b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/CopyJarsMojo.java index 54ef513..ec22ce3 100644 --- a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/CopyJarsMojo.java +++ b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/CopyJarsMojo.java @@ -5,7 +5,6 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; - import org.apache.maven.configuration.BeanConfigurator; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; @@ -17,7 +16,12 @@ import org.apache.maven.plugins.annotations.ResolutionScope; import org.apache.maven.project.MavenProject; -@Mojo(name = "copy", defaultPhase = LifecyclePhase.PACKAGE, threadSafe = true, requiresDependencyResolution = ResolutionScope.RUNTIME) +@Mojo( + name = "copy", + defaultPhase = LifecyclePhase.PACKAGE, + threadSafe = true, + requiresDependencyResolution = ResolutionScope.RUNTIME +) public class CopyJarsMojo extends AbstractMojo { @Component @@ -39,18 +43,27 @@ public void execute() throws MojoExecutionException, MojoFailureException { return; } - LocalArtifactWrapper artifactWrapper = ArtifactHelper.getArtifactPaths(beanConfigurator, project); + LocalArtifactWrapper artifactWrapper = ArtifactHelper.getArtifactPaths( + beanConfigurator, + project + ); Path prefix = artifactWrapper.getPrefix(); for (LocalArtifact artifact : artifactWrapper.getArtifacts()) { Path localPath = artifact.getLocalPath(); - Path targetPath = Paths.get(outputDirectory).resolve(prefix).resolve(artifact.getTargetPath()); + Path targetPath = Paths + .get(outputDirectory) + .resolve(prefix) + .resolve(artifact.getTargetPath()); FileHelper.ensureDirectoryExists(targetPath.getParent()); try { Files.copy(localPath, targetPath, StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { - throw new MojoFailureException(String.format("Error moving file from %s to %s", localPath, targetPath), e); + throw new MojoFailureException( + String.format("Error moving file from %s to %s", localPath, targetPath), + e + ); } } } diff --git a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/DefaultFileDownloader.java b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/DefaultFileDownloader.java index b989257..ebea796 100644 --- a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/DefaultFileDownloader.java +++ b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/DefaultFileDownloader.java @@ -1,21 +1,20 @@ package com.hubspot.maven.plugins.slimfast; +import com.amazonaws.SdkBaseException; +import com.amazonaws.services.s3.AmazonS3; +import com.amazonaws.services.s3.model.S3Object; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; - import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugin.logging.Log; -import com.amazonaws.SdkBaseException; -import com.amazonaws.services.s3.AmazonS3; -import com.amazonaws.services.s3.model.S3Object; - public class DefaultFileDownloader implements FileDownloader { + private AmazonS3 s3Service; private Path cacheDirectory; private Log log; @@ -28,14 +27,17 @@ public void init(DownloadConfiguration config, Log log) { } @Override - public void download(DownloadConfiguration config, S3Artifact artifact) throws MojoExecutionException, MojoFailureException { + public void download(DownloadConfiguration config, S3Artifact artifact) + throws MojoExecutionException, MojoFailureException { Path targetFile = config.getOutputDirectory().resolve(artifact.getTargetPath()); if (artifactIsCached(targetFile, artifact)) { log.info("Target file exists " + targetFile); return; } - Path cacheFile = cacheDirectory.resolve(config.getPrefix().relativize(Paths.get(artifact.getTargetPath()))); + Path cacheFile = cacheDirectory.resolve( + config.getPrefix().relativize(Paths.get(artifact.getTargetPath())) + ); if (artifactIsCached(cacheFile, artifact)) { log.info("Target file is cached " + cacheFile); } else { @@ -48,7 +50,10 @@ public void download(DownloadConfiguration config, S3Artifact artifact) throws M try { Files.copy(cacheFile, targetFile); } catch (IOException e) { - throw new MojoFailureException(String.format("Error copying file from %s to %s", cacheFile, targetFile), e); + throw new MojoFailureException( + String.format("Error copying file from %s to %s", cacheFile, targetFile), + e + ); } verifyChecksums(targetFile, artifact); @@ -57,11 +62,13 @@ public void download(DownloadConfiguration config, S3Artifact artifact) throws M @Override public void destroy() throws MojoExecutionException, MojoFailureException {} - private boolean artifactIsCached(Path path, S3Artifact artifact) throws MojoExecutionException { + private boolean artifactIsCached(Path path, S3Artifact artifact) + throws MojoExecutionException { return Files.exists(path) && checksumsMatch(path, artifact); } - private void doDownload(String bucket, String key, Path path) throws MojoFailureException, MojoExecutionException { + private void doDownload(String bucket, String key, Path path) + throws MojoFailureException, MojoExecutionException { Path tempPath = createTempFile(path); try { @@ -79,33 +86,59 @@ private void doDownload(String bucket, String key, Path path) throws MojoFailure FileHelper.ensureDirectoryExists(path.getParent()); Files.move(tempPath, path, StandardCopyOption.ATOMIC_MOVE); } catch (IOException e) { - throw new MojoFailureException(String.format("Error moving file from %s to %s", tempPath, path), e); + throw new MojoFailureException( + String.format("Error moving file from %s to %s", tempPath, path), + e + ); } } private Path createTempFile(Path path) throws MojoFailureException { try { - return Files.createTempFile(cacheDirectory, String.valueOf(path.getFileName()), null); + return Files.createTempFile( + cacheDirectory, + String.valueOf(path.getFileName()), + null + ); } catch (IOException e) { throw new MojoFailureException("Error creating temp file in " + cacheDirectory, e); } } - private static void verifyChecksums(Path path, S3Artifact artifact) throws MojoExecutionException { + private static void verifyChecksums(Path path, S3Artifact artifact) + throws MojoExecutionException { long actualSize = FileHelper.size(path); long expectedSize = artifact.getSize(); if (actualSize != expectedSize) { - throw new MojoExecutionException(String.format("File %s has unexpected size, expected=%s, actual=%s", path, expectedSize, actualSize)); + throw new MojoExecutionException( + String.format( + "File %s has unexpected size, expected=%s, actual=%s", + path, + expectedSize, + actualSize + ) + ); } String actualMd5 = FileHelper.md5(path); String expectedMd5 = artifact.getMd5(); if (!actualMd5.equals(expectedMd5)) { - throw new MojoExecutionException(String.format("File %s has unexpected checksum, expected=%s, actual=%s", path, expectedMd5, actualMd5)); + throw new MojoExecutionException( + String.format( + "File %s has unexpected checksum, expected=%s, actual=%s", + path, + expectedMd5, + actualMd5 + ) + ); } } - private static boolean checksumsMatch(Path path, S3Artifact artifact) throws MojoExecutionException { - return FileHelper.size(path) == artifact.getSize() && FileHelper.md5(path).equals(artifact.getMd5()); + private static boolean checksumsMatch(Path path, S3Artifact artifact) + throws MojoExecutionException { + return ( + FileHelper.size(path) == artifact.getSize() && + FileHelper.md5(path).equals(artifact.getMd5()) + ); } } diff --git a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/DefaultFileUploader.java b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/DefaultFileUploader.java index 3dedace..13050c6 100644 --- a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/DefaultFileUploader.java +++ b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/DefaultFileUploader.java @@ -1,17 +1,16 @@ package com.hubspot.maven.plugins.slimfast; -import java.nio.file.Path; - -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; -import org.apache.maven.plugin.logging.Log; - import com.amazonaws.AmazonServiceException; import com.amazonaws.SdkBaseException; import com.amazonaws.SdkClientException; import com.amazonaws.services.s3.AmazonS3; +import java.nio.file.Path; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugin.logging.Log; public class DefaultFileUploader extends BaseFileUploader { + private AmazonS3 s3Service; private Log log; @@ -22,7 +21,8 @@ protected void doInit(UploadConfiguration config, Log log) { } @Override - protected void doUpload(String bucket, String key, Path path) throws MojoFailureException, MojoExecutionException { + protected void doUpload(String bucket, String key, Path path) + throws MojoFailureException, MojoExecutionException { if (keyExists(bucket, key)) { log.info("Key already exists " + key); return; @@ -44,7 +44,10 @@ private boolean keyExists(String bucket, String key) throws MojoFailureException s3Service.getObjectMetadata(bucket, key); return true; } catch (SdkBaseException e) { - if (e instanceof AmazonServiceException && ((AmazonServiceException) e).getStatusCode() == 404) { + if ( + e instanceof AmazonServiceException && + ((AmazonServiceException) e).getStatusCode() == 404 + ) { return false; } else { throw new MojoFailureException("Error getting object metadata for key " + key, e); diff --git a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/DownloadConfiguration.java b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/DownloadConfiguration.java index 559d9a4..38a8ee7 100644 --- a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/DownloadConfiguration.java +++ b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/DownloadConfiguration.java @@ -1,21 +1,23 @@ package com.hubspot.maven.plugins.slimfast; -import java.nio.file.Path; - import com.amazonaws.services.s3.AmazonS3; +import java.nio.file.Path; public class DownloadConfiguration { + private final Path prefix; private final Path cacheDirectory; private final Path outputDirectory; private final String s3AccessKey; private final String s3SecretKey; - public DownloadConfiguration(Path prefix, - Path cacheDirectory, - Path outputDirectory, - String s3AccessKey, - String s3SecretKey) { + public DownloadConfiguration( + Path prefix, + Path cacheDirectory, + Path outputDirectory, + String s3AccessKey, + String s3SecretKey + ) { this.prefix = prefix; this.cacheDirectory = cacheDirectory; this.outputDirectory = outputDirectory; diff --git a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/DownloadJarsMojo.java b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/DownloadJarsMojo.java index fbffe45..3c3aa6e 100644 --- a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/DownloadJarsMojo.java +++ b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/DownloadJarsMojo.java @@ -1,5 +1,7 @@ package com.hubspot.maven.plugins.slimfast; +import com.google.common.base.Throwables; +import com.google.common.util.concurrent.ThreadFactoryBuilder; import java.io.File; import java.io.IOException; import java.nio.file.Paths; @@ -12,32 +14,43 @@ import java.util.concurrent.Future; import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; - import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; -import com.google.common.base.Throwables; -import com.google.common.util.concurrent.ThreadFactoryBuilder; - @Mojo(name = "download", requiresProject = false, threadSafe = true) public class DownloadJarsMojo extends AbstractMojo { - @Parameter(property = "slimfast.fileDownloader", alias = "fileDownloader", defaultValue = "com.hubspot.maven.plugins.slimfast.DefaultFileDownloader") + @Parameter( + property = "slimfast.fileDownloader", + alias = "fileDownloader", + defaultValue = "com.hubspot.maven.plugins.slimfast.DefaultFileDownloader" + ) private String fileDownloaderType; - @Parameter(property = "slimfast.s3.accessKey", defaultValue = "${s3.access.key}", required = true) + @Parameter( + property = "slimfast.s3.accessKey", + defaultValue = "${s3.access.key}", + required = true + ) private String s3AccessKey; - @Parameter(property = "slimfast.s3.secretKey", defaultValue = "${s3.secret.key}", required = true) + @Parameter( + property = "slimfast.s3.secretKey", + defaultValue = "${s3.secret.key}", + required = true + ) private String s3SecretKey; @Parameter(property = "slimfast.s3.downloadThreads", defaultValue = "10") private int s3DownloadThreads; - @Parameter(property = "slimfast.cacheDirectory", defaultValue = "${settings.localRepository}") + @Parameter( + property = "slimfast.cacheDirectory", + defaultValue = "${settings.localRepository}" + ) private String cacheDirectory; @Parameter(property = "slimfast.outputDirectory", defaultValue = "${basedir}") @@ -53,21 +66,30 @@ public void execute() throws MojoExecutionException, MojoFailureException { final DownloadConfiguration configuration = buildConfiguration(wrapper.getPrefix()); FileHelper.ensureDirectoryExists(configuration.getCacheDirectory()); - ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("slimfast-download").setDaemon(true).build(); - ExecutorService executor = Executors.newFixedThreadPool(s3DownloadThreads, threadFactory); + ThreadFactory threadFactory = new ThreadFactoryBuilder() + .setNameFormat("slimfast-download") + .setDaemon(true) + .build(); + ExecutorService executor = Executors.newFixedThreadPool( + s3DownloadThreads, + threadFactory + ); final FileDownloader downloader = instantiateFileDownloader(); downloader.init(configuration, getLog()); List> futures = new ArrayList<>(); for (final S3Artifact artifact : wrapper.getArtifacts()) { - futures.add(executor.submit(new Callable() { - - @Override - public Object call() throws Exception { - downloader.download(configuration, artifact); - return null; - } - })); + futures.add( + executor.submit( + new Callable() { + @Override + public Object call() throws Exception { + downloader.download(configuration, artifact); + return null; + } + } + ) + ); } executor.shutdown(); @@ -83,7 +105,10 @@ private S3ArtifactWrapper readArtifactInfo() throws MojoFailureException { } } - private void waitForDownloadsToFinish(ExecutorService executor, List> futures) throws MojoExecutionException, MojoFailureException { + private void waitForDownloadsToFinish( + ExecutorService executor, + List> futures + ) throws MojoExecutionException, MojoFailureException { try { if (!executor.awaitTermination(5, TimeUnit.MINUTES)) { getLog().error("Took more than 5 minutes to download files, quitting"); @@ -92,7 +117,6 @@ private void waitForDownloadsToFinish(ExecutorService executor, List> for (Future future : futures) { future.get(); - } } catch (InterruptedException e) { throw new MojoExecutionException("Interrupted", e); @@ -105,11 +129,11 @@ private void waitForDownloadsToFinish(ExecutorService executor, List> private DownloadConfiguration buildConfiguration(String prefix) { return new DownloadConfiguration( - Paths.get(prefix), - Paths.get(cacheDirectory), - Paths.get(outputDirectory), - s3AccessKey, - s3SecretKey + Paths.get(prefix), + Paths.get(cacheDirectory), + Paths.get(outputDirectory), + s3AccessKey, + s3SecretKey ); } @@ -117,7 +141,10 @@ private FileDownloader instantiateFileDownloader() throws MojoExecutionException try { return (FileDownloader) Class.forName(fileDownloaderType).newInstance(); } catch (ClassNotFoundException e) { - throw new MojoExecutionException("Unable to find file downloader implementation", e); + throw new MojoExecutionException( + "Unable to find file downloader implementation", + e + ); } catch (InstantiationException | IllegalAccessException e) { throw new MojoExecutionException("Unable to instantiate file downloader", e); } catch (ClassCastException e) { diff --git a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/FileDownloader.java b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/FileDownloader.java index 69716f7..5a3ae78 100644 --- a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/FileDownloader.java +++ b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/FileDownloader.java @@ -5,7 +5,9 @@ import org.apache.maven.plugin.logging.Log; public interface FileDownloader { - void init(DownloadConfiguration config, Log log) throws MojoExecutionException, MojoFailureException; - void download(DownloadConfiguration config, S3Artifact artifact) throws MojoExecutionException, MojoFailureException; + void init(DownloadConfiguration config, Log log) + throws MojoExecutionException, MojoFailureException; + void download(DownloadConfiguration config, S3Artifact artifact) + throws MojoExecutionException, MojoFailureException; void destroy() throws MojoExecutionException, MojoFailureException; } diff --git a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/FileHelper.java b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/FileHelper.java index 0c08ac5..a24e1d9 100644 --- a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/FileHelper.java +++ b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/FileHelper.java @@ -1,23 +1,24 @@ package com.hubspot.maven.plugins.slimfast; +import com.google.common.hash.Hashing; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; - import javax.annotation.Nullable; - import org.apache.maven.plugin.MojoExecutionException; -import com.google.common.hash.Hashing; - public class FileHelper { - public static void ensureDirectoryExists(@Nullable Path path) throws MojoExecutionException { + public static void ensureDirectoryExists(@Nullable Path path) + throws MojoExecutionException { if (path != null && !Files.exists(path)) { try { Files.createDirectories(path); } catch (IOException e) { - throw new MojoExecutionException("Error creating parent directories for path " + path, e); + throw new MojoExecutionException( + "Error creating parent directories for path " + path, + e + ); } } } diff --git a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/FileUploader.java b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/FileUploader.java index 512870c..c7a57f0 100644 --- a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/FileUploader.java +++ b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/FileUploader.java @@ -5,7 +5,9 @@ import org.apache.maven.plugin.logging.Log; public interface FileUploader { - void init(UploadConfiguration config, Log log) throws MojoExecutionException, MojoFailureException; - void upload(UploadConfiguration config, LocalArtifact artifact) throws MojoExecutionException, MojoFailureException; + void init(UploadConfiguration config, Log log) + throws MojoExecutionException, MojoFailureException; + void upload(UploadConfiguration config, LocalArtifact artifact) + throws MojoExecutionException, MojoFailureException; void destroy() throws MojoExecutionException, MojoFailureException; } diff --git a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/FromManifestUploader.java b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/FromManifestUploader.java index 83d1e71..22290e1 100644 --- a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/FromManifestUploader.java +++ b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/FromManifestUploader.java @@ -5,7 +5,9 @@ import org.apache.maven.plugin.logging.Log; public interface FromManifestUploader { - void init(UploadConfiguration config, Log log) throws MojoExecutionException, MojoFailureException; - void uploadFromManifest(UploadConfiguration config, PreparedArtifact artifact) throws MojoExecutionException, MojoFailureException; - void destroy() throws MojoExecutionException, MojoFailureException; + void init(UploadConfiguration config, Log log) + throws MojoExecutionException, MojoFailureException; + void uploadFromManifest(UploadConfiguration config, PreparedArtifact artifact) + throws MojoExecutionException, MojoFailureException; + void destroy() throws MojoExecutionException, MojoFailureException; } diff --git a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/JsonHelper.java b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/JsonHelper.java index fa7c71f..2d2c7ac 100644 --- a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/JsonHelper.java +++ b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/JsonHelper.java @@ -14,7 +14,6 @@ import java.util.LinkedHashSet; import java.util.Set; import java.util.function.Function; - import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; @@ -22,7 +21,8 @@ public class JsonHelper { - public static void writeArtifactsToJson(File outputFile, S3ArtifactWrapper wrapper) throws IOException { + public static void writeArtifactsToJson(File outputFile, S3ArtifactWrapper wrapper) + throws IOException { JSONObject json = new JSONObject(); JSONArray artifacts = new JSONArray(); @@ -39,7 +39,10 @@ public static void writeArtifactsToJson(File outputFile, S3ArtifactWrapper wrapp } } - public static void writeArtifactsToJson(File outputFile, PreparedArtifactWrapper wrapper) throws IOException { + public static void writeArtifactsToJson( + File outputFile, + PreparedArtifactWrapper wrapper + ) throws IOException { JSONObject json = new JSONObject(); JSONArray artifacts = new JSONArray(); @@ -56,7 +59,8 @@ public static void writeArtifactsToJson(File outputFile, PreparedArtifactWrapper } } - public static S3ArtifactWrapper readArtifactsFromJson(File inputFile) throws IOException { + public static S3ArtifactWrapper readArtifactsFromJson(File inputFile) + throws IOException { JSONParser parser = new JSONParser(); try (Reader reader = newReader(inputFile)) { @@ -76,7 +80,8 @@ public static S3ArtifactWrapper readArtifactsFromJson(File inputFile) throws IOE } } - public static PreparedArtifactWrapper readPreparedArtifactsFromJson(File inputFile) throws IOException { + public static PreparedArtifactWrapper readPreparedArtifactsFromJson(File inputFile) + throws IOException { JSONParser parser = new JSONParser(); try (Reader reader = newReader(inputFile)) { @@ -98,12 +103,16 @@ public static PreparedArtifactWrapper readPreparedArtifactsFromJson(File inputFi private static Writer newWriter(File file) throws IOException { FileOutputStream outputStream = new FileOutputStream(file); - return new BufferedWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8)); + return new BufferedWriter( + new OutputStreamWriter(outputStream, StandardCharsets.UTF_8) + ); } private static Reader newReader(File file) throws IOException { FileInputStream outputStream = new FileInputStream(file); - return new BufferedReader(new InputStreamReader(outputStream, StandardCharsets.UTF_8)); + return new BufferedReader( + new InputStreamReader(outputStream, StandardCharsets.UTF_8) + ); } private static JSONObject toJsonObject(S3Artifact artifact) { @@ -141,6 +150,4 @@ private static PreparedArtifact preparedArtifactFromJsonObject(JSONObject jsonOb return new PreparedArtifact(localPath, targetPath); } - - } diff --git a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/LocalArtifact.java b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/LocalArtifact.java index 276330b..740c812 100644 --- a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/LocalArtifact.java +++ b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/LocalArtifact.java @@ -3,6 +3,7 @@ import java.nio.file.Path; public class LocalArtifact { + private final Path localPath; private final Path targetPath; diff --git a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/LocalArtifactWrapper.java b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/LocalArtifactWrapper.java index 354502b..4c7bc37 100644 --- a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/LocalArtifactWrapper.java +++ b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/LocalArtifactWrapper.java @@ -4,6 +4,7 @@ import java.util.Set; public class LocalArtifactWrapper { + private final Path prefix; private final Set artifacts; diff --git a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/PreparedArtifact.java b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/PreparedArtifact.java index fac99dd..a150b78 100644 --- a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/PreparedArtifact.java +++ b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/PreparedArtifact.java @@ -1,19 +1,20 @@ package com.hubspot.maven.plugins.slimfast; public class PreparedArtifact { - private final String localPath; - private final String targetPath; - public PreparedArtifact(String localPath, String targetPath) { - this.localPath = localPath; - this.targetPath = targetPath; - } + private final String localPath; + private final String targetPath; - public String getLocalPath() { - return localPath; - } + public PreparedArtifact(String localPath, String targetPath) { + this.localPath = localPath; + this.targetPath = targetPath; + } - public String getTargetPath() { - return targetPath; - } + public String getLocalPath() { + return localPath; + } + + public String getTargetPath() { + return targetPath; + } } diff --git a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/PreparedArtifactWrapper.java b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/PreparedArtifactWrapper.java index cd600d6..362f688 100644 --- a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/PreparedArtifactWrapper.java +++ b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/PreparedArtifactWrapper.java @@ -3,19 +3,20 @@ import java.util.Set; public class PreparedArtifactWrapper { - private final String prefix; - private final Set artifacts; - public PreparedArtifactWrapper(String prefix, Set artifacts) { - this.prefix = prefix; - this.artifacts = artifacts; - } + private final String prefix; + private final Set artifacts; - public String getPrefix() { - return prefix; - } + public PreparedArtifactWrapper(String prefix, Set artifacts) { + this.prefix = prefix; + this.artifacts = artifacts; + } - public Set getArtifacts() { - return artifacts; - } + public String getPrefix() { + return prefix; + } + + public Set getArtifacts() { + return artifacts; + } } diff --git a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/S3Artifact.java b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/S3Artifact.java index da6a25b..1eed302 100644 --- a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/S3Artifact.java +++ b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/S3Artifact.java @@ -3,6 +3,7 @@ import java.util.Objects; public class S3Artifact { + private final String bucket; private final String key; private final String targetPath; @@ -48,11 +49,13 @@ public boolean equals(Object o) { } S3Artifact artifact = (S3Artifact) o; - return Objects.equals(size, artifact.size) && - Objects.equals(bucket, artifact.bucket) && - Objects.equals(key, artifact.key) && - Objects.equals(targetPath, artifact.targetPath) && - Objects.equals(md5, artifact.md5); + return ( + Objects.equals(size, artifact.size) && + Objects.equals(bucket, artifact.bucket) && + Objects.equals(key, artifact.key) && + Objects.equals(targetPath, artifact.targetPath) && + Objects.equals(md5, artifact.md5) + ); } @Override diff --git a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/S3ArtifactWrapper.java b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/S3ArtifactWrapper.java index a08f20b..28e8c7a 100644 --- a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/S3ArtifactWrapper.java +++ b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/S3ArtifactWrapper.java @@ -3,6 +3,7 @@ import java.util.Set; public class S3ArtifactWrapper { + private final String prefix; private final Set artifacts; diff --git a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/S3Factory.java b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/S3Factory.java index 4ba5ac8..2115382 100644 --- a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/S3Factory.java +++ b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/S3Factory.java @@ -15,14 +15,15 @@ private S3Factory() { public static AmazonS3 create(String accessKey, String secretKey) { AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey); - return AmazonS3ClientBuilder.standard() - .withCredentials(new AWSStaticCredentialsProvider(credentials)) - .withClientConfiguration( - new ClientConfiguration() - .withConnectionTimeout(2_000) - .withRequestTimeout(5_000) - .withMaxErrorRetry(5) - ) - .build(); + return AmazonS3ClientBuilder + .standard() + .withCredentials(new AWSStaticCredentialsProvider(credentials)) + .withClientConfiguration( + new ClientConfiguration() + .withConnectionTimeout(2_000) + .withRequestTimeout(5_000) + .withMaxErrorRetry(5) + ) + .build(); } } diff --git a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/UploadConfiguration.java b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/UploadConfiguration.java index 875e740..6d56990 100644 --- a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/UploadConfiguration.java +++ b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/UploadConfiguration.java @@ -3,6 +3,7 @@ import java.nio.file.Path; public class UploadConfiguration { + private final Path prefix; private final String s3Bucket; private final String s3ArtifactRoot; @@ -11,13 +12,15 @@ public class UploadConfiguration { private final Path outputFile; private final boolean allowUnresolvedSnapshots; - public UploadConfiguration(Path prefix, - String s3Bucket, - String s3ArtifactRoot, - String s3AccessKey, - String s3SecretKey, - Path outputFile, - boolean allowUnresolvedSnapshots) { + public UploadConfiguration( + Path prefix, + String s3Bucket, + String s3ArtifactRoot, + String s3AccessKey, + String s3SecretKey, + Path outputFile, + boolean allowUnresolvedSnapshots + ) { this.prefix = prefix; this.s3Bucket = s3Bucket; this.s3ArtifactRoot = s3ArtifactRoot; diff --git a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/UploadFromManifestMojo.java b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/UploadFromManifestMojo.java index 46afbd4..0e2dfa7 100644 --- a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/UploadFromManifestMojo.java +++ b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/UploadFromManifestMojo.java @@ -2,13 +2,6 @@ import com.google.common.base.Throwables; import com.google.common.util.concurrent.ThreadFactoryBuilder; -import org.apache.maven.plugin.AbstractMojo; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; -import org.apache.maven.plugins.annotations.Mojo; -import org.apache.maven.plugins.annotations.Parameter; -import org.apache.maven.plugins.annotations.ResolutionScope; - import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; @@ -20,132 +13,184 @@ import java.util.concurrent.Future; import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.plugins.annotations.ResolutionScope; -@Mojo(name = "upload-from-manifest", threadSafe = true, requiresDependencyResolution = ResolutionScope.RUNTIME) +@Mojo( + name = "upload-from-manifest", + threadSafe = true, + requiresDependencyResolution = ResolutionScope.RUNTIME +) public class UploadFromManifestMojo extends AbstractMojo { - private static final String DEFAULT_UPLOADER = "com.hubspot.maven.plugins.slimfast.DefaultFileUploader"; - private static final String DRY_RUN_UPLOADER = "com.hubspot.maven.plugins.slimfast.DryRunFileUploader"; - - @Parameter(property = "slimfast.fromManifestFileUploader", alias = "fileUploader", defaultValue = DEFAULT_UPLOADER) - private String fileUploaderType; - - @Parameter(property = "slimfast.s3.accessKey", defaultValue = "${s3.access.key}", required = true) - private String s3AccessKey; - - @Parameter(property = "slimfast.s3.secretKey", defaultValue = "${s3.secret.key}", required = true) - private String s3SecretKey; - @Parameter(property = "slimfast.manifestFile", defaultValue = "${project.build.directory}/slimfast-local.json") - private String manifestFile; - - @Parameter(property = "slimfast.dryRun", defaultValue = "false") - private boolean dryRun; - - @Parameter(property = "slimfast.s3.bucket", defaultValue = "${s3.bucket}", required = true) - private String s3Bucket; - - @Parameter(property = "slimfast.s3.artifactPrefix", defaultValue = "${s3.artifact.root}", required = true) - private String s3ArtifactRoot; - - @Parameter(property = "slimfast.s3.uploadThreads", defaultValue = "10") - private int s3UploadThreads; - - @Parameter(property = "slimfast.plugin.skip", defaultValue = "false") - private boolean skip; - - @Parameter(property = "slimfast.outputFile", defaultValue = "${project.build.directory}/slimfast.json") - private String outputFile; - - @Parameter(property = "slimfast.allowUnresolvedSnapshots", defaultValue = "false") - private boolean allowUnresolvedSnapshots; - - - @Override - public void execute() throws MojoExecutionException, MojoFailureException { - if (skip) { - getLog().info("Skipping plugin execution"); - return; - } - - final PreparedArtifactWrapper preparedArtifactWrapper; - try { - preparedArtifactWrapper = JsonHelper.readPreparedArtifactsFromJson(Paths.get(manifestFile).toFile()); - } catch (IOException e) { - throw new MojoExecutionException("Failed to read manifest file", e); - } - - ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("slimfast-upload").setDaemon(true).build(); - ExecutorService executor = Executors.newFixedThreadPool(s3UploadThreads, threadFactory); - Path prefix = Paths.get(preparedArtifactWrapper.getPrefix()); - final UploadConfiguration configuration = buildConfiguration(prefix); - FromManifestUploader fileUploader = instantiateFileUploader(); - fileUploader.init(configuration, getLog()); - - List> futures = new ArrayList<>(); - for (final PreparedArtifact artifact : preparedArtifactWrapper.getArtifacts()) { - futures.add(executor.submit(() -> { - fileUploader.uploadFromManifest(configuration, artifact); - return null; - })); - } + private static final String DEFAULT_UPLOADER = + "com.hubspot.maven.plugins.slimfast.DefaultFileUploader"; + private static final String DRY_RUN_UPLOADER = + "com.hubspot.maven.plugins.slimfast.DryRunFileUploader"; + + @Parameter( + property = "slimfast.fromManifestFileUploader", + alias = "fileUploader", + defaultValue = DEFAULT_UPLOADER + ) + private String fileUploaderType; + + @Parameter( + property = "slimfast.s3.accessKey", + defaultValue = "${s3.access.key}", + required = true + ) + private String s3AccessKey; + + @Parameter( + property = "slimfast.s3.secretKey", + defaultValue = "${s3.secret.key}", + required = true + ) + private String s3SecretKey; + + @Parameter( + property = "slimfast.manifestFile", + defaultValue = "${project.build.directory}/slimfast-local.json" + ) + private String manifestFile; + + @Parameter(property = "slimfast.dryRun", defaultValue = "false") + private boolean dryRun; + + @Parameter( + property = "slimfast.s3.bucket", + defaultValue = "${s3.bucket}", + required = true + ) + private String s3Bucket; + + @Parameter( + property = "slimfast.s3.artifactPrefix", + defaultValue = "${s3.artifact.root}", + required = true + ) + private String s3ArtifactRoot; + + @Parameter(property = "slimfast.s3.uploadThreads", defaultValue = "10") + private int s3UploadThreads; + + @Parameter(property = "slimfast.plugin.skip", defaultValue = "false") + private boolean skip; + + @Parameter( + property = "slimfast.outputFile", + defaultValue = "${project.build.directory}/slimfast.json" + ) + private String outputFile; + + @Parameter(property = "slimfast.allowUnresolvedSnapshots", defaultValue = "false") + private boolean allowUnresolvedSnapshots; + + @Override + public void execute() throws MojoExecutionException, MojoFailureException { + if (skip) { + getLog().info("Skipping plugin execution"); + return; + } - executor.shutdown(); - waitForUploadsToFinish(executor, futures); - fileUploader.destroy(); + final PreparedArtifactWrapper preparedArtifactWrapper; + try { + preparedArtifactWrapper = + JsonHelper.readPreparedArtifactsFromJson(Paths.get(manifestFile).toFile()); + } catch (IOException e) { + throw new MojoExecutionException("Failed to read manifest file", e); } - private UploadConfiguration buildConfiguration(Path prefix) { - return new UploadConfiguration( - prefix, - s3Bucket, - s3ArtifactRoot, - s3AccessKey, - s3SecretKey, - Paths.get(outputFile), - allowUnresolvedSnapshots - ); + ThreadFactory threadFactory = new ThreadFactoryBuilder() + .setNameFormat("slimfast-upload") + .setDaemon(true) + .build(); + ExecutorService executor = Executors.newFixedThreadPool( + s3UploadThreads, + threadFactory + ); + Path prefix = Paths.get(preparedArtifactWrapper.getPrefix()); + final UploadConfiguration configuration = buildConfiguration(prefix); + FromManifestUploader fileUploader = instantiateFileUploader(); + fileUploader.init(configuration, getLog()); + + List> futures = new ArrayList<>(); + for (final PreparedArtifact artifact : preparedArtifactWrapper.getArtifacts()) { + futures.add( + executor.submit(() -> { + fileUploader.uploadFromManifest(configuration, artifact); + return null; + }) + ); } - private void waitForUploadsToFinish(ExecutorService executor, List> futures) throws MojoExecutionException, MojoFailureException { - try { - if (!executor.awaitTermination(5, TimeUnit.MINUTES)) { - getLog().error("Took more than 5 minutes to upload files, quitting"); - throw new MojoExecutionException("Took more than 5 minutes to upload files"); - } - - for (Future future : futures) { - future.get(); - - } - } catch (InterruptedException e) { - throw new MojoExecutionException("Interrupted", e); - } catch (ExecutionException e) { - Throwables.propagateIfInstanceOf(e.getCause(), MojoExecutionException.class); - Throwables.propagateIfInstanceOf(e.getCause(), MojoFailureException.class); - throw new MojoExecutionException("Unexpected exception", e.getCause()); - } + executor.shutdown(); + waitForUploadsToFinish(executor, futures); + fileUploader.destroy(); + } + + private UploadConfiguration buildConfiguration(Path prefix) { + return new UploadConfiguration( + prefix, + s3Bucket, + s3ArtifactRoot, + s3AccessKey, + s3SecretKey, + Paths.get(outputFile), + allowUnresolvedSnapshots + ); + } + + private void waitForUploadsToFinish(ExecutorService executor, List> futures) + throws MojoExecutionException, MojoFailureException { + try { + if (!executor.awaitTermination(5, TimeUnit.MINUTES)) { + getLog().error("Took more than 5 minutes to upload files, quitting"); + throw new MojoExecutionException("Took more than 5 minutes to upload files"); + } + + for (Future future : futures) { + future.get(); + } + } catch (InterruptedException e) { + throw new MojoExecutionException("Interrupted", e); + } catch (ExecutionException e) { + Throwables.propagateIfInstanceOf(e.getCause(), MojoExecutionException.class); + Throwables.propagateIfInstanceOf(e.getCause(), MojoFailureException.class); + throw new MojoExecutionException("Unexpected exception", e.getCause()); + } + } + + private FromManifestUploader instantiateFileUploader() throws MojoExecutionException { + final String resolvedFileUploaderType; + if (dryRun) { + if (DEFAULT_UPLOADER.equals(fileUploaderType)) { + resolvedFileUploaderType = DRY_RUN_UPLOADER; + } else { + throw new MojoExecutionException( + "May not specify custom fileUploader when using the dryRun flag" + ); + } + } else { + resolvedFileUploaderType = fileUploaderType; } - private FromManifestUploader instantiateFileUploader() throws MojoExecutionException { - final String resolvedFileUploaderType; - if (dryRun) { - if (DEFAULT_UPLOADER.equals(fileUploaderType)) { - resolvedFileUploaderType = DRY_RUN_UPLOADER; - } else { - throw new MojoExecutionException("May not specify custom fileUploader when using the dryRun flag"); - } - } else { - resolvedFileUploaderType = fileUploaderType; - } - - try { - return (FromManifestUploader) Class.forName(resolvedFileUploaderType).newInstance(); - } catch (ClassNotFoundException e) { - throw new MojoExecutionException("Unable to find file uploader implementation", e); - } catch (InstantiationException | IllegalAccessException e) { - throw new MojoExecutionException("Unable to instantiate file uploader", e); - } catch (ClassCastException e) { - throw new MojoExecutionException("Must implement FromManifestUploader interface", e); - } + try { + return (FromManifestUploader) Class.forName(resolvedFileUploaderType).newInstance(); + } catch (ClassNotFoundException e) { + throw new MojoExecutionException("Unable to find file uploader implementation", e); + } catch (InstantiationException | IllegalAccessException e) { + throw new MojoExecutionException("Unable to instantiate file uploader", e); + } catch (ClassCastException e) { + throw new MojoExecutionException( + "Must implement FromManifestUploader interface", + e + ); } + } } diff --git a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/UploadJarsMojo.java b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/UploadJarsMojo.java index d225e65..f418e26 100644 --- a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/UploadJarsMojo.java +++ b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/UploadJarsMojo.java @@ -1,5 +1,7 @@ package com.hubspot.maven.plugins.slimfast; +import com.google.common.base.Throwables; +import com.google.common.util.concurrent.ThreadFactoryBuilder; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; @@ -11,7 +13,6 @@ import java.util.concurrent.Future; import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; - import org.apache.maven.configuration.BeanConfigurator; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; @@ -23,13 +24,18 @@ import org.apache.maven.plugins.annotations.ResolutionScope; import org.apache.maven.project.MavenProject; -import com.google.common.base.Throwables; -import com.google.common.util.concurrent.ThreadFactoryBuilder; - -@Mojo(name = "upload", defaultPhase = LifecyclePhase.DEPLOY, threadSafe = true, requiresDependencyResolution = ResolutionScope.RUNTIME) +@Mojo( + name = "upload", + defaultPhase = LifecyclePhase.DEPLOY, + threadSafe = true, + requiresDependencyResolution = ResolutionScope.RUNTIME +) public class UploadJarsMojo extends AbstractMojo { - private static final String DEFAULT_UPLOADER = "com.hubspot.maven.plugins.slimfast.DefaultFileUploader"; - private static final String DRY_RUN_UPLOADER = "com.hubspot.maven.plugins.slimfast.DryRunFileUploader"; + + private static final String DEFAULT_UPLOADER = + "com.hubspot.maven.plugins.slimfast.DefaultFileUploader"; + private static final String DRY_RUN_UPLOADER = + "com.hubspot.maven.plugins.slimfast.DryRunFileUploader"; @Component private BeanConfigurator beanConfigurator; @@ -37,31 +43,57 @@ public class UploadJarsMojo extends AbstractMojo { @Parameter(defaultValue = "${project}", readonly = true, required = true) private MavenProject project; - @Parameter(property = "slimfast.fileUploader", alias = "fileUploader", defaultValue = DEFAULT_UPLOADER) + @Parameter( + property = "slimfast.fileUploader", + alias = "fileUploader", + defaultValue = DEFAULT_UPLOADER + ) private String fileUploaderType; - @Parameter(property = "slimfast.s3.bucket", defaultValue = "${s3.bucket}", required = true) + @Parameter( + property = "slimfast.s3.bucket", + defaultValue = "${s3.bucket}", + required = true + ) private String s3Bucket; - @Parameter(property = "slimfast.s3.artifactPrefix", defaultValue = "${s3.artifact.root}", required = true) + @Parameter( + property = "slimfast.s3.artifactPrefix", + defaultValue = "${s3.artifact.root}", + required = true + ) private String s3ArtifactRoot; - @Parameter(property = "slimfast.s3.accessKey", defaultValue = "${s3.access.key}", required = true) + @Parameter( + property = "slimfast.s3.accessKey", + defaultValue = "${s3.access.key}", + required = true + ) private String s3AccessKey; - @Parameter(property = "slimfast.s3.secretKey", defaultValue = "${s3.secret.key}", required = true) + @Parameter( + property = "slimfast.s3.secretKey", + defaultValue = "${s3.secret.key}", + required = true + ) private String s3SecretKey; @Parameter(property = "slimfast.s3.uploadThreads", defaultValue = "10") private int s3UploadThreads; - @Parameter(property = "slimfast.repositoryPath", defaultValue = "${settings.localRepository}") + @Parameter( + property = "slimfast.repositoryPath", + defaultValue = "${settings.localRepository}" + ) private String repositoryPath; @Parameter(property = "slimfast.plugin.skip", defaultValue = "false") private boolean skip; - @Parameter(property = "slimfast.outputFile", defaultValue = "${project.build.directory}/slimfast.json") + @Parameter( + property = "slimfast.outputFile", + defaultValue = "${project.build.directory}/slimfast.json" + ) private String outputFile; @Parameter(property = "slimfast.allowUnresolvedSnapshots", defaultValue = "false") @@ -77,25 +109,39 @@ public void execute() throws MojoExecutionException, MojoFailureException { return; } - LocalArtifactWrapper artifactWrapper = ArtifactHelper.getArtifactPaths(beanConfigurator, project); - final UploadConfiguration configuration = buildConfiguration(artifactWrapper.getPrefix()); + LocalArtifactWrapper artifactWrapper = ArtifactHelper.getArtifactPaths( + beanConfigurator, + project + ); + final UploadConfiguration configuration = buildConfiguration( + artifactWrapper.getPrefix() + ); FileHelper.ensureDirectoryExists(configuration.getOutputFile().getParent()); - ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("slimfast-upload").setDaemon(true).build(); - ExecutorService executor = Executors.newFixedThreadPool(s3UploadThreads, threadFactory); + ThreadFactory threadFactory = new ThreadFactoryBuilder() + .setNameFormat("slimfast-upload") + .setDaemon(true) + .build(); + ExecutorService executor = Executors.newFixedThreadPool( + s3UploadThreads, + threadFactory + ); final FileUploader uploader = instantiateFileUploader(); uploader.init(configuration, getLog()); List> futures = new ArrayList<>(); for (final LocalArtifact artifact : artifactWrapper.getArtifacts()) { - futures.add(executor.submit(new Callable() { - - @Override - public Object call() throws Exception { - uploader.upload(configuration, artifact); - return null; - } - })); + futures.add( + executor.submit( + new Callable() { + @Override + public Object call() throws Exception { + uploader.upload(configuration, artifact); + return null; + } + } + ) + ); } executor.shutdown(); @@ -103,7 +149,8 @@ public Object call() throws Exception { uploader.destroy(); } - private void waitForUploadsToFinish(ExecutorService executor, List> futures) throws MojoExecutionException, MojoFailureException { + private void waitForUploadsToFinish(ExecutorService executor, List> futures) + throws MojoExecutionException, MojoFailureException { try { if (!executor.awaitTermination(5, TimeUnit.MINUTES)) { getLog().error("Took more than 5 minutes to upload files, quitting"); @@ -112,7 +159,6 @@ private void waitForUploadsToFinish(ExecutorService executor, List> fu for (Future future : futures) { future.get(); - } } catch (InterruptedException e) { throw new MojoExecutionException("Interrupted", e); @@ -125,13 +171,13 @@ private void waitForUploadsToFinish(ExecutorService executor, List> fu private UploadConfiguration buildConfiguration(Path prefix) { return new UploadConfiguration( - prefix, - s3Bucket, - s3ArtifactRoot, - s3AccessKey, - s3SecretKey, - Paths.get(outputFile), - allowUnresolvedSnapshots + prefix, + s3Bucket, + s3ArtifactRoot, + s3AccessKey, + s3SecretKey, + Paths.get(outputFile), + allowUnresolvedSnapshots ); } @@ -141,7 +187,9 @@ private FileUploader instantiateFileUploader() throws MojoExecutionException { if (DEFAULT_UPLOADER.equals(fileUploaderType)) { resolvedFileUploaderType = DRY_RUN_UPLOADER; } else { - throw new MojoExecutionException("May not specify custom fileUploader when using the dryRun flag"); + throw new MojoExecutionException( + "May not specify custom fileUploader when using the dryRun flag" + ); } } else { resolvedFileUploaderType = fileUploaderType; diff --git a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/WriteManifestMojo.java b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/WriteManifestMojo.java index 6083e05..eb73bb4 100644 --- a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/WriteManifestMojo.java +++ b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/WriteManifestMojo.java @@ -1,5 +1,10 @@ package com.hubspot.maven.plugins.slimfast; +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.HashSet; +import java.util.Set; import org.apache.maven.configuration.BeanConfigurator; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; @@ -10,52 +15,63 @@ import org.apache.maven.plugins.annotations.ResolutionScope; import org.apache.maven.project.MavenProject; -import java.io.IOException; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.HashSet; -import java.util.Set; - -@Mojo(name = "write-manifest", threadSafe = true, requiresDependencyResolution = ResolutionScope.RUNTIME) +@Mojo( + name = "write-manifest", + threadSafe = true, + requiresDependencyResolution = ResolutionScope.RUNTIME +) public class WriteManifestMojo extends AbstractMojo { - @Component - private BeanConfigurator beanConfigurator; - - @Parameter(defaultValue = "${project}", readonly = true, required = true) - private MavenProject project; - - @Parameter(property = "slimfast.plugin.skip", defaultValue = "false") - private boolean skip; - - @Parameter(property = "slimfast.outputFile", defaultValue = "${project.build.directory}/slimfast-local.json") - private String outputFile; - - @Override - public void execute() throws MojoExecutionException, MojoFailureException { - if (skip) { - getLog().info("Skipping plugin execution"); - return; - } - - LocalArtifactWrapper artifactWrapper = ArtifactHelper.getArtifactPaths(beanConfigurator, project); - Path outputFile = Paths.get(this.outputFile); - FileHelper.ensureDirectoryExists(outputFile.getParent()); - Path prefix = artifactWrapper.getPrefix(); - - Set s3Artifacts = new HashSet<>(); - for (LocalArtifact artifact : artifactWrapper.getArtifacts()) { - s3Artifacts.add(prepareArtifact(artifact)); - } - - PreparedArtifactWrapper preparedArtifactWrapper = new PreparedArtifactWrapper(prefix.toString(), s3Artifacts); - try { - JsonHelper.writeArtifactsToJson(outputFile.toFile(), preparedArtifactWrapper); - } catch (IOException e) { - throw new MojoFailureException("Failed writing manifest file to disk", e); - } + + @Component + private BeanConfigurator beanConfigurator; + + @Parameter(defaultValue = "${project}", readonly = true, required = true) + private MavenProject project; + + @Parameter(property = "slimfast.plugin.skip", defaultValue = "false") + private boolean skip; + + @Parameter( + property = "slimfast.outputFile", + defaultValue = "${project.build.directory}/slimfast-local.json" + ) + private String outputFile; + + @Override + public void execute() throws MojoExecutionException, MojoFailureException { + if (skip) { + getLog().info("Skipping plugin execution"); + return; } - private PreparedArtifact prepareArtifact(LocalArtifact artifact) { - return new PreparedArtifact(artifact.getLocalPath().toString(), artifact.getTargetPath().toString()); + LocalArtifactWrapper artifactWrapper = ArtifactHelper.getArtifactPaths( + beanConfigurator, + project + ); + Path outputFile = Paths.get(this.outputFile); + FileHelper.ensureDirectoryExists(outputFile.getParent()); + Path prefix = artifactWrapper.getPrefix(); + + Set s3Artifacts = new HashSet<>(); + for (LocalArtifact artifact : artifactWrapper.getArtifacts()) { + s3Artifacts.add(prepareArtifact(artifact)); } + + PreparedArtifactWrapper preparedArtifactWrapper = new PreparedArtifactWrapper( + prefix.toString(), + s3Artifacts + ); + try { + JsonHelper.writeArtifactsToJson(outputFile.toFile(), preparedArtifactWrapper); + } catch (IOException e) { + throw new MojoFailureException("Failed writing manifest file to disk", e); + } + } + + private PreparedArtifact prepareArtifact(LocalArtifact artifact) { + return new PreparedArtifact( + artifact.getLocalPath().toString(), + artifact.getTargetPath().toString() + ); + } } diff --git a/spotbugs-exclude-filter.xml b/spotbugs-exclude-filter.xml new file mode 100644 index 0000000..8a00f6b --- /dev/null +++ b/spotbugs-exclude-filter.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + From 126e5ec0be9fa30a8dad377217fcca7c9dbf0cdb Mon Sep 17 00:00:00 2001 From: Jared Stehler Date: Sun, 10 Mar 2024 14:15:19 -0400 Subject: [PATCH 2/3] use release basepom --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 3c54b82..536f0d9 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.hubspot basepom - 59.2-SNAPSHOT + 59.2 com.hubspot.slimfast From 7150ffbd0179d238bf65466a9ffbe0af796dbadc Mon Sep 17 00:00:00 2001 From: Jared Stehler Date: Tue, 12 Mar 2024 09:47:29 -0400 Subject: [PATCH 3/3] moving spotbugs configs to basepom --- pom.xml | 22 +------------------ .../maven/plugins/slimfast/S3Factory.java | 2 ++ spotbugs-exclude-filter.xml | 12 ---------- 3 files changed, 3 insertions(+), 33 deletions(-) delete mode 100644 spotbugs-exclude-filter.xml diff --git a/pom.xml b/pom.xml index 536f0d9..989f31c 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.hubspot basepom - 59.2 + 59.3 com.hubspot.slimfast @@ -88,26 +88,6 @@ - - - - com.github.spotbugs - spotbugs-maven-plugin - - ${project.parent.basedir}/spotbugs-exclude-filter.xml - - - - - check - - package - - - - - - Jonathan Haber diff --git a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/S3Factory.java b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/S3Factory.java index 2115382..ec17e21 100644 --- a/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/S3Factory.java +++ b/slimfast-plugin/src/main/java/com/hubspot/maven/plugins/slimfast/S3Factory.java @@ -6,9 +6,11 @@ import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; public class S3Factory { + @SuppressFBWarnings("CT_CONSTRUCTOR_THROW") private S3Factory() { throw new AssertionError(); } diff --git a/spotbugs-exclude-filter.xml b/spotbugs-exclude-filter.xml deleted file mode 100644 index 8a00f6b..0000000 --- a/spotbugs-exclude-filter.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - - - - - -