1 package org.openecomp.sdc.onboarding.util;
4 import java.io.IOException;
5 import java.io.InputStream;
7 import java.nio.file.Files;
8 import java.nio.file.Paths;
9 import java.nio.file.StandardCopyOption;
10 import java.nio.file.StandardOpenOption;
11 import java.security.NoSuchAlgorithmException;
12 import java.util.Arrays;
13 import java.util.List;
14 import org.apache.maven.artifact.repository.ArtifactRepository;
15 import org.apache.maven.execution.MavenSession;
16 import org.apache.maven.plugin.AbstractMojo;
17 import org.apache.maven.plugin.MojoExecutionException;
18 import org.apache.maven.plugin.MojoFailureException;
19 import org.apache.maven.plugins.annotations.LifecyclePhase;
20 import org.apache.maven.plugins.annotations.Mojo;
21 import org.apache.maven.plugins.annotations.Parameter;
22 import org.apache.maven.plugins.annotations.ResolutionScope;
23 import org.apache.maven.project.MavenProject;
25 @Mojo(name = "copy-helper", threadSafe = true, defaultPhase = LifecyclePhase.CLEAN,
26 requiresDependencyResolution = ResolutionScope.NONE)
27 public class CopyArtifactPlugin extends AbstractMojo {
29 @Parameter(defaultValue = "${session}")
30 private MavenSession session;
31 @Parameter(defaultValue = "${project}", readonly = true)
32 private MavenProject project;
34 private String groupId;
36 private String artifactId;
38 private String version;
40 private String targetLocation;
44 private ArtifactHelper artifactHelper;
46 public void execute() throws MojoExecutionException, MojoFailureException {
47 if (!project.getProperties().containsKey("resolvedVersion")) {
50 boolean isSnapshot = version.contains("SNAPSHOT");
51 List<ArtifactRepository> artRepoList = artifactHelper.getRepositories(isSnapshot);
52 String resolvedVersion =
53 project.getProperties().getProperty("resolvedVersion");
55 if (!version.equals(resolvedVersion)) {
56 if(copyResolvedArtifact(artRepoList, resolvedVersion) && getLog().isInfoEnabled()){
57 getLog().info("Data Artifact Copied with "+resolvedVersion);
61 File orgFile = new File(
62 session.getLocalRepository().getBasedir() + File.separator + (groupId.replace(".", File.separator))
63 + File.separator + artifactId + File.separator + version);
64 if (!orgFile.exists()) {
67 File[] list = orgFile.listFiles(t -> t.getName().equals(artifactId + "-" + version + ".jar"));
68 if (list != null && list.length > 0) {
69 String directory = session.getLocalRepository().getBasedir() + File.separator + (groupId.replace(".",
70 File.separator)) + File.separator + targetLocation + File.separator + version;
71 if (!Paths.get(directory, name).toFile().exists()) {
74 Files.copy(list[0].toPath(), Paths.get(directory, name), StandardCopyOption.REPLACE_EXISTING);
75 copyTargetArtifact(directory, list[0]);
77 } catch (IOException | NoSuchAlgorithmException e) {
78 throw new MojoFailureException(e.getMessage());
82 private void copyTargetArtifact(String directory, File source) throws IOException, NoSuchAlgorithmException {
83 File[] files = new File(directory).listFiles(
84 f -> f.getName().endsWith(".jar") && !f.getName().equals(name) && f.getName().startsWith(
85 name.substring(0, name.lastIndexOf('-'))));
86 if (files == null || files.length == 0) {
89 Arrays.sort(files, this::compare);
90 File tgtFile = files[files.length - 1];
91 Files.copy(source.toPath(), tgtFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
92 for (String checksumType : Arrays.asList("sha1", "md5")) {
93 File potentialFile = new File(tgtFile.getAbsolutePath() + "." + checksumType);
94 if (potentialFile.exists()) {
95 Files.write(potentialFile.toPath(),
96 artifactHelper.getChecksum(source.getAbsolutePath(), checksumType).getBytes(),
97 StandardOpenOption.CREATE);
103 private boolean copyResolvedArtifact(List<ArtifactRepository> list, String resolvedVersion){
104 for (ArtifactRepository repo : list) {
107 new URL(repo.getUrl() + (groupId.replace('.', '/')) + '/' + artifactId + '/' + version + '/'
108 + artifactId + "-" + (version.equals(resolvedVersion) ? version :
109 version.replace("SNAPSHOT", resolvedVersion))
112 } catch (IOException e) {
120 private void writeContents(URL path) throws IOException {
122 session.getLocalRepository().getBasedir() + File.separator + (groupId.replace(".", File.separator))
123 + File.separator + artifactId + File.separator + version;
124 try (InputStream is = path.openStream()) {
125 Files.copy(is, Paths.get(directory, artifactId + "-" + version + ".jar"),
126 StandardCopyOption.REPLACE_EXISTING);
131 private int compare(File file1, File file2) {
132 if (file1.lastModified() > file2.lastModified()) {
135 if (file1.lastModified() < file2.lastModified()) {