View Javadoc

1   package org.apache.maven.artifact.ant.util;
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.Hashtable;
23  import java.util.Map;
24  
25  import org.apache.tools.ant.Project;
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<String,String> props, Project project )
53      {
54          for ( Map.Entry<String, String> entry : props.entrySet() )
55          {
56              String key = entry.getKey();
57              if ( "basedir".equals( key ) || "ant.file".equals( key ) )
58              {
59                  // basedir and ant.file get special treatment in execute()
60                  continue;
61              }
62  
63              // don't re-set user properties, avoid the warning message
64              if ( project.getProperty( key ) == null )
65              {
66                  // no user property
67                  project.setNewProperty( key, entry.getValue() );
68              }
69          }
70      }
71  
72      /**
73       * Copy references from one project to another.
74       *
75       * @param fromProject
76       * @param toProject
77       */
78      public static void copyReferences( Project fromProject, Project toProject )
79      {
80          copyReferences( fromProject.getReferences(), toProject );
81      }
82  
83      /**
84       * Copy references from a hashtable to a project.  Will not
85       * overwrite existing references.
86       *
87       * @param refs
88       * @param project
89       */
90      public static void copyReferences( Hashtable<String,String> refs, Project project )
91      {
92          for ( Map.Entry<String, String> entry : refs.entrySet() )
93          {
94              String key = entry.getKey();
95              // don't overwrite existing references
96              if ( project.getReference( key ) == null )
97              {
98                  project.addReference( key, entry.getValue() );
99              }
100         }
101     }
102 
103 }