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