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 java.util.List; 023 024import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; 025import org.apache.maven.artifact.versioning.VersionRange; 026import org.apache.maven.enforcer.rule.api.EnforcerRuleException; 027import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper; 028import org.apache.maven.model.Prerequisites; 029import org.apache.maven.project.MavenProject; 030import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; 031 032/** 033 * @author Robert Scholte 034 * @since 1.3 035 */ 036public class RequirePrerequisite 037 extends AbstractNonCacheableEnforcerRule 038{ 039 /** 040 * Only the projects with one of these packagings will be enforced to have the correct prerequisite. 041 * 042 * @since 1.4 043 */ 044 private List<String> packagings; 045 046 /** 047 * Can either be version or a range, e.g. {@code 2.2.1} or {@code [2.2.1,)} 048 */ 049 private String mavenVersion; 050 051 /** 052 * Set the mavenVersion Can either be version or a range, e.g. {@code 2.2.1} or {@code [2.2.1,)} 053 * 054 * @param mavenVersion the version or {@code null} 055 */ 056 public void setMavenVersion( String mavenVersion ) 057 { 058 this.mavenVersion = mavenVersion; 059 } 060 061 /** 062 * Only the projects with one of these packagings will be enforced to have the correct prerequisite. 063 * 064 * @since 1.4 065 * @param packagings the list of packagings 066 */ 067 public void setPackagings( List<String> packagings ) 068 { 069 this.packagings = packagings; 070 } 071 072 @Override 073 public void execute( EnforcerRuleHelper helper ) 074 throws EnforcerRuleException 075 { 076 try 077 { 078 MavenProject project = (MavenProject) helper.evaluate( "${project}" ); 079 080 if ( "pom".equals( project.getPackaging() ) ) 081 { 082 helper.getLog().debug( "Packaging is pom, skipping requirePrerequisite rule" ); 083 return; 084 } 085 086 if ( packagings != null && !packagings.contains( project.getPackaging() ) ) 087 { 088 // CHECKSTYLE_OFF: LineLength 089 helper.getLog().debug( "Packaging is " + project.getPackaging() + ", skipping requirePrerequisite rule" ); 090 return; 091 // CHECKSTYLE_ON: LineLength 092 } 093 094 Prerequisites prerequisites = project.getPrerequisites(); 095 096 if ( prerequisites == null ) 097 { 098 throw new EnforcerRuleException( "Requires prerequisite not set" ); 099 } 100 101 if ( mavenVersion != null ) 102 { 103 104 VersionRange requiredVersionRange = VersionRange.createFromVersionSpec( mavenVersion ); 105 106 if ( !requiredVersionRange.hasRestrictions() ) 107 { 108 requiredVersionRange = VersionRange.createFromVersionSpec( "[" + mavenVersion + ",)" ); 109 } 110 111 VersionRange specifiedVersion = VersionRange.createFromVersionSpec( prerequisites.getMaven() ); 112 113 VersionRange restrictedVersionRange = requiredVersionRange.restrict( specifiedVersion ); 114 115 if ( restrictedVersionRange.getRecommendedVersion() == null ) 116 { 117 throw new EnforcerRuleException( "The specified Maven prerequisite( " + specifiedVersion 118 + " ) doesn't match the required version: " + mavenVersion ); 119 } 120 } 121 } 122 catch ( ExpressionEvaluationException e ) 123 { 124 throw new EnforcerRuleException( e.getMessage(), e ); 125 } 126 catch ( InvalidVersionSpecificationException e ) 127 { 128 throw new EnforcerRuleException( e.getMessage(), e ); 129 } 130 } 131 132}