1 package org.apache.maven.artifact.ant.util; 2 3 import java.util.Enumeration; 4 import java.util.Hashtable; 5 6 import org.apache.tools.ant.Project; 7 8 /* 9 * Licensed to the Apache Software Foundation (ASF) under one 10 * or more contributor license agreements. See the NOTICE file 11 * distributed with this work for additional information 12 * regarding copyright ownership. The ASF licenses this file 13 * to you under the Apache License, Version 2.0 (the 14 * "License"); you may not use this file except in compliance 15 * with the License. You may obtain a copy of the License at 16 * 17 * http://www.apache.org/licenses/LICENSE-2.0 18 * 19 * Unless required by applicable law or agreed to in writing, 20 * software distributed under the License is distributed on an 21 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 22 * KIND, either express or implied. See the License for the 23 * specific language governing permissions and limitations 24 * under the License. 25 */ 26 27 /** 28 * Utility stuff for dealing with Ant. 29 */ 30 public class AntUtil 31 { 32 33 /** 34 * Copies all properties from the given project to the new project - omitting those that have already been set in the 35 * new project as well as properties named basedir or ant.file. 36 * 37 * @param fromProject copy from 38 * @param toProject copy to 39 */ 40 public static void copyProperties( Project fromProject, Project toProject ) 41 { 42 copyProperties( fromProject.getProperties(), toProject ); 43 } 44 45 /** 46 * Copies all properties from the given table to the new project - omitting those that have already been set in the 47 * new project as well as properties named basedir or ant.file. 48 * 49 * @param props properties <code>Hashtable</code> to copy to the new project. 50 * @param project the project where the properties are added 51 */ 52 public static void copyProperties( Hashtable props, Project project ) 53 { 54 Enumeration e = props.keys(); 55 while ( e.hasMoreElements() ) 56 { 57 String key = e.nextElement().toString(); 58 if ( "basedir".equals( key ) || "ant.file".equals( key ) ) 59 { 60 // basedir and ant.file get special treatment in execute() 61 continue; 62 } 63 64 String value = props.get( key ).toString(); 65 // don't re-set user properties, avoid the warning message 66 if ( project.getProperty( key ) == null ) 67 { 68 // no user property 69 project.setNewProperty( key, value ); 70 } 71 } 72 } 73 74 /** 75 * Copy references from one project to another. 76 * 77 * @param fromProject 78 * @param toProject 79 */ 80 public static void copyReferences( Project fromProject, Project toProject ) 81 { 82 copyReferences( fromProject.getReferences(), toProject ); 83 } 84 85 /** 86 * Copy references from a hashtable to a project. Will not 87 * overwrite existing references. 88 * 89 * @param refs 90 * @param project 91 */ 92 public static void copyReferences( Hashtable refs, Project project ) 93 { 94 Enumeration e = refs.keys(); 95 while ( e.hasMoreElements() ) 96 { 97 String key = e.nextElement().toString(); 98 // don't overwrite existing references 99 if ( project.getReference( key ) == null ) 100 { 101 project.addReference( key, refs.get( key ) ); 102 } 103 } 104 } 105 106 }