All Downloads are FREE. Search and download functionalities are using the official Maven repository.

pl.project13.maven.validation.ValidationMojo Maven / Gradle / Ivy

Go to download

This plugin makes basic repository information available through maven resources. This can be used to display "what version is this?" or "who has deployed this and when, from which branch?" information at runtime, making it easy to find things like "oh, that isn't deployed yet, I'll test it tomorrow" and making both testers and developers life easier. See https://github.com/git-commit-id/maven-git-commit-id-plugin

There is a newer version: 4.9.10
Show newest version
/*
 * This file is part of git-commit-id-plugin by Konrad 'ktoso' Malawski 
 *
 * git-commit-id-plugin is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * git-commit-id-plugin is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with git-commit-id-plugin.  If not, see .
 */

package pl.project13.maven.validation;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import pl.project13.core.log.LoggerBridge;
import pl.project13.maven.log.MavenLoggerBridge;

import javax.annotation.Nonnull;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @since 2.2.2
 */
@Mojo(name = "validateRevision", defaultPhase = LifecyclePhase.VERIFY, threadSafe = true)
public class ValidationMojo extends AbstractMojo {

  @Nonnull
  private final LoggerBridge log = new MavenLoggerBridge(this, false);

  @Parameter(defaultValue = "true")
  private boolean validationShouldFailIfNoMatch;

  @Parameter
  private List validationProperties;

  @Override
  public void execute() throws MojoExecutionException {
    if (validationProperties != null && validationShouldFailIfNoMatch) {
      for (ValidationProperty validationProperty: validationProperties) {
        String name = validationProperty.getName();
        String value = validationProperty.getValue();
        String shouldMatchTo = validationProperty.getShouldMatchTo();
        if ((value != null) && (shouldMatchTo != null)) {
          validateIfValueAndShouldMatchToMatches(name, value, shouldMatchTo);
        } else {
          printLogMessageWhenValueOrShouldMatchToIsEmpty(name, value, shouldMatchTo);
        }
      }
    }
  }

  private void validateIfValueAndShouldMatchToMatches(String name, String value, String shouldMatchTo) throws MojoExecutionException {
    Pattern pattern = Pattern.compile(shouldMatchTo);
    Matcher matcher = pattern.matcher(value);
    if (!matcher.find()) {
      String commonLogMessage = "Expected '" + value + "' to match with '" + shouldMatchTo + "'!";
      if (name != null) {
        throw new MojoExecutionException("Validation '" + name + "' failed! " + commonLogMessage);
      } else {
        throw new MojoExecutionException("Validation of an unidentified validation (please set the name property-tag to be able to identify the validation) failed! " + commonLogMessage);
      }
    }
  }

  private void printLogMessageWhenValueOrShouldMatchToIsEmpty(String name, String value, String shouldMatchTo) {
    String commonLogMessage = "since one of the values was null! (value = '" + value + "'; shouldMatchTo = '" + shouldMatchTo + "').";
    if (name != null) {
      log.warn("Skipping validation '" + name + "' " + commonLogMessage);
    } else {
      log.warn("Skipping an unidentified validation (please set the name property-tag to be able to identify the validation) " + commonLogMessage);
    }
  }

  public void setValidationShouldFailIfNoMatch(boolean validationShouldFailIfNoMatch) {
    this.validationShouldFailIfNoMatch = validationShouldFailIfNoMatch;
  }

  public void setValidationProperties(List validationProperties) {
    this.validationProperties = validationProperties;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy