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

com.lewisd.maven.lint.rules.basic.VersionPropertiesMustUseDotVersionRule Maven / Gradle / Ivy

Go to download

Generates a report of suspicious/inconsistent POM elements, and optionally fails the build if violations are found.

The newest version!
package com.lewisd.maven.lint.rules.basic;

import java.util.Map;
import java.util.Set;

import org.apache.maven.model.InputLocation;
import org.apache.maven.project.MavenProject;
import org.springframework.beans.factory.annotation.Autowired;

import com.lewisd.maven.lint.ResultCollector;
import com.lewisd.maven.lint.model.VersionProperty;
import com.lewisd.maven.lint.rules.AbstractRule;
import com.lewisd.maven.lint.util.ExpressionEvaluator;
import com.lewisd.maven.lint.util.ModelUtil;

public class VersionPropertiesMustUseDotVersionRule extends AbstractRule {

    @Autowired
    public VersionPropertiesMustUseDotVersionRule(final ExpressionEvaluator expressionEvaluator, final ModelUtil modelUtil) {
        super(expressionEvaluator, modelUtil);
    }

    @Override
    protected void addRequiredModels(final Set requiredModels) {
        requiredModels.add(VERSION_PROPERTIES);
    }

    @Override
    public String getIdentifier() {
        return "DotVersionProperty";
    }

    @Override
    public String getDescription() {
        return "The convention is to specify properties used to hold versions as some.library.version or some-library.version, " +
               "but never some-library-version or some.library-version.";
    }

    @Override
    public void invoke(final MavenProject mavenProject, final Map models, final ResultCollector resultCollector) {
        @SuppressWarnings("unchecked")
        final Map versionPropertyByObject = (Map) models.get(VERSION_PROPERTIES);

        for (final Map.Entry entry : versionPropertyByObject.entrySet()) {
            final VersionProperty versionProperty = entry.getValue();
            for (final String propertyName : versionProperty.getPropertyNames()) {
                if (isVersionProperty(propertyName) && !isAcceptableVersionPropertyName(propertyName)) {
                    final InputLocation location = modelUtil.getLocation(entry.getKey(), "version");
                    resultCollector.addViolation(mavenProject, this, "Version property names must use '.version', not '-version': '" + propertyName + "'",
                                                 location);
                }
            }
        }
    }

    protected boolean isVersionProperty(final String propertyName) {
        return propertyName.toLowerCase().endsWith("version");
    }

    protected boolean isAcceptableVersionPropertyName(final String propertyName) {
        return propertyName.endsWith(".version") || propertyName.equals("version");
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy