1 package org.apache.maven.plugin.ant;
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 org.apache.tools.ant.BuildException;
23 import org.apache.tools.ant.ExitException;
24 import org.apache.tools.ant.Main;
25 import org.apache.tools.ant.util.optional.NoExitSecurityManager;
26 import org.codehaus.plexus.util.StringUtils;
27
28 import java.io.ByteArrayOutputStream;
29 import java.io.File;
30 import java.io.OutputStream;
31 import java.io.PrintStream;
32 import java.util.Properties;
33
34 /**
35 * Wrap <code>Ant</code> call.
36 *
37 * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
38 * @version $Id: AntWrapper.java 1517969 2013-08-27 20:14:02Z krosenvold $
39 */
40 public class AntWrapper
41 {
42 /**
43 * Invoke <code>Ant</code> for the default target of a given build file.
44 *
45 * @param antBuild an <code>Ant build</code> file
46 * @throws IllegalArgumentException if any
47 * @throws BuildException if any
48 */
49 public static void invoke( File antBuild )
50 throws BuildException, IllegalArgumentException
51 {
52 if ( !antBuild.exists() )
53 {
54 throw new IllegalArgumentException( "antBuild should exist" );
55 }
56 if ( !antBuild.isFile() )
57 {
58 throw new IllegalArgumentException( "antBuild should be a file" );
59 }
60
61 // ----------------------------------------------------------------------
62 // NB: By using org.apache.tools.ant.launch.Launcher, we have:
63 // java.lang.ClassCastException
64 // at org.apache.tools.ant.launch.Launcher.run(Launcher.java:245)
65 // So, using org.apache.tools.ant.Main#main()
66 // ----------------------------------------------------------------------
67
68 Properties oldSystemProperties = System.getProperties();
69
70 System.setProperty( "basedir", antBuild.getParentFile().getAbsolutePath() );
71
72 SecurityManager oldSm = System.getSecurityManager();
73 System.setSecurityManager( new NoExitSecurityManager() );
74
75 PrintStream oldErr = System.err;
76 OutputStream errOS = new ByteArrayOutputStream();
77 PrintStream err = new PrintStream( errOS );
78 System.setErr( err );
79
80 PrintStream oldOut = System.out;
81 OutputStream outOS = new ByteArrayOutputStream();
82 PrintStream out = new PrintStream( outOS );
83 System.setOut( out );
84
85 // ----------------------------------------------------------------------
86 // To prevent Javac exception i.e. "Unable to find a javac compiler"
87 // Ant can use the same command line arguments as the javac of the current VM
88 // ----------------------------------------------------------------------
89 System.setProperty( "build.compiler", "extJavac" );
90
91 try
92 {
93 Main.main( new String[]{ "-f", antBuild.getAbsolutePath() } );
94 }
95 catch ( ExitException e )
96 {
97 if ( StringUtils.isNotEmpty( errOS.toString() ) )
98 {
99 throw new BuildException(
100 "Error in the Ant build file. \n= Ant output =\n" + outOS.toString() + "\n" + errOS.toString() );
101 }
102 }
103 finally
104 {
105 System.setSecurityManager( oldSm );
106 System.setErr( oldErr );
107 System.setOut( oldOut );
108 System.setProperties( oldSystemProperties );
109 }
110 }
111 }