Coverage Report - org.apache.maven.shared.invoker.MavenCommandLineBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
MavenCommandLineBuilder
78%
168/215
75%
84/112
4,273
 
 1  
 package org.apache.maven.shared.invoker;
 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.IOException;
 24  
 import java.util.Iterator;
 25  
 import java.util.List;
 26  
 import java.util.Map;
 27  
 import java.util.Properties;
 28  
 
 29  
 import org.codehaus.plexus.util.Os;
 30  
 import org.codehaus.plexus.util.StringUtils;
 31  
 import org.codehaus.plexus.util.cli.CommandLineUtils;
 32  
 import org.codehaus.plexus.util.cli.Commandline;
 33  
 
 34  
 /**
 35  
  * @version $Id: MavenCommandLineBuilder.java 807829 2009-08-25 22:06:10Z olamy $
 36  
  */
 37  46
 public class MavenCommandLineBuilder
 38  
 {
 39  
 
 40  1
     private static final InvokerLogger DEFAULT_LOGGER = new SystemOutLogger();
 41  
 
 42  46
     private InvokerLogger logger = DEFAULT_LOGGER;
 43  
 
 44  
     private File workingDirectory;
 45  
 
 46  
     private File localRepositoryDirectory;
 47  
 
 48  
     private File mavenHome;
 49  
 
 50  
     private File mvnCommand;
 51  
 
 52  
     private Properties systemEnvVars;
 53  
 
 54  
     public Commandline build( InvocationRequest request )
 55  
         throws CommandLineConfigurationException
 56  
     {
 57  
         try
 58  
         {
 59  9
             checkRequiredState();
 60  
         }
 61  0
         catch ( IOException e )
 62  
         {
 63  0
             throw new CommandLineConfigurationException( e.getMessage(), e );
 64  9
         }
 65  9
         File mvn = null;
 66  
         try
 67  
         {
 68  9
             mvn = findMavenExecutable();
 69  
         }
 70  0
         catch ( IOException e )
 71  
         {
 72  0
             throw new CommandLineConfigurationException( e.getMessage(), e );
 73  9
         }
 74  9
         Commandline cli = new Commandline();
 75  
 
 76  9
         cli.setExecutable( mvn.getAbsolutePath() );
 77  
 
 78  
         // handling for OS-level envars
 79  9
         setShellEnvironment( request, cli );
 80  
 
 81  
         // interactive, offline, update-snapshots,
 82  
         // debug/show-errors, checksum policy
 83  9
         setFlags( request, cli );
 84  
 
 85  
         // failure behavior and [eventually] forced-reactor
 86  
         // includes/excludes, etc.
 87  9
         setReactorBehavior( request, cli );
 88  
 
 89  
         // working directory and local repository location
 90  9
         setEnvironmentPaths( request, cli );
 91  
 
 92  
         // pom-file and basedir handling
 93  9
         setPomLocation( request, cli );
 94  
 
 95  9
         setSettingsLocation( request, cli );
 96  
 
 97  9
         setProperties( request, cli );
 98  
 
 99  9
         setProfiles( request, cli );
 100  
 
 101  9
         setGoals( request, cli );
 102  
 
 103  9
         return cli;
 104  
     }
 105  
 
 106  
     protected void checkRequiredState()
 107  
         throws IOException
 108  
     {
 109  11
         if ( logger == null )
 110  
         {
 111  1
             throw new IllegalStateException( "A logger instance is required." );
 112  
         }
 113  
 
 114  10
         if ( ( mavenHome == null ) && ( System.getProperty( "maven.home" ) == null ) )
 115  
         // can be restored with 1.5
 116  
         // && ( System.getenv( "M2_HOME" ) != null ) )
 117  
         {
 118  0
             if ( !getSystemEnvVars().containsKey( "M2_HOME" ) )
 119  
             {
 120  0
                 throw new IllegalStateException( "Maven application directory was not "
 121  
                     + "specified, and ${maven.home} is not provided in the system "
 122  
                     + "properties. Please specify at least on of these." );
 123  
             }
 124  
         }
 125  10
     }
 126  
 
 127  
     protected void setSettingsLocation( InvocationRequest request, Commandline cli )
 128  
     {
 129  10
         File userSettingsFile = request.getUserSettingsFile();
 130  
 
 131  10
         if ( userSettingsFile != null )
 132  
         {
 133  
             try
 134  
             {
 135  2
                 File canSet = userSettingsFile.getCanonicalFile();
 136  2
                 userSettingsFile = canSet;
 137  
             }
 138  0
             catch ( IOException e )
 139  
             {
 140  0
                 logger.debug( "Failed to canonicalize user settings path: " + userSettingsFile.getAbsolutePath()
 141  
                     + ". Using as-is.", e );
 142  2
             }
 143  
 
 144  2
             cli.createArg().setValue( "-s" );
 145  2
             cli.createArg().setValue( userSettingsFile.getPath() );
 146  
         }
 147  10
     }
 148  
 
 149  
     protected void setShellEnvironment( InvocationRequest request, Commandline cli )
 150  
         throws CommandLineConfigurationException
 151  
     {
 152  9
         if ( request.isShellEnvironmentInherited() )
 153  
         {
 154  
             try
 155  
             {
 156  9
                 cli.addSystemEnvironment();
 157  9
                 cli.addEnvironment( "MAVEN_TERMINATE_CMD", "on" );
 158  
             }
 159  0
             catch ( IOException e )
 160  
             {
 161  0
                 throw new CommandLineConfigurationException( "Error reading shell environment variables. Reason: "
 162  
                     + e.getMessage(), e );
 163  
             }
 164  0
             catch ( Exception e )
 165  
             {
 166  0
                 if ( e instanceof RuntimeException )
 167  
                 {
 168  0
                     throw (RuntimeException) e;
 169  
                 }
 170  
                 else
 171  
                 {
 172  0
                     IllegalStateException error =
 173  
                         new IllegalStateException( "Unknown error retrieving shell environment variables. Reason: "
 174  
                             + e.getMessage() );
 175  0
                     error.initCause( e );
 176  
 
 177  0
                     throw error;
 178  
                 }
 179  9
             }
 180  
         }
 181  
 
 182  9
         if ( request.getJavaHome() != null )
 183  
         {
 184  0
             cli.addEnvironment( "JAVA_HOME", request.getJavaHome().getAbsolutePath() );
 185  
         }
 186  
 
 187  9
         if ( request.getMavenOpts() != null )
 188  
         {
 189  0
             cli.addEnvironment( "MAVEN_OPTS", request.getMavenOpts() );
 190  
         }
 191  
 
 192  9
         for ( Iterator iterator = request.getShellEnvironments().keySet().iterator(); iterator.hasNext(); )
 193  
         {
 194  0
             String key = (String) iterator.next();
 195  0
             String value = (String) request.getShellEnvironments().get( key );
 196  0
             cli.addEnvironment( key, value );
 197  
         }
 198  9
     }
 199  
 
 200  
     protected void setProfiles( InvocationRequest request, Commandline cli )
 201  
     {
 202  9
         List profiles = request.getProfiles();
 203  
 
 204  9
         if ( ( profiles != null ) && !profiles.isEmpty() )
 205  
         {
 206  1
             cli.createArg().setValue( "-P" );
 207  1
             cli.createArg().setValue( StringUtils.join( profiles.iterator(), "," ) );
 208  
         }
 209  
 
 210  9
     }
 211  
 
 212  
     protected void setGoals( InvocationRequest request, Commandline cli )
 213  
     {
 214  11
         List goals = request.getGoals();
 215  
 
 216  11
         if ( ( goals != null ) && !goals.isEmpty() )
 217  
         {
 218  10
             cli.createArg().setLine( StringUtils.join( goals.iterator(), " " ) );
 219  
         }
 220  11
     }
 221  
 
 222  
     protected void setProperties( InvocationRequest request, Commandline cli )
 223  
     {
 224  12
         Properties properties = request.getProperties();
 225  
 
 226  12
         if ( properties != null )
 227  
         {
 228  5
             for ( Iterator it = properties.entrySet().iterator(); it.hasNext(); )
 229  
             {
 230  6
                 Map.Entry entry = (Map.Entry) it.next();
 231  
 
 232  6
                 String key = (String) entry.getKey();
 233  6
                 String value = (String) entry.getValue();
 234  
 
 235  6
                 cli.createArg().setValue( "-D" );
 236  6
                 cli.createArg().setValue( key + '=' + value );
 237  
             }
 238  
         }
 239  12
     }
 240  
 
 241  
     protected void setPomLocation( InvocationRequest request, Commandline cli )
 242  
     {
 243  15
         boolean pomSpecified = false;
 244  
 
 245  15
         File pom = request.getPomFile();
 246  15
         String pomFilename = request.getPomFileName();
 247  15
         File baseDirectory = request.getBaseDirectory();
 248  
 
 249  15
         if ( pom != null )
 250  
         {
 251  2
             pomSpecified = true;
 252  
         }
 253  13
         else if ( baseDirectory != null )
 254  
         {
 255  12
             if ( baseDirectory.isDirectory() )
 256  
             {
 257  10
                 if ( pomFilename != null )
 258  
                 {
 259  2
                     pom = new File( baseDirectory, pomFilename );
 260  
 
 261  2
                     pomSpecified = true;
 262  
                 }
 263  
                 else
 264  
                 {
 265  8
                     pom = new File( baseDirectory, "pom.xml" );
 266  
                 }
 267  
             }
 268  
             else
 269  
             {
 270  2
                 logger.warn( "Base directory is a file. Using base directory as POM location." );
 271  
 
 272  2
                 pom = baseDirectory;
 273  
 
 274  2
                 pomSpecified = true;
 275  
             }
 276  
         }
 277  
 
 278  15
         if ( pomSpecified )
 279  
         {
 280  
             try
 281  
             {
 282  6
                 File canPom = pom.getCanonicalFile();
 283  6
                 pom = canPom;
 284  
             }
 285  0
             catch ( IOException e )
 286  
             {
 287  0
                 logger.debug( "Failed to canonicalize the POM path: " + pom + ". Using as-is.", e );
 288  6
             }
 289  
 
 290  6
             if ( !"pom.xml".equals( pom.getName() ) )
 291  
             {
 292  4
                 logger.debug( "Specified POM file is not named \'pom.xml\'. "
 293  
                     + "Using the \'-f\' command-line option to accommodate non-standard filename..." );
 294  
 
 295  4
                 cli.createArg().setValue( "-f" );
 296  4
                 cli.createArg().setValue( pom.getName() );
 297  
             }
 298  
         }
 299  15
     }
 300  
 
 301  
     protected void setEnvironmentPaths( InvocationRequest request, Commandline cli )
 302  
     {
 303  23
         File workingDirectory = request.getBaseDirectory();
 304  
 
 305  23
         if ( workingDirectory == null )
 306  
         {
 307  9
             File pomFile = request.getPomFile();
 308  9
             if ( pomFile != null )
 309  
             {
 310  2
                 workingDirectory = pomFile.getParentFile();
 311  
             }
 312  
         }
 313  
 
 314  23
         if ( workingDirectory == null )
 315  
         {
 316  7
             workingDirectory = this.workingDirectory;
 317  
         }
 318  
 
 319  23
         if ( workingDirectory == null )
 320  
         {
 321  6
             workingDirectory = new File( System.getProperty( "user.dir" ) );
 322  
         }
 323  17
         else if ( workingDirectory.isFile() )
 324  
         {
 325  2
             logger.warn( "Specified base directory (" + workingDirectory + ") is a file."
 326  
                 + " Using its parent directory..." );
 327  
 
 328  2
             workingDirectory = workingDirectory.getParentFile();
 329  
         }
 330  
 
 331  
         try
 332  
         {
 333  23
             cli.setWorkingDirectory( workingDirectory.getCanonicalPath() );
 334  
         }
 335  0
         catch ( IOException e )
 336  
         {
 337  0
             logger.debug( "Failed to canonicalize base directory: " + workingDirectory + ". Using as-is.", e );
 338  
 
 339  0
             cli.setWorkingDirectory( workingDirectory.getAbsolutePath() );
 340  23
         }
 341  
 
 342  23
         File localRepositoryDirectory = request.getLocalRepositoryDirectory( this.localRepositoryDirectory );
 343  
 
 344  23
         if ( localRepositoryDirectory != null )
 345  
         {
 346  
             try
 347  
             {
 348  6
                 File canLRD = localRepositoryDirectory.getCanonicalFile();
 349  6
                 localRepositoryDirectory = canLRD;
 350  
             }
 351  0
             catch ( IOException e )
 352  
             {
 353  0
                 logger.debug( "Failed to canonicalize local repository directory: " + localRepositoryDirectory
 354  
                     + ". Using as-is.", e );
 355  6
             }
 356  
 
 357  6
             if ( !localRepositoryDirectory.isDirectory() )
 358  
             {
 359  2
                 throw new IllegalArgumentException( "Local repository location: \'" + localRepositoryDirectory
 360  
                     + "\' is NOT a directory." );
 361  
             }
 362  
 
 363  4
             cli.createArg().setValue( "-D" );
 364  4
             cli.createArg().setValue( "maven.repo.local=" + localRepositoryDirectory.getPath() );
 365  
         }
 366  21
     }
 367  
 
 368  
     protected void setReactorBehavior( InvocationRequest request, Commandline cli )
 369  
     {
 370  
         // NOTE: The default is "fail-fast"
 371  14
         String failureBehavior = request.getFailureBehavior();
 372  
 
 373  14
         if ( StringUtils.isNotEmpty( failureBehavior ) )
 374  
         {
 375  14
             if ( InvocationRequest.REACTOR_FAIL_AT_END.equals( failureBehavior ) )
 376  
             {
 377  1
                 cli.createArg().setValue( "-fae" );
 378  
             }
 379  13
             else if ( InvocationRequest.REACTOR_FAIL_NEVER.equals( failureBehavior ) )
 380  
             {
 381  1
                 cli.createArg().setValue( "-fn" );
 382  
             }
 383  
         }
 384  
 
 385  14
         if ( request.isActivatedReactor() )
 386  
         {
 387  2
             cli.createArg().setValue( "-r" );
 388  2
             String[] includes = request.getActivatedReactorIncludes();
 389  2
             String[] excludes = request.getActivatedReactorExcludes();
 390  2
             if ( includes != null )
 391  
             {
 392  1
                 cli.createArg().setValue( "-D" );
 393  1
                 cli.createArg().setValue( "maven.reactor.includes=" + StringUtils.join( includes, "," ) );
 394  
             }
 395  2
             if ( excludes != null )
 396  
             {
 397  1
                 cli.createArg().setValue( "-D" );
 398  1
                 cli.createArg().setValue( "maven.reactor.excludes=" + StringUtils.join( excludes, "," ) );
 399  
             }
 400  
         }
 401  14
     }
 402  
 
 403  
     protected void setFlags( InvocationRequest request, Commandline cli )
 404  
     {
 405  17
         if ( !request.isInteractive() )
 406  
         {
 407  17
             cli.createArg().setValue( "-B" );
 408  
         }
 409  
 
 410  17
         if ( request.isOffline() )
 411  
         {
 412  2
             cli.createArg().setValue( "-o" );
 413  
         }
 414  
 
 415  17
         if ( request.isUpdateSnapshots() )
 416  
         {
 417  1
             cli.createArg().setValue( "-U" );
 418  
         }
 419  
 
 420  17
         if ( !request.isRecursive() )
 421  
         {
 422  0
             cli.createArg().setValue( "-N" );
 423  
         }
 424  
 
 425  17
         if ( request.isDebug() )
 426  
         {
 427  8
             cli.createArg().setValue( "-X" );
 428  
         }
 429  
         // this is superceded by -X, if it exists.
 430  9
         else if ( request.isShowErrors() )
 431  
         {
 432  1
             cli.createArg().setValue( "-e" );
 433  
         }
 434  
 
 435  17
         String checksumPolicy = request.getGlobalChecksumPolicy();
 436  17
         if ( InvocationRequest.CHECKSUM_POLICY_FAIL.equals( checksumPolicy ) )
 437  
         {
 438  1
             cli.createArg().setValue( "-C" );
 439  
         }
 440  16
         else if ( InvocationRequest.CHECKSUM_POLICY_WARN.equals( checksumPolicy ) )
 441  
         {
 442  1
             cli.createArg().setValue( "-c" );
 443  
         }
 444  17
         if ( request.isNonPluginUpdates() )
 445  
         {
 446  0
             cli.createArg().setValue( "-npu" );
 447  
         }
 448  
         
 449  17
         if ( request.isShowVersion() )
 450  
         {
 451  0
             cli.createArg().setValue( "-V" );
 452  
         }
 453  17
     }
 454  
 
 455  
     protected File findMavenExecutable()
 456  
         throws CommandLineConfigurationException, IOException
 457  
     {
 458  10
         if ( mavenHome == null )
 459  
         {
 460  3
             String mavenHomeProperty = System.getProperty( "maven.home" );
 461  3
             if ( mavenHomeProperty != null )
 462  
             {
 463  3
                 mavenHome = new File( mavenHomeProperty );
 464  3
                 if ( !mavenHome.isDirectory() )
 465  
                 {
 466  0
                     File binDir = mavenHome.getParentFile();
 467  0
                     if ( "bin".equals( binDir.getName() ) )
 468  
                     {
 469  
                         // ah, they specified the mvn
 470  
                         // executable instead...
 471  0
                         mavenHome = binDir.getParentFile();
 472  
                     }
 473  
                     else
 474  
                     {
 475  0
                         throw new IllegalStateException( "${maven.home} is not specified as a directory: \'"
 476  
                             + mavenHomeProperty + "\'." );
 477  
                     }
 478  
                 }
 479  
             }
 480  
 
 481  3
             if ( ( mavenHome == null ) && ( getSystemEnvVars().getProperty( "M2_HOME" ) != null ) )
 482  
             {
 483  0
                 mavenHome = new File( getSystemEnvVars().getProperty( "M2_HOME" ) );
 484  
             }
 485  
         }
 486  
 
 487  10
         logger.debug( "Using ${maven.home} of: \'" + mavenHome + "\'." );
 488  
 
 489  10
         if ( mvnCommand == null )
 490  
         {
 491  10
             if ( Os.isFamily( "windows" ) )
 492  
             {
 493  10
                 mvnCommand = new File( mavenHome, "/bin/mvn.bat" );
 494  
             }
 495  
             else
 496  
             {
 497  0
                 mvnCommand = new File( mavenHome, "/bin/mvn" );
 498  
             }
 499  
 
 500  
             try
 501  
             {
 502  10
                 File canonicalMvn = mvnCommand.getCanonicalFile();
 503  10
                 mvnCommand = canonicalMvn;
 504  
             }
 505  0
             catch ( IOException e )
 506  
             {
 507  0
                 logger.debug( "Failed to canonicalize maven executable: " + mvnCommand + ". Using as-is.", e );
 508  10
             }
 509  
 
 510  10
             if ( !mvnCommand.exists() )
 511  
             {
 512  0
                 throw new CommandLineConfigurationException( "Maven executable not found at: " + mvnCommand );
 513  
             }
 514  
         }
 515  
 
 516  10
         return mvnCommand;
 517  
     }
 518  
 
 519  
     /**
 520  
      * Wraps a path with quotes to handle paths with spaces. If no spaces are found, the original string is returned.
 521  
      * 
 522  
      * @param path string to wrap if containing spaces
 523  
      * @return quote wrapped string
 524  
      * @deprecated Quoting of command line arguments should be left to the Commandline from plexus-utils.
 525  
      */
 526  
     public String wrapStringWithQuotes( String path )
 527  
     {
 528  4
         if ( path.indexOf( " " ) > -1 )
 529  
         {
 530  2
             return "\"" + path + "\"";
 531  
         }
 532  
         else
 533  
         {
 534  2
             return path;
 535  
         }
 536  
     }
 537  
 
 538  
     private Properties getSystemEnvVars()
 539  
         throws IOException
 540  
     {
 541  0
         if ( this.systemEnvVars == null )
 542  
         {
 543  
             // with 1.5 replace with System.getenv()
 544  0
             this.systemEnvVars = CommandLineUtils.getSystemEnvVars();
 545  
         }
 546  0
         return this.systemEnvVars;
 547  
     }
 548  
 
 549  
     public File getLocalRepositoryDirectory()
 550  
     {
 551  0
         return localRepositoryDirectory;
 552  
     }
 553  
 
 554  
     public void setLocalRepositoryDirectory( File localRepositoryDirectory )
 555  
     {
 556  3
         this.localRepositoryDirectory = localRepositoryDirectory;
 557  3
     }
 558  
 
 559  
     public InvokerLogger getLogger()
 560  
     {
 561  0
         return logger;
 562  
     }
 563  
 
 564  
     public void setLogger( InvokerLogger logger )
 565  
     {
 566  7
         this.logger = logger;
 567  7
     }
 568  
 
 569  
     public File getMavenHome()
 570  
     {
 571  0
         return mavenHome;
 572  
     }
 573  
 
 574  
     public void setMavenHome( File mavenHome )
 575  
     {
 576  7
         this.mavenHome = mavenHome;
 577  7
     }
 578  
 
 579  
     public File getWorkingDirectory()
 580  
     {
 581  0
         return workingDirectory;
 582  
     }
 583  
 
 584  
     public void setWorkingDirectory( File workingDirectory )
 585  
     {
 586  2
         this.workingDirectory = workingDirectory;
 587  2
     }
 588  
 
 589  
 }