Add properties for raw repo, docker reg
[oparent.git] / version-check-maven-plugin / src / main / java / org / onap / oparent / versioncheck / VersionCheckMojo.java
1 /*
2  * Copyright 2017 Huawei Technologies, Ltd. and others.
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 an "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.onap.oparent.versioncheck;
18
19 import java.io.IOException;
20 import java.io.InputStreamReader;
21 import java.net.MalformedURLException;
22 import java.net.URI;
23 import java.nio.charset.StandardCharsets;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.Map.Entry;
27 import java.util.Set;
28 import java.util.TreeSet;
29
30 import org.apache.commons.csv.CSVFormat;
31 import org.apache.commons.csv.CSVRecord;
32 import org.apache.maven.model.Dependency;
33 import org.apache.maven.plugin.AbstractMojo;
34 import org.apache.maven.plugin.MojoExecutionException;
35 import org.apache.maven.plugin.logging.Log;
36 import org.apache.maven.plugins.annotations.LifecyclePhase;
37 import org.apache.maven.plugins.annotations.Mojo;
38 import org.apache.maven.plugins.annotations.Parameter;
39 import org.apache.maven.project.MavenProject;
40
41 @Mojo(name = "version-check", defaultPhase = LifecyclePhase.PROCESS_SOURCES)
42 public class VersionCheckMojo extends AbstractMojo {
43
44     /**
45      * The Maven Project.
46      *
47      * @since 1.0-alpha-1
48      */
49     @Parameter(defaultValue = "${project}", required = true, readonly = true)
50     protected MavenProject project;
51
52     /**
53      * Location of the file.
54      */
55     @Parameter(property = "manifestUri", required = true)
56     private URI manifestUri;
57
58     public void execute() throws MojoExecutionException {
59         final Log log = getLog();
60
61         log.info("Checking version manifest " + manifestUri);
62
63         Map<String, String> expectedVersions = new HashMap<>();
64
65         try (InputStreamReader in = new InputStreamReader(manifestUri.toURL().openStream(),
66                 StandardCharsets.ISO_8859_1)) {
67             Iterable<CSVRecord> records = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(in);
68             for (CSVRecord record : records) {
69                 String groupId = record.get("groupId");
70                 String artifactId = record.get("artifactId");
71                 String version = record.get("version");
72                 log.debug("Expected version: " + groupId + ":" + artifactId + ":" + version);
73                 expectedVersions.put(groupId + ":" + artifactId, version);
74             }
75         } catch (MalformedURLException e) {
76             log.error(e);
77             throw new MojoExecutionException(e.getMessage());
78         } catch (IOException e) {
79             log.error(e);
80             throw new MojoExecutionException(e.getMessage());
81         }
82
83         Map<String, String> actualVersions = new HashMap<>();
84         MavenProject parent = project.getParent();
85         if (parent != null) {
86             log.debug("Parent: " + parent);
87             actualVersions.put(parent.getGroupId() + ":" + parent.getArtifactId(), parent.getVersion());
88         } else {
89             log.debug("No parent");
90         }
91
92         for (Dependency dep : project.getDependencies()) {
93             log.debug("Dependency: " + dep.toString());
94             actualVersions.put(dep.getGroupId() + ":" + dep.getArtifactId(), dep.getVersion());
95         }
96
97         Set<String> mismatches = new TreeSet<>();
98         for (Entry<String, String> expected : expectedVersions.entrySet()) {
99             String artifact = expected.getKey();
100             String expectedVersion = expectedVersions.get(artifact);
101             String actualVersion = actualVersions.get(artifact);
102             if (actualVersion != null && !actualVersion.equals(expectedVersion)) {
103                 mismatches.add(artifact);
104             }
105         }
106
107         if (mismatches.isEmpty()) {
108             log.debug("No version mismatches found");
109         } else {
110             log.warn("The following dependencies should be updated to match the version manifest:");
111             for (String artifact : mismatches) {
112                 String expectedVersion = expectedVersions.get(artifact);
113                 String actualVersion = actualVersions.get(artifact);
114                 if (actualVersion != null && !actualVersion.equals(expectedVersion)) {
115                     log.warn("  " + artifact + " " + actualVersion + " -> " + expectedVersion);
116                 }
117             }
118         }
119
120     }
121 }