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;
22 import java.util.List;
23 import java.util.regex.Matcher;
24 import java.util.regex.Pattern;
25 import org.apache.maven.artifact.repository.ArtifactRepository;
26 import org.apache.maven.execution.MavenSession;
27 import org.apache.maven.plugin.AbstractMojo;
28 import org.apache.maven.plugin.MojoExecutionException;
29 import org.apache.maven.plugin.MojoFailureException;
30 import org.apache.maven.plugins.annotations.LifecyclePhase;
31 import org.apache.maven.plugins.annotations.Mojo;
32 import org.apache.maven.plugins.annotations.Parameter;
33 import org.apache.maven.plugins.annotations.ResolutionScope;
34 import org.apache.maven.project.MavenProject;
35 import org.apache.maven.settings.Proxy;
37 @Mojo(name = "init-artifact-helper", threadSafe = true, defaultPhase = LifecyclePhase.PRE_CLEAN,
38 requiresDependencyResolution = ResolutionScope.NONE)
39 public class InitializationHelperMojo extends AbstractMojo {
41 private static final String SKIP_GET = "skipGet";
42 private static final String HTTP = "http";
43 private static final String HTTPS = "https";
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 String excludePackaging;
62 private ArtifactHelper artifactHelper;
65 public void execute() throws MojoExecutionException, MojoFailureException {
66 if (System.getProperties().containsKey(SKIP_GET)) {
67 project.getProperties()
68 .setProperty(SKIP_GET, Boolean.toString(System.getProperties().containsKey(SKIP_GET)));
71 File orgFile = new File(
72 session.getLocalRepository().getBasedir() + File.separator + (groupId.replace(".", File.separator))
73 + File.separator + artifactId + File.separator + version);
74 String resolvedVersion = getResolvedVersion(artifactHelper.getRepositories(version.contains("SNAPSHOT")));
75 project.getProperties().setProperty("resolvedVersion", resolvedVersion);
76 System.getProperties().setProperty(SKIP_GET, Boolean.TRUE.toString());
77 if (resolvedVersion.equals(version) && !orgFile.exists()) {
78 project.getProperties().setProperty(SKIP_GET, Boolean.TRUE.toString());
83 private String getResolvedVersion(List<ArtifactRepository> list) {
84 Pattern timestampPattern = Pattern.compile(".*<timestamp>(.*)</timestamp>.*");
85 Pattern buildNumberPattern = Pattern.compile(".*<buildNumber>(.*)</buildNumber>.*");
86 String timestamp = null;
87 String buildNumber = null;
88 for (ArtifactRepository repo : list) {
90 URL url = new URL(repo.getUrl() + (groupId.replace('.', '/')) + '/' + artifactId + '/' + version
91 + "/maven-metadata.xml");
93 String content = artifactHelper.getContents(url);
94 Matcher m = timestampPattern.matcher(content);
96 timestamp = m.group(1);
98 m = buildNumberPattern.matcher(content);
100 buildNumber = m.group(1);
102 } catch (IOException e) {
105 if (timestamp != null && buildNumber != null) {
106 return timestamp + "-" + buildNumber;
112 private void setProxy(URL url) {
113 if (url.getProtocol().equalsIgnoreCase(HTTP)) {
114 setProperties("http.proxyHost", "http.proxyPort", "http.nonProxyHosts", HTTP);
115 } else if (url.getProtocol().equalsIgnoreCase(HTTPS)) {
116 setProperties("https.proxyHost", "https.proxyPort", "https.nonProxyHosts", HTTPS);
120 private void setProperties(String proxyHostProperty, String proxyPortProperty, String nonProxyHostsProperty,
122 for (Proxy proxy : session.getSettings().getProxies()) {
123 if (proxy.isActive() && proxy.getProtocol().equalsIgnoreCase(protocol)) {
124 System.setProperty(proxyHostProperty, proxy.getHost());
125 System.setProperty(proxyPortProperty, String.valueOf(proxy.getPort()));
126 System.setProperty(nonProxyHostsProperty, proxy.getNonProxyHosts());