2 * Copyright © 2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.onboarding.util;
20 import java.io.IOException;
21 import java.io.InputStream;
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;
41 @Mojo(name = "copy-helper", threadSafe = true, defaultPhase = LifecyclePhase.CLEAN,
42 requiresDependencyResolution = ResolutionScope.NONE)
43 public class CopyArtifactPlugin extends AbstractMojo {
45 @Parameter(defaultValue = "${session}")
46 private MavenSession session;
47 @Parameter(defaultValue = "${project}", readonly = true)
48 private MavenProject project;
50 private String groupId;
52 private String artifactId;
54 private String version;
56 private String targetLocation;
60 private ArtifactHelper artifactHelper;
63 public void execute() throws MojoExecutionException, MojoFailureException {
64 if (!project.getProperties().containsKey("resolvedVersion")) {
67 boolean isSnapshot = version.contains("SNAPSHOT");
68 List<ArtifactRepository> artRepoList = artifactHelper.getRepositories(isSnapshot);
69 String resolvedVersion = project.getProperties().getProperty("resolvedVersion");
71 if (!version.equals(resolvedVersion)) {
72 boolean result = copyResolvedArtifact(artRepoList, resolvedVersion);
73 if (result && getLog().isInfoEnabled()) {
74 getLog().info("Data Artifact Copied with " + resolvedVersion);
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()) {
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()) {
91 Files.copy(list[0].toPath(), Paths.get(directory, name), StandardCopyOption.REPLACE_EXISTING);
92 copyTargetArtifact(directory, list[0]);
94 } catch (IOException | NoSuchAlgorithmException e) {
95 throw new MojoFailureException(e.getMessage(), e);
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) {
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);
120 private boolean copyResolvedArtifact(List<ArtifactRepository> list, String resolvedVersion) {
121 for (ArtifactRepository repo : list) {
124 new URL(repo.getUrl() + (groupId.replace('.', '/')) + '/' + artifactId + '/' + version + '/'
125 + artifactId + "-" + (version.equals(resolvedVersion) ? version :
126 version.replace("SNAPSHOT", resolvedVersion))
129 } catch (IOException e) {
137 private void writeContents(URL path) throws IOException {
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);
148 private int compare(File file1, File file2) {
149 if (file1.lastModified() > file2.lastModified()) {
152 if (file1.lastModified() < file2.lastModified()) {