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 release. 030 */ 031public class RequireSnapshotVersion 032 extends AbstractNonCacheableEnforcerRule 033{ 034 035 /** 036 * Allows this rule to fail when the parent is defined as a release. 037 */ 038 private boolean failWhenParentIsRelease = true; 039 040 @Override 041 public void execute( EnforcerRuleHelper helper ) 042 throws EnforcerRuleException 043 { 044 045 MavenProject project = getProject( helper ); 046 Artifact artifact = project.getArtifact(); 047 048 if ( !artifact.isSnapshot() ) 049 { 050 String message = getMessage(); 051 StringBuilder sb = new StringBuilder(); 052 if ( message != null ) 053 { 054 sb.append( message ).append( '\n' ); 055 } 056 sb.append( "This project cannot be a release:" ).append( artifact.getId() ); 057 throw new EnforcerRuleException( sb.toString() ); 058 } 059 if ( failWhenParentIsRelease ) 060 { 061 Artifact parentArtifact = project.getParentArtifact(); 062 if ( parentArtifact != null && !parentArtifact.isSnapshot() ) 063 { 064 throw new EnforcerRuleException( "Parent cannot be a release: " + parentArtifact.getId() ); 065 } 066 } 067 068 } 069 070 private MavenProject getProject( EnforcerRuleHelper helper ) 071 throws EnforcerRuleException 072 { 073 try 074 { 075 return (MavenProject) helper.evaluate( "${project}" ); 076 } 077 catch ( ExpressionEvaluationException eee ) 078 { 079 throw new EnforcerRuleException( "Unable to retrieve the MavenProject: ", eee ); 080 } 081 } 082 083 public boolean isFailWhenParentIsRelease() 084 { 085 return failWhenParentIsRelease; 086 } 087 088 public void setFailWhenParentIsRelease( boolean failWhenParentIsRelease ) 089 { 090 this.failWhenParentIsRelease = failWhenParentIsRelease; 091 } 092 093}