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 /** 073 * {@inheritDoc} 074 */ 075 public void execute( EnforcerRuleHelper helper ) 076 throws EnforcerRuleException 077 { 078 try 079 { 080 MavenProject project = (MavenProject) helper.evaluate( "${project}" ); 081 082 if ( "pom".equals( project.getPackaging() ) ) 083 { 084 helper.getLog().debug( "Packaging is pom, skipping requirePrerequisite rule" ); 085 return; 086 } 087 088 if ( packagings != null && !packagings.contains( project.getPackaging() ) ) 089 { 090 // CHECKSTYLE_OFF: LineLength 091 helper.getLog().debug( "Packaging is " + project.getPackaging() + ", skipping requirePrerequisite rule" ); 092 return; 093 // CHECKSTYLE_ON: LineLength 094 } 095 096 Prerequisites prerequisites = project.getPrerequisites(); 097 098 if ( prerequisites == null ) 099 { 100 throw new EnforcerRuleException( "Requires prerequisite not set" ); 101 } 102 103 if ( mavenVersion != null ) 104 { 105 106 VersionRange requiredVersionRange = VersionRange.createFromVersionSpec( mavenVersion ); 107 108 if ( !requiredVersionRange.hasRestrictions() ) 109 { 110 requiredVersionRange = VersionRange.createFromVersionSpec( "[" + mavenVersion + ",)" ); 111 } 112 113 VersionRange specifiedVersion = VersionRange.createFromVersionSpec( prerequisites.getMaven() ); 114 115 VersionRange restrictedVersionRange = requiredVersionRange.restrict( specifiedVersion ); 116 117 if ( restrictedVersionRange.getRecommendedVersion() == null ) 118 { 119 throw new EnforcerRuleException( "The specified Maven prerequisite( " + specifiedVersion 120 + " ) doesn't match the required version: " + mavenVersion ); 121 } 122 } 123 } 124 catch ( ExpressionEvaluationException e ) 125 { 126 throw new EnforcerRuleException( e.getMessage(), e ); 127 } 128 catch ( InvalidVersionSpecificationException e ) 129 { 130 throw new EnforcerRuleException( e.getMessage(), e ); 131 } 132 } 133 134}