c89fdd2a4ed9f53583d643ddd6027ba8d09dcdd2
[sdc.git] /
1 /*
2  * Copyright © 2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on a "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.onboarding.util;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.net.URL;
23 import java.nio.file.Files;
24 import java.nio.file.Paths;
25 import java.nio.file.StandardCopyOption;
26 import java.nio.file.StandardOpenOption;
27 import java.security.NoSuchAlgorithmException;
28 import java.util.Arrays;
29 import java.util.List;
30 import org.apache.maven.artifact.repository.ArtifactRepository;
31 import org.apache.maven.execution.MavenSession;
32 import org.apache.maven.plugin.AbstractMojo;
33 import org.apache.maven.plugin.MojoExecutionException;
34 import org.apache.maven.plugin.MojoFailureException;
35 import org.apache.maven.plugins.annotations.LifecyclePhase;
36 import org.apache.maven.plugins.annotations.Mojo;
37 import org.apache.maven.plugins.annotations.Parameter;
38 import org.apache.maven.plugins.annotations.ResolutionScope;
39 import org.apache.maven.project.MavenProject;
40
41 @Mojo(name = "copy-helper", threadSafe = true, defaultPhase = LifecyclePhase.CLEAN,
42         requiresDependencyResolution = ResolutionScope.NONE)
43 public class CopyArtifactPlugin extends AbstractMojo {
44
45     @Parameter(defaultValue = "${session}")
46     private MavenSession session;
47     @Parameter(defaultValue = "${project}", readonly = true)
48     private MavenProject project;
49     @Parameter
50     private String groupId;
51     @Parameter
52     private String artifactId;
53     @Parameter
54     private String version;
55     @Parameter
56     private String targetLocation;
57     @Parameter
58     private String name;
59     @Parameter
60     private ArtifactHelper artifactHelper;
61
62     @Override
63     public void execute() throws MojoExecutionException, MojoFailureException {
64         if (!project.getProperties().containsKey("resolvedVersion")) {
65             return;
66         }
67         boolean isSnapshot = version.contains("SNAPSHOT");
68         List<ArtifactRepository> artRepoList = artifactHelper.getRepositories(isSnapshot);
69         String resolvedVersion = project.getProperties().getProperty("resolvedVersion");
70         try {
71             if (!version.equals(resolvedVersion)) {
72                 boolean result = copyResolvedArtifact(artRepoList, resolvedVersion);
73                 if (result && getLog().isInfoEnabled()) {
74                     getLog().info("Data Artifact Copied with " + resolvedVersion);
75                 }
76
77             }
78             File orgFile = new File(
79                     session.getLocalRepository().getBasedir() + File.separator + (groupId.replace(".", File.separator))
80                             + File.separator + artifactId + File.separator + version);
81             if (!orgFile.exists()) {
82                 return;
83             }
84             File[] list = orgFile.listFiles(t -> t.getName().equals(artifactId + "-" + version + ".jar"));
85             if (list != null && list.length > 0) {
86                 String directory = session.getLocalRepository().getBasedir() + File.separator + (groupId.replace(".",
87                         File.separator)) + File.separator + targetLocation + File.separator + version;
88                 if (!Paths.get(directory, name).toFile().exists()) {
89                     return;
90                 }
91                 Files.copy(list[0].toPath(), Paths.get(directory, name), StandardCopyOption.REPLACE_EXISTING);
92                 copyTargetArtifact(directory, list[0]);
93             }
94         } catch (IOException | NoSuchAlgorithmException e) {
95             throw new MojoFailureException(e.getMessage(), e);
96         }
97     }
98
99     private void copyTargetArtifact(String directory, File source) throws IOException, NoSuchAlgorithmException {
100         File[] files = new File(directory).listFiles(
101                 f -> f.getName().endsWith(".jar") && !f.getName().equals(name) && f.getName().startsWith(
102                         name.substring(0, name.lastIndexOf('-'))));
103         if (files == null || files.length == 0) {
104             return;
105         }
106         Arrays.sort(files, this::compare);
107         File tgtFile = files[files.length - 1];
108         Files.copy(source.toPath(), tgtFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
109         for (String checksumType : Arrays.asList("sha1", "md5")) {
110             File potentialFile = new File(tgtFile.getAbsolutePath() + "." + checksumType);
111             if (potentialFile.exists()) {
112                 Files.write(potentialFile.toPath(),
113                         artifactHelper.getChecksum(source.getAbsolutePath(), checksumType).getBytes(),
114                         StandardOpenOption.CREATE);
115             }
116         }
117     }
118
119
120     private boolean copyResolvedArtifact(List<ArtifactRepository> list, String resolvedVersion) {
121         for (ArtifactRepository repo : list) {
122             try {
123                 writeContents(
124                         new URL(repo.getUrl() + (groupId.replace('.', '/')) + '/' + artifactId + '/' + version + '/'
125                                         + artifactId + "-" + (version.equals(resolvedVersion) ? version :
126                                                                       version.replace("SNAPSHOT", resolvedVersion))
127                                         + ".jar"));
128                 return true;
129             } catch (IOException e) {
130                 getLog().debug(e);
131             }
132         }
133         return false;
134     }
135
136
137     private void writeContents(URL path) throws IOException {
138         String directory =
139                 session.getLocalRepository().getBasedir() + File.separator + (groupId.replace(".", File.separator))
140                         + File.separator + artifactId + File.separator + version;
141         try (InputStream is = path.openStream()) {
142             Files.copy(is, Paths.get(directory, artifactId + "-" + version + ".jar"),
143                     StandardCopyOption.REPLACE_EXISTING);
144         }
145
146     }
147
148     private int compare(File file1, File file2) {
149         if (file1.lastModified() > file2.lastModified()) {
150             return 1;
151         }
152         if (file1.lastModified() < file2.lastModified()) {
153             return -1;
154         }
155         return 0;
156     }
157
158 }