diff options
Diffstat (limited to 'openecomp-be/tools/artifact-copy-plugin/src')
4 files changed, 367 insertions, 0 deletions
diff --git a/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/ArtifactHelper.java b/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/ArtifactHelper.java new file mode 100644 index 0000000000..e7ad5dacb1 --- /dev/null +++ b/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/ArtifactHelper.java @@ -0,0 +1,56 @@ +package org.openecomp.sdc.onboarding.util; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.project.MavenProject; + +public class ArtifactHelper { + + private MavenProject project; + + List<ArtifactRepository> getRepositories(boolean snapshotRepo) { + List<ArtifactRepository> list = new ArrayList<>(); + for (ArtifactRepository artRepo : project.getRemoteArtifactRepositories()) { + if (snapshotRepo) { + if (artRepo.getSnapshots().isEnabled()) { + list.add(artRepo); + } + } else { + if (artRepo.getReleases().isEnabled()) { + list.add(artRepo); + } + } + } + return list; + } + + String getContents(URL path) throws IOException { + try (InputStream is = path.openStream(); Scanner scnr = new Scanner(is).useDelimiter("\\A")) { + return scnr.hasNext() ? scnr.next() : ""; + } + } + + String getChecksum(String filePath, String hashType) throws NoSuchAlgorithmException, IOException { + MessageDigest md = MessageDigest.getInstance(hashType); + md.update(Files.readAllBytes(Paths.get(filePath))); + byte[] hashBytes = md.digest(); + + StringBuffer buffer = new StringBuffer(); + for (byte hashByte : hashBytes) { + buffer.append(Integer.toString((hashByte & 0xff) + 0x100, 16).substring(1)); + } + return buffer.toString(); + } + +} + + diff --git a/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/CalibrateArtifactPlugin.java b/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/CalibrateArtifactPlugin.java new file mode 100644 index 0000000000..4838608835 --- /dev/null +++ b/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/CalibrateArtifactPlugin.java @@ -0,0 +1,82 @@ +package org.openecomp.sdc.onboarding.util; + +import java.io.File; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugins.annotations.Component; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.plugins.annotations.ResolutionScope; +import org.apache.maven.project.MavenProject; +import org.apache.maven.project.MavenProjectHelper; + +@Mojo(name = "calibrate-artifact-helper", threadSafe = true, defaultPhase = LifecyclePhase.INSTALL, + requiresDependencyResolution = ResolutionScope.TEST) +public class CalibrateArtifactPlugin extends AbstractMojo { + + private static final String ARTIFACT_COPY_PATH = "artifactPathToCopy"; + + @Parameter(defaultValue = "${session}") + private MavenSession session; + @Parameter(defaultValue = "${project}", readonly = true) + private MavenProject project; + @Component + private MavenProjectHelper projectHelper; + @Parameter + private String groupId; + @Parameter + private String artifactId; + @Parameter + private String version; + @Parameter + private String targetLocation; + @Parameter + private String name; + @Parameter + private String excludePackaging; + @Parameter + private ArtifactHelper artifactHelper; + + public void execute() throws MojoExecutionException, MojoFailureException { + if (project.getPackaging().equals(excludePackaging)) { + return; + } + if (project.getProperties().containsKey(ARTIFACT_COPY_PATH) + && project.getProperties().getProperty(ARTIFACT_COPY_PATH) != null) { + File f = null; + String artifactPath = project.getProperties().getProperty(ARTIFACT_COPY_PATH) + .startsWith(session.getLocalRepository().getBasedir()) ? + project.getProperties().getProperty(ARTIFACT_COPY_PATH) : + project.getProperties().getProperty(ARTIFACT_COPY_PATH) + .replace(groupId, groupId.replace('.', '/')); + if (artifactPath.startsWith(session.getLocalRepository().getBasedir())) { + f = new File(artifactPath); + } else { + f = new File(session.getLocalRepository().getBasedir(), artifactPath); + } + if (f.exists()) { + project.getArtifact().setFile(f); + } + } + File file = new File(project.getBuild().getDirectory(), project.getBuild().getFinalName() + ".unicorn"); + if (file.exists()) { + try { + Files.copy(file.toPath(), Paths.get( + session.getLocalRepository().getBasedir() + File.separator + project.getGroupId().replace(".", + File.separator) + File.separator + project.getArtifactId() + File.separator + + project.getVersion(), project.getBuild().getFinalName() + ".unicorn"), + StandardCopyOption.REPLACE_EXISTING); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } + } +} diff --git a/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/CopyArtifactPlugin.java b/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/CopyArtifactPlugin.java new file mode 100644 index 0000000000..20b1a7c940 --- /dev/null +++ b/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/CopyArtifactPlugin.java @@ -0,0 +1,141 @@ +package org.openecomp.sdc.onboarding.util; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; +import java.nio.file.StandardOpenOption; +import java.security.NoSuchAlgorithmException; +import java.util.Arrays; +import java.util.List; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.plugins.annotations.ResolutionScope; +import org.apache.maven.project.MavenProject; + +@Mojo(name = "copy-helper", threadSafe = true, defaultPhase = LifecyclePhase.CLEAN, + requiresDependencyResolution = ResolutionScope.NONE) +public class CopyArtifactPlugin extends AbstractMojo { + + @Parameter(defaultValue = "${session}") + private MavenSession session; + @Parameter(defaultValue = "${project}", readonly = true) + private MavenProject project; + @Parameter + private String groupId; + @Parameter + private String artifactId; + @Parameter + private String version; + @Parameter + private String targetLocation; + @Parameter + private String name; + @Parameter + private ArtifactHelper artifactHelper; + + public void execute() throws MojoExecutionException, MojoFailureException { + if (!project.getProperties().containsKey("resolvedVersion")) { + return; + } + boolean isSnapshot = version.contains("SNAPSHOT"); + List<ArtifactRepository> artRepoList = artifactHelper.getRepositories(isSnapshot); + String resolvedVersion = + project.getProperties().getProperty("resolvedVersion"); + try { + if (!version.equals(resolvedVersion)) { + if(copyResolvedArtifact(artRepoList, resolvedVersion) && getLog().isInfoEnabled()){ + getLog().info("Data Artifact Copied with "+resolvedVersion); + } + + } + File orgFile = new File( + session.getLocalRepository().getBasedir() + File.separator + (groupId.replace(".", File.separator)) + + File.separator + artifactId + File.separator + version); + if (!orgFile.exists()) { + return; + } + File[] list = orgFile.listFiles(t -> t.getName().equals(artifactId + "-" + version + ".jar")); + if (list != null && list.length > 0) { + String directory = session.getLocalRepository().getBasedir() + File.separator + (groupId.replace(".", + File.separator)) + File.separator + targetLocation + File.separator + version; + if (!Paths.get(directory, name).toFile().exists()) { + return; + } + Files.copy(list[0].toPath(), Paths.get(directory, name), StandardCopyOption.REPLACE_EXISTING); + copyTargetArtifact(directory, list[0]); + } + } catch (IOException | NoSuchAlgorithmException e) { + throw new MojoFailureException(e.getMessage()); + } + } + + private void copyTargetArtifact(String directory, File source) throws IOException, NoSuchAlgorithmException { + File[] files = new File(directory).listFiles( + f -> f.getName().endsWith(".jar") && !f.getName().equals(name) && f.getName().startsWith( + name.substring(0, name.lastIndexOf('-')))); + if (files == null || files.length == 0) { + return; + } + Arrays.sort(files, this::compare); + File tgtFile = files[files.length - 1]; + Files.copy(source.toPath(), tgtFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + for (String checksumType : Arrays.asList("sha1", "md5")) { + File potentialFile = new File(tgtFile.getAbsolutePath() + "." + checksumType); + if (potentialFile.exists()) { + Files.write(potentialFile.toPath(), + artifactHelper.getChecksum(source.getAbsolutePath(), checksumType).getBytes(), + StandardOpenOption.CREATE); + } + } + } + + + private boolean copyResolvedArtifact(List<ArtifactRepository> list, String resolvedVersion){ + for (ArtifactRepository repo : list) { + try { + writeContents( + new URL(repo.getUrl() + (groupId.replace('.', '/')) + '/' + artifactId + '/' + version + '/' + + artifactId + "-" + (version.equals(resolvedVersion) ? version : + version.replace("SNAPSHOT", resolvedVersion)) + + ".jar")); + return true; + } catch (IOException e) { + getLog().warn(e); + } + } + return false; + } + + + private void writeContents(URL path) throws IOException { + String directory = + session.getLocalRepository().getBasedir() + File.separator + (groupId.replace(".", File.separator)) + + File.separator + artifactId + File.separator + version; + try (InputStream is = path.openStream()) { + Files.copy(is, Paths.get(directory, artifactId + "-" + version + ".jar"), + StandardCopyOption.REPLACE_EXISTING); + } + + } + + private int compare(File file1, File file2) { + if (file1.lastModified() > file2.lastModified()) { + return 1; + } + if (file1.lastModified() < file2.lastModified()) { + return -1; + } + return 0; + } + +} diff --git a/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/InitializationHelperMojo.java b/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/InitializationHelperMojo.java new file mode 100644 index 0000000000..067517504c --- /dev/null +++ b/openecomp-be/tools/artifact-copy-plugin/src/main/java/org/openecomp/sdc/onboarding/util/InitializationHelperMojo.java @@ -0,0 +1,88 @@ +package org.openecomp.sdc.onboarding.util; + +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.plugins.annotations.ResolutionScope; +import org.apache.maven.project.MavenProject; + +@Mojo(name = "init-artifact-helper", threadSafe = true, defaultPhase = LifecyclePhase.PRE_CLEAN, + requiresDependencyResolution = ResolutionScope.NONE) +public class InitializationHelperMojo extends AbstractMojo { + + private static final String SKIP_GET = "skipGet"; + + @Parameter(defaultValue = "${session}") + private MavenSession session; + @Parameter(defaultValue = "${project}", readonly = true) + private MavenProject project; + @Parameter + private String groupId; + @Parameter + private String artifactId; + @Parameter + private String version; + @Parameter + private String targetLocation; + @Parameter + private String name; + @Parameter + private String excludePackaging; + @Parameter + private ArtifactHelper artifactHelper; + + public void execute() throws MojoExecutionException, MojoFailureException { + if (System.getProperties().containsKey(SKIP_GET)) { + project.getProperties() + .setProperty(SKIP_GET, Boolean.valueOf(System.getProperties().containsKey(SKIP_GET)).toString()); + return; + } else { + File orgFile = new File( + session.getLocalRepository().getBasedir() + File.separator + (groupId.replace(".", File.separator)) + + File.separator + artifactId + File.separator + version); + String resolvedVersion = getResolvedVersion(artifactHelper.getRepositories(version.contains("SNAPSHOT"))); + project.getProperties().setProperty("resolvedVersion", resolvedVersion); + System.getProperties().setProperty(SKIP_GET, Boolean.TRUE.toString()); + if (resolvedVersion.equals(version) && !orgFile.exists()) { + project.getProperties().setProperty(SKIP_GET, Boolean.TRUE.toString()); + } + } + } + + private String getResolvedVersion(List<ArtifactRepository> list) { + Pattern timestampPattern = Pattern.compile(".*<timestamp>(.*)</timestamp>.*"); + Pattern buildNumberPattern = Pattern.compile(".*<buildNumber>(.*)</buildNumber>.*"); + String timestamp = null; + String buildNumber = null; + for (ArtifactRepository repo : list) { + try { + String content = artifactHelper.getContents( + new URL(repo.getUrl() + (groupId.replace('.', '/')) + '/' + artifactId + '/' + version + + "/maven-metadata.xml")); + Matcher m = timestampPattern.matcher(content); + if (m.find()) { + timestamp = m.group(1); + } + m = buildNumberPattern.matcher(content); + if (m.find()) { + buildNumber = m.group(1); + } + } catch (IOException e) { + continue; + } + } + return timestamp != null && buildNumber != null ? timestamp + "-" + buildNumber : version; + } + +} |