Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id D59F6200C55 for ; Wed, 8 Mar 2017 18:04:07 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id D434E160B90; Wed, 8 Mar 2017 17:04:07 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 5998F160B8A for ; Wed, 8 Mar 2017 18:04:06 +0100 (CET) Received: (qmail 10907 invoked by uid 500); 8 Mar 2017 17:04:04 -0000 Mailing-List: contact commits-help@maven.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@maven.apache.org Delivered-To: mailing list commits@maven.apache.org Received: (qmail 8561 invoked by uid 99); 8 Mar 2017 17:04:02 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 08 Mar 2017 17:04:02 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 6FB9DF322E; Wed, 8 Mar 2017 17:04:02 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: schulte@apache.org To: commits@maven.apache.org Date: Wed, 08 Mar 2017 17:04:34 -0000 Message-Id: <9251f713e92b46ccbcba1f3e6da40d17@git.apache.org> In-Reply-To: <5635eba896e245219ad9d10d6282e57d@git.apache.org> References: <5635eba896e245219ad9d10d6282e57d@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [34/45] maven git commit: [MNG-6078] Perform a proper merge of the two sources of command line arguments archived-at: Wed, 08 Mar 2017 17:04:08 -0000 [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 Project: http://git-wip-us.apache.org/repos/asf/maven/repo Commit: http://git-wip-us.apache.org/repos/asf/maven/commit/dc9c4db4 Tree: http://git-wip-us.apache.org/repos/asf/maven/tree/dc9c4db4 Diff: http://git-wip-us.apache.org/repos/asf/maven/diff/dc9c4db4 Branch: refs/heads/MNG-6114 Commit: dc9c4db4494b62e2231bb67b39678decf6329852 Parents: 5885e70 Author: Stephen Connolly Authored: Tue Feb 21 10:10:21 2017 +0000 Committer: Stephen Connolly Committed: Thu Feb 23 12:44:34 2017 +0000 ---------------------------------------------------------------------- .../java/org/apache/maven/cli/MavenCli.java | 25 +++--- .../org/apache/maven/cli/MergedCommandLine.java | 75 ++++++++++++++++++ .../java/org/apache/maven/cli/MavenCliTest.java | 83 +++++++++++++++----- 3 files changed, 149 insertions(+), 34 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/maven/blob/dc9c4db4/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.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 f788a5f..8d38ab0 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 @@ public class MavenCli 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 @@ public class MavenCli } } - 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 @@ public class MavenCli 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 ) { http://git-wip-us.apache.org/repos/asf/maven/blob/dc9c4db4/maven-embedder/src/main/java/org/apache/maven/cli/MergedCommandLine.java ---------------------------------------------------------------------- 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 0000000..cb0a587 --- /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