View Javadoc
1   package org.apache.maven.it;
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.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.Locale;
30  import java.util.Map;
31  import java.util.Properties;
32  import java.util.regex.Matcher;
33  import java.util.regex.Pattern;
34  
35  import org.apache.maven.shared.utils.StringUtils;
36  import org.apache.maven.shared.utils.cli.CommandLineException;
37  import org.apache.maven.shared.utils.cli.CommandLineUtils;
38  import org.apache.maven.shared.utils.cli.Commandline;
39  import org.apache.maven.shared.utils.cli.StreamConsumer;
40  import org.apache.maven.shared.utils.cli.WriterStreamConsumer;
41  import org.apache.maven.shared.utils.io.FileUtils;
42  
43  /**
44   * @author Benjamin Bentmann
45   */
46  class ForkedLauncher
47      implements MavenLauncher
48  {
49  
50      private final String mavenHome;
51  
52      private final String executable;
53  
54      private final Map<String, String> envVars;
55  
56      ForkedLauncher( String mavenHome )
57      {
58          this( mavenHome, Collections.<String, String>emptyMap(), false );
59      }
60  
61      ForkedLauncher( String mavenHome, Map<String, String> envVars, boolean debugJvm )
62      {
63          this( mavenHome, envVars, debugJvm, false );
64      }
65  
66      ForkedLauncher( String mavenHome, Map<String, String> envVars, boolean debugJvm, boolean wrapper )
67      {
68          this.mavenHome = mavenHome;
69          this.envVars = envVars;
70  
71          if ( wrapper )
72          {
73              final StringBuilder script = new StringBuilder();
74              
75              if ( !isWindows() )
76              {
77                  script.append( "./" );
78              }
79              
80              script.append( "mvnw" );
81              
82              if ( debugJvm )
83              {
84                  script.append( "Debug" );
85              }
86              executable = script.toString();
87          }
88          else
89          {
90              String script = "mvn" + ( debugJvm ? "Debug" : "" );
91  
92              if ( mavenHome != null )
93              {
94                  executable = new File( mavenHome, "bin/" + script ).getPath();
95              }
96              else
97              {
98                  executable = script;
99              }
100         }
101     }
102 
103     public int run( String[] cliArgs, Properties systemProperties, Map<String, String> envVars,
104                     String workingDirectory, File logFile )
105         throws IOException, LauncherException
106     {
107         Commandline cmd = new Commandline();
108 
109         cmd.setExecutable( executable );
110 
111         if ( mavenHome != null )
112         {
113             cmd.addEnvironment( "M2_HOME", mavenHome );
114         }
115 
116         if ( envVars != null )
117         {
118             for ( Map.Entry<String, String> envVar : envVars.entrySet() )
119             {
120                 cmd.addEnvironment( envVar.getKey(), envVar.getValue() );
121             }
122         }
123 
124         if ( envVars == null || envVars.get( "JAVA_HOME" ) == null )
125         {
126             cmd.addEnvironment( "JAVA_HOME", System.getProperty( "java.home" ) );
127         }
128 
129         cmd.addEnvironment( "MAVEN_TERMINATE_CMD", "on" );
130 
131         cmd.setWorkingDirectory( workingDirectory );
132 
133         for ( Object o : systemProperties.keySet() )
134         {
135             String key = (String) o;
136             String value = systemProperties.getProperty( key );
137             cmd.createArg().setValue( "-D" + key + "=" + value );
138         }
139 
140         for ( String cliArg : cliArgs )
141         {
142             cmd.createArg().setValue( cliArg );
143         }
144 
145         Writer logWriter = new FileWriter( logFile );
146 
147         StreamConsumer out = new WriterStreamConsumer( logWriter );
148 
149         StreamConsumer err = new WriterStreamConsumer( logWriter );
150 
151         try
152         {
153             return CommandLineUtils.executeCommandLine( cmd, out, err );
154         }
155         catch ( CommandLineException e )
156         {
157             throw new LauncherException( "Failed to run Maven: " + e.getMessage() + "\n" + cmd, e );
158         }
159         finally
160         {
161             logWriter.close();
162         }
163     }
164 
165     public int run( String[] cliArgs, Properties systemProperties, String workingDirectory, File logFile )
166         throws IOException, LauncherException
167     {
168         return run( cliArgs, systemProperties, envVars, workingDirectory, logFile );
169     }
170 
171     public String getMavenVersion()
172         throws IOException, LauncherException
173     {
174         File logFile;
175         try
176         {
177             logFile = File.createTempFile( "maven", "log" );
178         }
179         catch ( IOException e )
180         {
181             throw new LauncherException( "Error creating temp file", e );
182         }
183 
184         // disable EMMA runtime controller port allocation, should be harmless if EMMA is not used
185         Map<String, String> envVars = Collections.singletonMap( "MAVEN_OPTS", "-Demma.rt.control=false" );
186         run( new String[] { "--version" }, new Properties(), envVars, null, logFile );
187 
188         List<String> logLines = FileUtils.loadFile( logFile );
189         // noinspection ResultOfMethodCallIgnored
190         logFile.delete();
191 
192         String version = extractMavenVersion( logLines );
193 
194         if ( version == null )
195         {
196             throw new LauncherException( "Illegal Maven output: String 'Maven' not found in the following output:\n"
197                 + StringUtils.join( logLines.iterator(), "\n" ) );
198         }
199         else
200         {
201             return version;
202         }
203     }
204 
205     static String extractMavenVersion( List<String> logLines )
206     {
207         String version = null;
208 
209         final Pattern mavenVersion = Pattern.compile( "(?i).*Maven.*? ([0-9]\\.\\S*).*" );
210 
211         for ( Iterator<String> it = logLines.iterator(); version == null && it.hasNext(); )
212         {
213             String line = it.next();
214 
215             Matcher m = mavenVersion.matcher( line );
216             if ( m.matches() )
217             {
218                 version = m.group( 1 );
219             }
220         }
221 
222         return version;
223     }
224     
225     private static boolean isWindows()
226     {
227         String osName = System.getProperty( "os.name" ).toLowerCase( Locale.US );
228 
229         return ( osName.indexOf( "windows" ) > -1 );
230     }
231 
232 }