blob: 067517504c96e98cc9354aa9f4f49008c2987577 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
package org.openecomp.sdc.onboarding.util;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
@Mojo(name = "init-artifact-helper", threadSafe = true, defaultPhase = LifecyclePhase.PRE_CLEAN,
requiresDependencyResolution = ResolutionScope.NONE)
public class InitializationHelperMojo extends AbstractMojo {
private static final String SKIP_GET = "skipGet";
@Parameter(defaultValue = "${session}")
private MavenSession session;
@Parameter(defaultValue = "${project}", readonly = true)
private MavenProject project;
@Parameter
private String groupId;
@Parameter
private String artifactId;
@Parameter
private String version;
@Parameter
private String targetLocation;
@Parameter
private String name;
@Parameter
private String excludePackaging;
@Parameter
private ArtifactHelper artifactHelper;
public void execute() throws MojoExecutionException, MojoFailureException {
if (System.getProperties().containsKey(SKIP_GET)) {
project.getProperties()
.setProperty(SKIP_GET, Boolean.valueOf(System.getProperties().containsKey(SKIP_GET)).toString());
return;
} else {
File orgFile = new File(
session.getLocalRepository().getBasedir() + File.separator + (groupId.replace(".", File.separator))
+ File.separator + artifactId + File.separator + version);
String resolvedVersion = getResolvedVersion(artifactHelper.getRepositories(version.contains("SNAPSHOT")));
project.getProperties().setProperty("resolvedVersion", resolvedVersion);
System.getProperties().setProperty(SKIP_GET, Boolean.TRUE.toString());
if (resolvedVersion.equals(version) && !orgFile.exists()) {
project.getProperties().setProperty(SKIP_GET, Boolean.TRUE.toString());
}
}
}
private String getResolvedVersion(List<ArtifactRepository> list) {
Pattern timestampPattern = Pattern.compile(".*<timestamp>(.*)</timestamp>.*");
Pattern buildNumberPattern = Pattern.compile(".*<buildNumber>(.*)</buildNumber>.*");
String timestamp = null;
String buildNumber = null;
for (ArtifactRepository repo : list) {
try {
String content = artifactHelper.getContents(
new URL(repo.getUrl() + (groupId.replace('.', '/')) + '/' + artifactId + '/' + version
+ "/maven-metadata.xml"));
Matcher m = timestampPattern.matcher(content);
if (m.find()) {
timestamp = m.group(1);
}
m = buildNumberPattern.matcher(content);
if (m.find()) {
buildNumber = m.group(1);
}
} catch (IOException e) {
continue;
}
}
return timestamp != null && buildNumber != null ? timestamp + "-" + buildNumber : version;
}
}
|