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.lang.reflect.Field;
23  
24  import org.apache.tools.ant.BuildException;
25  import org.apache.tools.ant.Project;
26  import org.apache.tools.ant.taskdefs.Ant;
27  
28  /**
29   * A modified version of the Ant task that allows access to the
30   * new sub project.
31   *
32   * @author pgier
33   */
34  public class AntTaskModified
35      extends Ant
36  {
37  
38      /**
39       * The Ant tasks sets newProject to null at the end of execute(), so
40       * we need to save this object to a different place.
41       */
42      private Project savedNewProject;
43  
44      public void init()
45      {
46          super.init();
47          savedNewProject = saveNewProject();
48      }
49  
50      /**
51       * This is a hack to get access to the private variable "newProject" in the Ant task. This should not be used.
52       * Note: This may not work with later versions of Ant
53       *
54       * @return
55       */
56      private Project saveNewProject()
57      {
58          try
59          {
60              Field newProjectField = Ant.class.getDeclaredField( "newProject" );
61              newProjectField.setAccessible( true );
62  
63              return (Project) newProjectField.get( this );
64          }
65          catch ( Exception e )
66          {
67              throw new BuildException( "Unable to load cache: " + e, e );
68          }
69      }
70  
71      /**
72       * Get the new Ant project created by this task.
73       *
74       * @return
75       */
76      public Project getSavedNewProject()
77      {
78          return savedNewProject;
79      }
80  
81  }