1 package org.apache.maven.plugin.invoker; 2 3 /* 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 import java.util.ArrayList; 23 import java.util.Arrays; 24 import java.util.Properties; 25 26 import org.apache.maven.shared.invoker.InvocationRequest; 27 import org.codehaus.plexus.util.StringUtils; 28 29 /** 30 * Provides a convenient facade around the <code>invoker.properties</code>. 31 * 32 * @author Benjamin Bentmann 33 * @version $Id: InvokerProperties.java 688545 2008-08-24 18:38:59Z bentmann $ 34 */ 35 class InvokerProperties 36 { 37 38 /** 39 * The invoker properties being wrapped, never <code>null</code>. 40 */ 41 private final Properties properties; 42 43 /** 44 * Creates a new facade for the specified invoker properties. The properties will not be copied, so any changes to 45 * them will be reflected by the facade. 46 * 47 * @param properties The invoker properties to wrap, may be <code>null</code> if none. 48 */ 49 public InvokerProperties( Properties properties ) 50 { 51 this.properties = ( properties != null ) ? properties : new Properties(); 52 } 53 54 /** 55 * Gets the invoker properties being wrapped. 56 * 57 * @return The invoker properties being wrapped, never <code>null</code>. 58 */ 59 public Properties getProperties() 60 { 61 return this.properties; 62 } 63 64 /** 65 * Determines whether these invoker properties contain a build definition for the specified invocation index. 66 * 67 * @param index The one-based index of the invocation to check for, must not be negative. 68 * @return <code>true</code> if the invocation with the specified index is defined, <code>false</code> otherwise. 69 */ 70 public boolean isInvocationDefined( int index ) 71 { 72 return properties.getProperty( "invoker.goals." + index ) != null; 73 } 74 75 /** 76 * Configures the specified invocation request from these invoker properties. Settings not present in the invoker 77 * properties will be left unchanged in the invocation request. 78 * 79 * @param request The invocation request to configure, must not be <code>null</code>. 80 * @param index The one-based index of the invocation to configure, must not be negative. 81 */ 82 public void configureInvocation( InvocationRequest request, int index ) 83 { 84 String goals = get( "invoker.goals", index ); 85 if ( goals != null ) 86 { 87 request.setGoals( new ArrayList( Arrays.asList( StringUtils.split( goals, ", \t\n\r\f" ) ) ) ); 88 } 89 90 String profiles = get( "invoker.profiles", index ); 91 if ( profiles != null ) 92 { 93 request.setProfiles( new ArrayList( Arrays.asList( StringUtils.split( profiles, ", \t\n\r\f" ) ) ) ); 94 } 95 96 String mvnOpts = get( "invoker.mavenOpts", index ); 97 if ( mvnOpts != null ) 98 { 99 request.setMavenOpts( mvnOpts ); 100 } 101 102 String failureBehavior = get( "invoker.failureBehavior", index ); 103 if ( failureBehavior != null ) 104 { 105 request.setFailureBehavior( failureBehavior ); 106 } 107 108 String nonRecursive = get( "invoker.nonRecursive", index ); 109 if ( nonRecursive != null ) 110 { 111 request.setRecursive( !Boolean.valueOf( nonRecursive ).booleanValue() ); 112 } 113 } 114 115 /** 116 * Checks whether the specified exit code matches the one expected for the given invocation. 117 * 118 * @param exitCode The exit code of the Maven invocation to check. 119 * @param index The index of the invocation for which to check the exit code, must not be negative. 120 * @return <code>true</code> if the exit code is zero and a success was expected or if the exit code is non-zero and 121 * a failue was expected, <code>false</code> otherwise. 122 */ 123 public boolean isExpectedResult( int exitCode, int index ) 124 { 125 boolean nonZeroExit = "failure".equalsIgnoreCase( get( "invoker.buildResult", index ) ); 126 return ( exitCode != 0 ) == nonZeroExit; 127 } 128 129 /** 130 * Gets a value from the invoker properties. The invoker properties are intended to describe the invocation settings 131 * for multiple builds of the same project. For this reason, the properties are indexed. First, a property named 132 * <code>key.index</code> will be queried. If this property does not exist, the value of the property named 133 * <code>key</code> will finally be returned. 134 * 135 * @param key The (base) key for the invoker property to lookup, must not be <code>null</code>. 136 * @param index The index of the invocation for which to retrieve the value, must not be negative. 137 * @return The value for the requested invoker property or <code>null</code> if not defined. 138 */ 139 String get( String key, int index ) 140 { 141 if ( index < 0 ) 142 { 143 throw new IllegalArgumentException( "invalid invocation index: " + index ); 144 } 145 146 String value = properties.getProperty( key + '.' + index ); 147 if ( value == null ) 148 { 149 value = properties.getProperty( key ); 150 } 151 return value; 152 } 153 154 }