2 * Copyright 2017 Huawei Technologies, Ltd. and others.
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 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.
17 package org.onap.oparent.versioncheck;
19 import java.io.IOException;
20 import java.io.InputStreamReader;
21 import java.net.MalformedURLException;
23 import java.nio.charset.StandardCharsets;
24 import java.util.HashMap;
26 import java.util.Map.Entry;
28 import java.util.TreeSet;
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;
41 @Mojo(name = "version-check", defaultPhase = LifecyclePhase.PROCESS_SOURCES)
42 public class VersionCheckMojo extends AbstractMojo {
49 @Parameter(defaultValue = "${project}", required = true, readonly = true)
50 protected MavenProject project;
53 * Location of the file.
55 @Parameter(property = "manifestUri", required = true)
56 private URI manifestUri;
58 public void execute() throws MojoExecutionException {
59 final Log log = getLog();
61 log.info("Checking version manifest " + manifestUri);
63 Map<String, String> expectedVersions = new HashMap<>();
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);
75 } catch (MalformedURLException e) {
77 throw new MojoExecutionException(e.getMessage());
78 } catch (IOException e) {
80 throw new MojoExecutionException(e.getMessage());
83 Map<String, String> actualVersions = new HashMap<>();
84 MavenProject parent = project.getParent();
86 log.debug("Parent: " + parent);
87 actualVersions.put(parent.getGroupId() + ":" + parent.getArtifactId(), parent.getVersion());
89 log.debug("No parent");
92 for (Dependency dep : project.getDependencies()) {
93 log.debug("Dependency: " + dep.toString());
94 actualVersions.put(dep.getGroupId() + ":" + dep.getArtifactId(), dep.getVersion());
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);
107 if (mismatches.isEmpty()) {
108 log.debug("No version mismatches found");
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);