From dc9c4db4494b62e2231bb67b39678decf6329852 Mon Sep 17 00:00:00 2001 From: Stephen Connolly Date: Tue, 21 Feb 2017 10:10:21 +0000 Subject: [PATCH] [MNG-6078] Perform a proper merge of the two sources of command line arguments - Needed to extend Commons CLI's CommandLine just to perform the merged --- .../java/org/apache/maven/cli/MavenCli.java | 25 ++---- .../apache/maven/cli/MergedCommandLine.java | 75 +++++++++++++++++ .../org/apache/maven/cli/MavenCliTest.java | 83 +++++++++++++++---- 3 files changed, 149 insertions(+), 34 deletions(-) create mode 100644 maven-embedder/src/main/java/org/apache/maven/cli/MergedCommandLine.java diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java index f788a5f615..8d38ab0623 100644 --- a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java +++ b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java @@ -383,7 +383,7 @@ void cli( CliRequest cliRequest ) CLIManager cliManager = new CLIManager(); List args = new ArrayList<>(); - + CommandLine mavenConfig = null; try { File configFile = new File( cliRequest.multiModuleProjectDirectory, MVN_MAVEN_CONFIG ); @@ -398,8 +398,8 @@ void cli( CliRequest cliRequest ) } } - CommandLine config = cliManager.parse( args.toArray( new String[args.size()] ) ); - List unrecongized = config.getArgList(); + mavenConfig = cliManager.parse( args.toArray( new String[args.size()] ) ); + List unrecongized = mavenConfig.getArgList(); if ( !unrecongized.isEmpty() ) { throw new ParseException( "Unrecognized maven.config entries: " + unrecongized ); @@ -415,21 +415,14 @@ void cli( CliRequest cliRequest ) try { - int index = 0; - for ( String arg : cliRequest.args ) + if ( mavenConfig == null ) { - if ( arg.startsWith( "-D" ) ) - { - // a property definition so needs to come last so that the last property wins - args.add( arg ); - } - else - { - // not a property definition so needs to come first to override maven.config - args.add( index++, arg ); - } + cliRequest.commandLine = cliManager.parse( cliRequest.args ); + } + else + { + cliRequest.commandLine = new MergedCommandLine( cliManager.parse( cliRequest.args ), mavenConfig ); } - cliRequest.commandLine = cliManager.parse( args.toArray( new String[args.size()] ) ); } catch ( ParseException e ) { diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/MergedCommandLine.java b/maven-embedder/src/main/java/org/apache/maven/cli/MergedCommandLine.java new file mode 100644 index 0000000000..cb0a5875db --- /dev/null +++ b/maven-embedder/src/main/java/org/apache/maven/cli/MergedCommandLine.java @@ -0,0 +1,75 @@ +package org.apache.maven.cli; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Option; + +import java.util.ArrayList; +import java.util.List; + +/** + * A {@link CommandLine} instance that represents a merged command line combining CLI arguments with those from the + * {@code .mvn/maven.config} while reflecting the handling of {@link CLIManager#SET_SYSTEM_PROPERTY} versus all the + * other command line options (last wins vs first wins respectively). + */ +class MergedCommandLine + extends CommandLine +{ + MergedCommandLine( CommandLine commandLine, CommandLine configFile ) + { + // such a pity that Commons CLI does not offer either a builder or a formatter and we need to extend + // to perform the merge. A formatter would mean we could unparse and reparse (not ideal but would work). + // A builder would be ideal for this kind of merge like processing. + super(); + // the args are easy, cli first then config file + for ( String arg : commandLine.getArgs() ) + { + addArg( arg ); + } + for ( String arg : configFile.getArgs() ) + { + addArg( arg ); + } + // now add all options, except for -D with cli first then config file + List