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.Map;
28  import org.apache.maven.shared.utils.cli.CommandLineException;
29  import org.apache.maven.shared.utils.cli.CommandLineUtils;
30  import org.apache.maven.shared.utils.cli.Commandline;
31  import org.apache.maven.shared.utils.cli.StreamConsumer;
32  import org.apache.maven.shared.utils.cli.WriterStreamConsumer;
33  
34  /**
35   * @author Benjamin Bentmann
36   */
37  class ForkedLauncher
38      implements MavenLauncher
39  {
40  
41      private final String mavenHome;
42  
43      private final String executable;
44  
45      public ForkedLauncher( String mavenHome )
46      {
47          this( mavenHome, false );
48      }
49  
50      public ForkedLauncher( String mavenHome, boolean debugJvm )
51      {
52          this.mavenHome = mavenHome;
53  
54          String script = debugJvm ? "mvnDebug" : "mvn";
55  
56          if ( mavenHome != null )
57          {
58              executable = new File( mavenHome, "bin/" + script ).getPath();
59          }
60          else
61          {
62              executable = script;
63          }
64      }
65  
66      public int run( String[] cliArgs, Map envVars, String workingDirectory, File logFile )
67          throws IOException, LauncherException
68      {
69          Commandline cmd = new Commandline();
70  
71          cmd.setExecutable( executable );
72  
73          if ( mavenHome != null )
74          {
75              cmd.addEnvironment( "M2_HOME", mavenHome );
76          }
77  
78          if ( envVars != null )
79          {
80              for ( Object o : envVars.keySet() )
81              {
82                  String key = (String) o;
83  
84                  cmd.addEnvironment( key, (String) envVars.get( key ) );
85              }
86          }
87  
88          if ( envVars == null || envVars.get( "JAVA_HOME" ) == null )
89          {
90              cmd.addEnvironment( "JAVA_HOME", System.getProperty( "java.home" ) );
91          }
92  
93          cmd.addEnvironment( "MAVEN_TERMINATE_CMD", "on" );
94  
95          cmd.setWorkingDirectory( workingDirectory );
96  
97          for ( String cliArg : cliArgs )
98          {
99              cmd.createArg().setValue( cliArg );
100         }
101 
102         Writer logWriter = new FileWriter( logFile );
103 
104         StreamConsumer out = new WriterStreamConsumer( logWriter );
105 
106         StreamConsumer err = new WriterStreamConsumer( logWriter );
107 
108         try
109         {
110             return CommandLineUtils.executeCommandLine( cmd, out, err );
111         }
112         catch ( CommandLineException e )
113         {
114             throw new LauncherException( "Failed to run Maven: " + e.getMessage() + "\n" + cmd, e );
115         }
116         finally
117         {
118             logWriter.close();
119         }
120     }
121 
122     public int run( String[] cliArgs, String workingDirectory, File logFile )
123         throws IOException, LauncherException
124     {
125         return run( cliArgs, Collections.EMPTY_MAP, workingDirectory, logFile );
126     }
127 
128 }