001package org.apache.maven.plugins.enforcer; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; 023import org.apache.maven.artifact.versioning.VersionRange; 024import org.apache.maven.enforcer.rule.api.EnforcerRuleException; 025import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper; 026import org.apache.maven.model.Prerequisites; 027import org.apache.maven.project.MavenProject; 028import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; 029 030/** 031 * 032 * @author Robert Scholte 033 * @since 1.3 034 */ 035public class RequirePrerequisite extends AbstractNonCacheableEnforcerRule 036{ 037 /** 038 * Can either be version or a range, e.g. {@code 2.2.1} or {@code [2.2.1,)} 039 */ 040 private String mavenVersion; 041 042 /** 043 * Set the mavenVersion 044 * 045 * Can either be version or a range, e.g. {@code 2.2.1} or {@code [2.2.1,)} 046 * 047 * @param mavenVersion the version or {@code null} 048 */ 049 public void setMavenVersion( String mavenVersion ) 050 { 051 this.mavenVersion = mavenVersion; 052 } 053 054 /** 055 * {@inheritDoc} 056 */ 057 public void execute( EnforcerRuleHelper helper ) 058 throws EnforcerRuleException 059 { 060 try 061 { 062 MavenProject project = (MavenProject) helper.evaluate( "${project}" ); 063 064 Prerequisites prerequisites = project.getPrerequisites(); 065 066 if( prerequisites == null ) 067 { 068 throw new EnforcerRuleException( "Requires prerequisite not set" ); 069 } 070 071 if( mavenVersion != null ) 072 { 073 074 VersionRange requiredVersionRange = VersionRange.createFromVersionSpec( mavenVersion ); 075 076 if( !requiredVersionRange.hasRestrictions() ) 077 { 078 requiredVersionRange = VersionRange.createFromVersionSpec( "[" + mavenVersion + ",)" ); 079 } 080 081 VersionRange specifiedVersion = VersionRange.createFromVersionSpec( prerequisites.getMaven() ); 082 083 VersionRange restrictedVersionRange = requiredVersionRange.restrict( specifiedVersion ); 084 085 if ( restrictedVersionRange.getRecommendedVersion() == null ) 086 { 087 throw new EnforcerRuleException( "The specified Maven prerequisite( " + specifiedVersion + " ) doesn't match the required version: " + mavenVersion ); 088 } 089 } 090 } 091 catch ( ExpressionEvaluationException e ) 092 { 093 throw new EnforcerRuleException( e.getMessage(), e ); 094 } 095 catch ( InvalidVersionSpecificationException e ) 096 { 097 throw new EnforcerRuleException( e.getMessage(), e ); 098 } 099 } 100 101}