From be096d395b595fcb7ef50204978904a538ed32a0 Mon Sep 17 00:00:00 2001 From: Gary Wu Date: Tue, 29 Aug 2017 14:21:06 -0700 Subject: Maven plugin to check dependency versions Initial draft of a custom maven plugin to check dependency versions against the version manifest. Change-Id: If009461f6ecbcd86e5a43004a1c5a421ec02ccca Issue-id: INT-124 Signed-off-by: Gary Wu --- oparent/pom.xml | 517 +++++++++++---------- pom.xml | 1 + version-check-maven-plugin/pom.xml | 74 +++ .../oparent/versioncheck/VersionCheckMojo.java | 121 +++++ 4 files changed, 465 insertions(+), 248 deletions(-) create mode 100644 version-check-maven-plugin/pom.xml create mode 100644 version-check-maven-plugin/src/main/java/org/onap/oparent/versioncheck/VersionCheckMojo.java diff --git a/oparent/pom.xml b/oparent/pom.xml index 9cde98f..09c1d35 100644 --- a/oparent/pom.xml +++ b/oparent/pom.xml @@ -42,244 +42,244 @@ **/gen/**,**/generated-sources/**,**/yang-gen**,**/pax/** ${basedir}/src/main/resources/swagger.properties - ${basedir}/src/main/resources/swagger.json - + + + org.onap.oparent + version-check-maven-plugin + 1.0.0-SNAPSHOT + + - - generate-json - - - ${swagger-properties} - - - swagger-sdk.generate-json - - - - - - org.codehaus.mojo - properties-maven-plugin - 1.0.0 - - - initialize - - read-project-properties - - - - ${basedir}/src/main/resources/swagger.properties - - - - - - - com.github.kongchen - swagger-maven-plugin - 3.1.4 - - - - ${api-rest-package} - http,https - ${api-host-ip}:${api-host-port} - ${api-base-path} - - ${api-title} - ${api-version} - ${api-description} - - ${api-license} - - - ${basedir}/src/main/resources - - - - - - compile - - generate - - - - - - org.apache.maven.plugins - maven-install-plugin - 2.3.1 - - - install-file-id - install - - install-file - - - ${basedir}/src/main/resources/swagger.json - ${project.groupId} - ${project.artifactId}-swagger-schema - ${project.version} - json - - - - - - - - - generate-sdk - - - ${swagger-json} - - - swagger-sdk.generate-java-sdk - - - - - - org.apache.maven.plugins - maven-antrun-plugin - 1.8 - - - initialize - ant-create-script - - true - - - - - - - - - - - - - - - - - - - - - - - - - - - run - - - - - - ant-contrib - ant-contrib - 1.0b3 - - - ant - ant - - - - - - - io.swagger - swagger-codegen-maven-plugin - 2.2.1 - - - - generate - - - ${basedir}/src/main/resources/swagger.json - ${project.build.directory}/generated-sources - java - - joda - - jersey2 - ${project.groupId} - ${project.artifactId}-java-sdk - ${project.version} - ${project.groupId}.${project.artifactId}.client.model - ${project.groupId}.${project.artifactId}.client.api - ${project.groupId}.${project.artifactId}.client.invoker - - - - - - exec-maven-plugin - org.codehaus.mojo - 1.5.0 - - - swagger-generate-sources - generate-sources - - exec - - - ${project.build.directory}${file.separator}${swagger.sdk.script.file} - - - - - - org.apache.maven.plugins - maven-clean-plugin - 3.0.0 - - - clean-generated-files - generate-sources - - clean - - - - - ${project.build.directory}/generated-sources - - - - - - - - + + generate-json + + + ${swagger-properties} + + + swagger-sdk.generate-json + + + + + + org.codehaus.mojo + properties-maven-plugin + 1.0.0 + + + initialize + + read-project-properties + + + + ${basedir}/src/main/resources/swagger.properties + + + + + + + com.github.kongchen + swagger-maven-plugin + 3.1.4 + + + + ${api-rest-package} + http,https + ${api-host-ip}:${api-host-port} + ${api-base-path} + + ${api-title} + ${api-version} + ${api-description} + + ${api-license} + + + ${basedir}/src/main/resources + + + + + + compile + + generate + + + + + + org.apache.maven.plugins + maven-install-plugin + 2.3.1 + + + install-file-id + install + + install-file + + + ${basedir}/src/main/resources/swagger.json + ${project.groupId} + ${project.artifactId}-swagger-schema + ${project.version} + json + + + + + + + + + generate-sdk + + + ${swagger-json} + + + swagger-sdk.generate-java-sdk + + + + + + org.apache.maven.plugins + maven-antrun-plugin + 1.8 + + + initialize + ant-create-script + + true + + + + + + + + + + + + + + + + + + + + + + + + + + run + + + - - org.onap.msb.swagger-sdk - swagger-sdk - 1.0.0-SNAPSHOT - + + ant-contrib + ant-contrib + 1.0b3 + + + ant + ant + + + - - + + + io.swagger + swagger-codegen-maven-plugin + 2.2.1 + + + + generate + + + ${basedir}/src/main/resources/swagger.json + ${project.build.directory}/generated-sources + java + + joda + + jersey2 + ${project.groupId} + ${project.artifactId}-java-sdk + ${project.version} + ${project.groupId}.${project.artifactId}.client.model + ${project.groupId}.${project.artifactId}.client.api + ${project.groupId}.${project.artifactId}.client.invoker + + + + + + exec-maven-plugin + org.codehaus.mojo + 1.5.0 + + + swagger-generate-sources + generate-sources + + exec + + + ${project.build.directory}${file.separator}${swagger.sdk.script.file} + + + + + + org.apache.maven.plugins + maven-clean-plugin + 3.0.0 + + + clean-generated-files + generate-sources + + clean + + + + + ${project.build.directory}/generated-sources + + + + + + + + + + + org.onap.msb.swagger-sdk + swagger-sdk + 1.0.0-SNAPSHOT + + + + @@ -320,19 +320,19 @@ process-sources - - onap-checkstyle/onap-java-style.xml - - ${project.build.sourceDirectory}/src/main/java - true - true - true - + onap-checkstyle/onap-java-style.xml + + ${project.build.sourceDirectory}/src/main/java + true + true + true + - false - true + false + true @@ -458,12 +458,29 @@ - - - org.sonarsource.scanner.maven - sonar-maven-plugin - 3.3.0.603 - + + + org.sonarsource.scanner.maven + sonar-maven-plugin + 3.3.0.603 + + + org.onap.oparent + version-check-maven-plugin + 1.0.0-SNAPSHOT + + + version-check + compile + + version-check + + + https://git.onap.org/oparent/plain/versions/src/main/resources/onap-versions/onap-version-manifest.csv + + + + @@ -488,6 +505,10 @@ org.apache.maven.plugins maven-failsafe-plugin + + org.onap.oparent + version-check-maven-plugin + diff --git a/pom.xml b/pom.xml index 7ac034e..1bd4a4f 100644 --- a/pom.xml +++ b/pom.xml @@ -27,6 +27,7 @@ checkstyle license oparent + version-check-maven-plugin 1.8 diff --git a/version-check-maven-plugin/pom.xml b/version-check-maven-plugin/pom.xml new file mode 100644 index 0000000..57f905b --- /dev/null +++ b/version-check-maven-plugin/pom.xml @@ -0,0 +1,74 @@ + + + 4.0.0 + + org.onap.oparent + version + 1.0.0-SNAPSHOT + .. + + version-check-maven-plugin + maven-plugin + version-check-maven-plugin Maven Plugin + https://www.onap.org + + UTF-8 + 3.2.3 + + + + org.apache.maven + maven-plugin-api + 2.0 + + + org.apache.maven.plugin-tools + maven-plugin-annotations + 3.2 + provided + + + org.codehaus.plexus + plexus-utils + 3.0.8 + + + org.apache.maven + maven-core + ${mavenVersion} + + + + org.apache.commons + commons-csv + 1.4 + + + + + + org.apache.maven.plugins + maven-plugin-plugin + 3.2 + + version-check + true + + + + mojo-descriptor + + descriptor + + + + help-goal + + helpmojo + + + + + + + diff --git a/version-check-maven-plugin/src/main/java/org/onap/oparent/versioncheck/VersionCheckMojo.java b/version-check-maven-plugin/src/main/java/org/onap/oparent/versioncheck/VersionCheckMojo.java new file mode 100644 index 0000000..0bb94f2 --- /dev/null +++ b/version-check-maven-plugin/src/main/java/org/onap/oparent/versioncheck/VersionCheckMojo.java @@ -0,0 +1,121 @@ +/* + * Copyright 2017 Huawei Technologies, Ltd. and others. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.oparent.versioncheck; + +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.MalformedURLException; +import java.net.URI; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.TreeSet; + +import org.apache.commons.csv.CSVFormat; +import org.apache.commons.csv.CSVRecord; +import org.apache.maven.model.Dependency; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.logging.Log; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.project.MavenProject; + +@Mojo(name = "version-check", defaultPhase = LifecyclePhase.PROCESS_SOURCES) +public class VersionCheckMojo extends AbstractMojo { + + /** + * The Maven Project. + * + * @since 1.0-alpha-1 + */ + @Parameter(defaultValue = "${project}", required = true, readonly = true) + protected MavenProject project; + + /** + * Location of the file. + */ + @Parameter(property = "manifestUri", required = true) + private URI manifestUri; + + public void execute() throws MojoExecutionException { + final Log log = getLog(); + + log.info("Checking version manifest " + manifestUri); + + Map expectedVersions = new HashMap<>(); + + try (InputStreamReader in = new InputStreamReader(manifestUri.toURL().openStream(), + StandardCharsets.ISO_8859_1)) { + Iterable records = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(in); + for (CSVRecord record : records) { + String groupId = record.get("groupId"); + String artifactId = record.get("artifactId"); + String version = record.get("version"); + log.debug("Expected version: " + groupId + ":" + artifactId + ":" + version); + expectedVersions.put(groupId + ":" + artifactId, version); + } + } catch (MalformedURLException e) { + log.error(e); + throw new MojoExecutionException(e.getMessage()); + } catch (IOException e) { + log.error(e); + throw new MojoExecutionException(e.getMessage()); + } + + Map actualVersions = new HashMap<>(); + MavenProject parent = project.getParent(); + if (parent != null) { + log.debug("Parent: " + parent); + actualVersions.put(parent.getGroupId() + ":" + parent.getArtifactId(), parent.getVersion()); + } else { + log.debug("No parent"); + } + + for (Dependency dep : project.getDependencies()) { + log.debug("Dependency: " + dep.toString()); + actualVersions.put(dep.getGroupId() + ":" + dep.getArtifactId(), dep.getVersion()); + } + + Set mismatches = new TreeSet<>(); + for (Entry expected : expectedVersions.entrySet()) { + String artifact = expected.getKey(); + String expectedVersion = expectedVersions.get(artifact); + String actualVersion = actualVersions.get(artifact); + if (actualVersion != null && !actualVersion.equals(expectedVersion)) { + mismatches.add(artifact); + } + } + + if (mismatches.isEmpty()) { + log.debug("No version mismatches found"); + } else { + log.warn("The following dependencies should be updated to match the version manifest:"); + for (String artifact : mismatches) { + String expectedVersion = expectedVersions.get(artifact); + String actualVersion = actualVersions.get(artifact); + if (actualVersion != null && !actualVersion.equals(expectedVersion)) { + log.warn(" " + artifact + " " + actualVersion + " -> " + expectedVersion); + } + } + } + + } +} -- cgit 1.2.3-korg