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