Question

I am trying to write a custom rule for Maven enforcer plugin which is very similar to the RequireSameVersions rule that is included with the enforcer plugin.

I want to look at all dependencies of a project, and if they have a custom property set, then we must ensure that the property is the same across all dependencies (whilst ignoring any dependencies without that property set, as these are version agnostic).

The problem I am facing is that in the code for the RequireSameVersions rule, the artifacts have the version exposed in the API, such that you can call artifact.getVersion() for each dependent artifact, however the only Object that it seems you can call getProperties() on is the maven project itself.

Thus, the code I would really like to write for the custom rule is:

    public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException {

        Set<String> versions = new HashSet<String>();

        MavenProject project = null;
        try {
            project = (MavenProject) helper.evaluate("${project}");
            Set<Artifact> dependentArtifacts = project.getDependencyArtifacts();
            for (Artifact artifact : dependentArtifacts) {
                Properties childProperties = getProperties(artifact); //TODO: what to put in this method
                String versionNumber = childProperties.getProperty("bespoke.version");
                if (versionNumber != null) {
                    versions.add(versionNumber);
                }
            }
        } catch (ExpressionEvaluationException eee) {
            throw new EnforcerRuleException(
                    "Unable to retrieve the MavenProject: ", eee);
        }

        if (versions.size() > 1) {
            // we have more than one version type across child projects, so we
            // fail the build.
            throw new EnforcerRuleException(
                    "modules of this project refer to more than one version");
        }
    }    

However, I don't know what to put in the getProperties(artifact) method. Any suggestions here would be fantastic, thank you.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top