1 package org.apache.maven.it;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.FileWriter;
24 import java.io.IOException;
25 import java.io.Writer;
26 import java.util.Collections;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Properties;
31 import java.util.regex.Matcher;
32 import java.util.regex.Pattern;
33
34 import org.apache.maven.shared.utils.StringUtils;
35 import org.apache.maven.shared.utils.cli.CommandLineException;
36 import org.apache.maven.shared.utils.cli.CommandLineUtils;
37 import org.apache.maven.shared.utils.cli.Commandline;
38 import org.apache.maven.shared.utils.cli.StreamConsumer;
39 import org.apache.maven.shared.utils.cli.WriterStreamConsumer;
40 import org.apache.maven.shared.utils.io.FileUtils;
41
42
43
44
45 class ForkedLauncher
46 implements MavenLauncher
47 {
48
49 private final String mavenHome;
50
51 private final String executable;
52
53 private final Map<String, String> envVars;
54
55 public ForkedLauncher( String mavenHome )
56 {
57 this( mavenHome, Collections.<String, String>emptyMap(), false );
58 }
59
60 public ForkedLauncher( String mavenHome, Map<String, String> envVars, boolean debugJvm )
61 {
62 this.mavenHome = mavenHome;
63 this.envVars = envVars;
64
65 String script = debugJvm ? "mvnDebug" : "mvn";
66
67 if ( mavenHome != null )
68 {
69 executable = new File( mavenHome, "bin/" + script ).getPath();
70 }
71 else
72 {
73 executable = script;
74 }
75 }
76
77 public int run( String[] cliArgs, Properties systemProperties, Map<String, String> envVars,
78 String workingDirectory, File logFile )
79 throws IOException, LauncherException
80 {
81 Commandline cmd = new Commandline();
82
83 cmd.setExecutable( executable );
84
85 if ( mavenHome != null )
86 {
87 cmd.addEnvironment( "M2_HOME", mavenHome );
88 }
89
90 if ( envVars != null )
91 {
92 for ( Map.Entry<String, String> envVar : envVars.entrySet() )
93 {
94 cmd.addEnvironment( envVar.getKey(), envVar.getValue() );
95 }
96 }
97
98 if ( envVars == null || envVars.get( "JAVA_HOME" ) == null )
99 {
100 cmd.addEnvironment( "JAVA_HOME", System.getProperty( "java.home" ) );
101 }
102
103 cmd.addEnvironment( "MAVEN_TERMINATE_CMD", "on" );
104
105 cmd.setWorkingDirectory( workingDirectory );
106
107 for ( Object o : systemProperties.keySet() )
108 {
109 String key = (String) o;
110 String value = systemProperties.getProperty( key );
111 cmd.createArg().setValue( "-D" + key + "=" + value );
112 }
113
114 for ( String cliArg : cliArgs )
115 {
116 cmd.createArg().setValue( cliArg );
117 }
118
119 Writer logWriter = new FileWriter( logFile );
120
121 StreamConsumer out = new WriterStreamConsumer( logWriter );
122
123 StreamConsumer err = new WriterStreamConsumer( logWriter );
124
125 try
126 {
127 return CommandLineUtils.executeCommandLine( cmd, out, err );
128 }
129 catch ( CommandLineException e )
130 {
131 throw new LauncherException( "Failed to run Maven: " + e.getMessage() + "\n" + cmd, e );
132 }
133 finally
134 {
135 logWriter.close();
136 }
137 }
138
139 public int run( String[] cliArgs, Properties systemProperties, String workingDirectory, File logFile )
140 throws IOException, LauncherException
141 {
142 return run( cliArgs, systemProperties, envVars, workingDirectory, logFile );
143 }
144
145 public String getMavenVersion()
146 throws IOException, LauncherException
147 {
148 File logFile;
149 try
150 {
151 logFile = File.createTempFile( "maven", "log" );
152 }
153 catch ( IOException e )
154 {
155 throw new LauncherException( "Error creating temp file", e );
156 }
157
158
159 Map<String, String> envVars = Collections.singletonMap( "MAVEN_OPTS", "-Demma.rt.control=false" );
160 run( new String[] { "--version" }, new Properties(), envVars, null, logFile );
161
162 List<String> logLines = FileUtils.loadFile( logFile );
163
164 logFile.delete();
165
166 String version = extractMavenVersion( logLines );
167
168 if ( version == null )
169 {
170 throw new LauncherException( "Illegal Maven output: String 'Maven' not found in the following output:\n"
171 + StringUtils.join( logLines.iterator(), "\n" ) );
172 }
173 else
174 {
175 return version;
176 }
177 }
178
179 static String extractMavenVersion( List<String> logLines )
180 {
181 String version = null;
182
183 final Pattern mavenVersion = Pattern.compile( "(?i).*Maven.*? ([0-9]\\.\\S*).*" );
184
185 for ( Iterator<String> it = logLines.iterator(); version == null && it.hasNext(); )
186 {
187 String line = it.next();
188
189 Matcher m = mavenVersion.matcher( line );
190 if ( m.matches() )
191 {
192 version = m.group( 1 );
193 }
194 }
195
196 return version;
197 }
198
199 }