1 package org.apache.maven.cli; 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 org.apache.commons.cli.CommandLine; 23 import org.apache.commons.cli.Option; 24 25 import java.util.ArrayList; 26 import java.util.List; 27 28 /** 29 * A {@link CommandLine} instance that represents a merged command line combining CLI arguments with those from the 30 * {@code .mvn/maven.config} while reflecting the handling of {@link CLIManager#SET_SYSTEM_PROPERTY} versus all the 31 * other command line options (last wins vs first wins respectively). 32 */ 33 class MergedCommandLine 34 extends CommandLine 35 { 36 MergedCommandLine( CommandLine commandLine, CommandLine configFile ) 37 { 38 // such a pity that Commons CLI does not offer either a builder or a formatter and we need to extend 39 // to perform the merge. A formatter would mean we could unparse and reparse (not ideal but would work). 40 // A builder would be ideal for this kind of merge like processing. 41 super(); 42 // the args are easy, cli first then config file 43 for ( String arg : commandLine.getArgs() ) 44 { 45 addArg( arg ); 46 } 47 for ( String arg : configFile.getArgs() ) 48 { 49 addArg( arg ); 50 } 51 // now add all options, except for -D with cli first then config file 52 List<Option> setPropertyOptions = new ArrayList<>(); 53 for ( Option opt : commandLine.getOptions() ) 54 { 55 if ( String.valueOf( CLIManager.SET_SYSTEM_PROPERTY ).equals( opt.getOpt() ) ) 56 { 57 setPropertyOptions.add( opt ); 58 } 59 else 60 { 61 addOption( opt ); 62 } 63 } 64 for ( Option opt : configFile.getOptions() ) 65 { 66 addOption( opt ); 67 } 68 // finally add the CLI system properties 69 for ( Option opt : setPropertyOptions ) 70 { 71 addOption( opt ); 72 } 73 } 74 75 }