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.Artifact; 023import org.apache.maven.enforcer.rule.api.EnforcerRuleException; 024import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper; 025import org.apache.maven.project.MavenProject; 026import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; 027 028/** 029 * This rule checks that the current project is not a snapshot. 030 * 031 * @author <a href="mailto:brianf@apache.org">Brian Fox</a> 032 */ 033public class RequireReleaseVersion 034 extends AbstractNonCacheableEnforcerRule 035{ 036 037 /** 038 * Allows this rule to fail when the parent is defined as a snapshot. 039 * 040 * @parameter 041 * 042 * @see {@link #setFailWhenParentIsSnapshot(boolean)} 043 * @see {@link #isFailWhenParentIsSnapshot()} 044 */ 045 private boolean failWhenParentIsSnapshot = true; 046 047 /* 048 * (non-Javadoc) 049 * 050 * @see org.apache.maven.enforcer.rule.api.EnforcerRule#execute(org.apache.maven.enforcer.rule.api.EnforcerRuleHelper) 051 */ 052 public void execute( EnforcerRuleHelper theHelper ) 053 throws EnforcerRuleException 054 { 055 056 MavenProject project = getProject( theHelper ); 057 058 if ( project.getArtifact().isSnapshot() ) 059 { 060 String message = getMessage(); 061 StringBuffer buf = new StringBuffer(); 062 if ( message != null ) 063 { 064 buf.append( message ).append( '\n' ); 065 } 066 buf.append( "This project cannot be a snapshot:" ).append( project.getArtifact().getId() ); 067 throw new EnforcerRuleException( buf.toString() ); 068 } 069 if ( failWhenParentIsSnapshot ) 070 { 071 Artifact parentArtifact = project.getParentArtifact(); 072 if ( parentArtifact != null && parentArtifact.isSnapshot() ) 073 { 074 throw new EnforcerRuleException( "Parent Cannot be a snapshot: " + parentArtifact.getId() ); 075 } 076 } 077 078 } 079 080 /** 081 * @param helper 082 * @return The evaluated {@link MavenProject}. 083 * @throws EnforcerRuleException 084 */ 085 private MavenProject getProject( EnforcerRuleHelper helper ) 086 throws EnforcerRuleException 087 { 088 try 089 { 090 return (MavenProject) helper.evaluate( "${project}" ); 091 } 092 catch ( ExpressionEvaluationException eee ) 093 { 094 throw new EnforcerRuleException( "Unable to retrieve the MavenProject: ", eee ); 095 } 096 } 097 098 public final boolean isFailWhenParentIsSnapshot() 099 { 100 return failWhenParentIsSnapshot; 101 } 102 103 public final void setFailWhenParentIsSnapshot( boolean failWhenParentIsSnapshot ) 104 { 105 this.failWhenParentIsSnapshot = failWhenParentIsSnapshot; 106 } 107}