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.integration.versionmanifest;
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;
25 import java.util.Map.Entry;
27 import java.util.TreeSet;
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;
40 @Mojo(name = "version-check", defaultPhase = LifecyclePhase.PROCESS_SOURCES)
41 public class VersionCheckMojo extends AbstractMojo {
48 @Parameter(defaultValue = "${project}", required = true, readonly = true)
49 protected MavenProject project;
52 * Location of the file.
54 @Parameter(property = "manifest", required = true, defaultValue = "/java-manifest.csv")
55 private String manifest;
57 public void execute() throws MojoExecutionException {
58 final Log log = getLog();
60 log.info("Checking version manifest " + manifest);
62 Map<String, String> expectedVersions = new HashMap<>();
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);
74 } catch (MalformedURLException e) {
76 throw new MojoExecutionException(e.getMessage());
77 } catch (IOException e) {
79 throw new MojoExecutionException(e.getMessage());
82 Map<String, String> actualVersions = new HashMap<>();
83 MavenProject parent = project.getParent();
85 log.debug("Parent: " + parent);
86 actualVersions.put(parent.getGroupId() + ":" + parent.getArtifactId(), parent.getVersion());
88 log.debug("No parent");
91 for (Dependency dep : project.getDependencies()) {
92 log.debug("Dependency: " + dep.toString());
93 actualVersions.put(dep.getGroupId() + ":" + dep.getArtifactId(), dep.getVersion());
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);
106 if (mismatches.isEmpty()) {
107 log.debug("No version mismatches found");
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);