483860883580dedd311060c44f43f488c6e133da
[sdc.git] /
1 package org.openecomp.sdc.onboarding.util;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.UncheckedIOException;
6 import java.nio.file.Files;
7 import java.nio.file.Paths;
8 import java.nio.file.StandardCopyOption;
9 import org.apache.maven.execution.MavenSession;
10 import org.apache.maven.plugin.AbstractMojo;
11 import org.apache.maven.plugin.MojoExecutionException;
12 import org.apache.maven.plugin.MojoFailureException;
13 import org.apache.maven.plugins.annotations.Component;
14 import org.apache.maven.plugins.annotations.LifecyclePhase;
15 import org.apache.maven.plugins.annotations.Mojo;
16 import org.apache.maven.plugins.annotations.Parameter;
17 import org.apache.maven.plugins.annotations.ResolutionScope;
18 import org.apache.maven.project.MavenProject;
19 import org.apache.maven.project.MavenProjectHelper;
20
21 @Mojo(name = "calibrate-artifact-helper", threadSafe = true, defaultPhase = LifecyclePhase.INSTALL,
22         requiresDependencyResolution = ResolutionScope.TEST)
23 public class CalibrateArtifactPlugin extends AbstractMojo {
24
25     private static final String ARTIFACT_COPY_PATH = "artifactPathToCopy";
26
27     @Parameter(defaultValue = "${session}")
28     private MavenSession session;
29     @Parameter(defaultValue = "${project}", readonly = true)
30     private MavenProject project;
31     @Component
32     private MavenProjectHelper projectHelper;
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 String excludePackaging;
45     @Parameter
46     private ArtifactHelper artifactHelper;
47
48     public void execute() throws MojoExecutionException, MojoFailureException {
49         if (project.getPackaging().equals(excludePackaging)) {
50             return;
51         }
52         if (project.getProperties().containsKey(ARTIFACT_COPY_PATH)
53                     && project.getProperties().getProperty(ARTIFACT_COPY_PATH) != null) {
54             File f = null;
55             String artifactPath = project.getProperties().getProperty(ARTIFACT_COPY_PATH)
56                                          .startsWith(session.getLocalRepository().getBasedir()) ?
57                                           project.getProperties().getProperty(ARTIFACT_COPY_PATH) :
58                                           project.getProperties().getProperty(ARTIFACT_COPY_PATH)
59                                                  .replace(groupId, groupId.replace('.', '/'));
60             if (artifactPath.startsWith(session.getLocalRepository().getBasedir())) {
61                 f = new File(artifactPath);
62             } else {
63                 f = new File(session.getLocalRepository().getBasedir(), artifactPath);
64             }
65             if (f.exists()) {
66                 project.getArtifact().setFile(f);
67             }
68         }
69         File file = new File(project.getBuild().getDirectory(), project.getBuild().getFinalName() + ".unicorn");
70         if (file.exists()) {
71             try {
72                 Files.copy(file.toPath(), Paths.get(
73                         session.getLocalRepository().getBasedir() + File.separator + project.getGroupId().replace(".",
74                                 File.separator) + File.separator + project.getArtifactId() + File.separator
75                                 + project.getVersion(), project.getBuild().getFinalName() + ".unicorn"),
76                         StandardCopyOption.REPLACE_EXISTING);
77             } catch (IOException e) {
78                 throw new UncheckedIOException(e);
79             }
80         }
81     }
82 }