1 package org.apache.maven.plugin.ant;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.ByteArrayOutputStream;
23 import java.io.File;
24 import java.io.OutputStream;
25 import java.io.PrintStream;
26 import java.util.Properties;
27
28 import org.apache.tools.ant.BuildException;
29 import org.apache.tools.ant.ExitException;
30 import org.apache.tools.ant.Main;
31 import org.apache.tools.ant.util.optional.NoExitSecurityManager;
32 import org.codehaus.plexus.util.StringUtils;
33
34
35
36
37
38
39
40 public class AntWrapper
41 {
42
43
44
45
46
47
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
63
64
65
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
87
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( "Error in the Ant build file. \n= Ant output =\n" + outOS.toString() + "\n"
100 + 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 }