20b1a7c940274d7d2b6f4b986ddcb0599da5d380
[sdc.git] /
1 package org.openecomp.sdc.onboarding.util;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.net.URL;
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;
24
25 @Mojo(name = "copy-helper", threadSafe = true, defaultPhase = LifecyclePhase.CLEAN,
26         requiresDependencyResolution = ResolutionScope.NONE)
27 public class CopyArtifactPlugin extends AbstractMojo {
28
29     @Parameter(defaultValue = "${session}")
30     private MavenSession session;
31     @Parameter(defaultValue = "${project}", readonly = true)
32     private MavenProject project;
33     @Parameter
34     private String groupId;
35     @Parameter
36     private String artifactId;
37     @Parameter
38     private String version;
39     @Parameter
40     private String targetLocation;
41     @Parameter
42     private String name;
43     @Parameter
44     private ArtifactHelper artifactHelper;
45
46     public void execute() throws MojoExecutionException, MojoFailureException {
47         if (!project.getProperties().containsKey("resolvedVersion")) {
48             return;
49         }
50         boolean isSnapshot = version.contains("SNAPSHOT");
51         List<ArtifactRepository> artRepoList = artifactHelper.getRepositories(isSnapshot);
52         String resolvedVersion =
53                 project.getProperties().getProperty("resolvedVersion");
54         try {
55             if (!version.equals(resolvedVersion)) {
56                 if(copyResolvedArtifact(artRepoList, resolvedVersion) && getLog().isInfoEnabled()){
57                     getLog().info("Data Artifact Copied with "+resolvedVersion);
58                 }
59
60             }
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()) {
65                 return;
66             }
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()) {
72                     return;
73                 }
74                 Files.copy(list[0].toPath(), Paths.get(directory, name), StandardCopyOption.REPLACE_EXISTING);
75                 copyTargetArtifact(directory, list[0]);
76             }
77         } catch (IOException | NoSuchAlgorithmException e) {
78             throw new MojoFailureException(e.getMessage());
79         }
80     }
81
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) {
87             return;
88         }
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);
98             }
99         }
100     }
101
102
103     private boolean copyResolvedArtifact(List<ArtifactRepository> list, String resolvedVersion){
104         for (ArtifactRepository repo : list) {
105             try {
106                 writeContents(
107                         new URL(repo.getUrl() + (groupId.replace('.', '/')) + '/' + artifactId + '/' + version + '/'
108                                         + artifactId + "-" + (version.equals(resolvedVersion) ? version :
109                                                                       version.replace("SNAPSHOT", resolvedVersion))
110                                         + ".jar"));
111                 return true;
112             } catch (IOException e) {
113                 getLog().warn(e);
114             }
115         }
116         return false;
117     }
118
119
120     private void writeContents(URL path) throws IOException {
121         String directory =
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);
127         }
128
129     }
130
131     private int compare(File file1, File file2) {
132         if (file1.lastModified() > file2.lastModified()) {
133             return 1;
134         }
135         if (file1.lastModified() < file2.lastModified()) {
136             return -1;
137         }
138         return 0;
139     }
140
141 }