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