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