From d201131b59f606519c38ec8c1138d7bcb15f48af Mon Sep 17 00:00:00 2001 From: John Dennis Casey Date: Thu, 2 Mar 2006 18:18:06 +0000 Subject: [PATCH] (merged from 382460.) [MNG-1415] Added quoted-argument reconstruction to MavenCLI, and quote preservation to the shell scripts. Both are necessary to fix this issue. Test is it0098. git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@382461 13f79535-47bb-0310-9956-ffa450edef68 --- maven-core-it/README.txt | 2 + maven-core-it/integration-tests.txt | 1 + maven-core-it/it0098/cli-options.txt | 1 + maven-core-it/it0098/expected-results.txt | 1 + maven-core-it/it0098/goals.txt | 1 + maven-core-it/it0098/pom.xml | 22 ++++ maven-core-it/it0098/test project/pom.xml | 18 +++ .../src/main/java/org/someproject/App.java | 13 ++ .../test/java/org/someproject/AppTest.java | 38 ++++++ maven-core-it/it0098/verifier.properties | 1 + maven-core/src/bin/m2 | 3 +- maven-core/src/bin/mvn | 10 +- .../java/org/apache/maven/cli/MavenCli.java | 121 +++++++++++++++++- 13 files changed, 229 insertions(+), 3 deletions(-) create mode 100644 maven-core-it/it0098/cli-options.txt create mode 100644 maven-core-it/it0098/expected-results.txt create mode 100644 maven-core-it/it0098/goals.txt create mode 100644 maven-core-it/it0098/pom.xml create mode 100644 maven-core-it/it0098/test project/pom.xml create mode 100644 maven-core-it/it0098/test project/src/main/java/org/someproject/App.java create mode 100644 maven-core-it/it0098/test project/src/test/java/org/someproject/AppTest.java create mode 100644 maven-core-it/it0098/verifier.properties diff --git a/maven-core-it/README.txt b/maven-core-it/README.txt index d2689ca1ce..349275b465 100644 --- a/maven-core-it/README.txt +++ b/maven-core-it/README.txt @@ -263,6 +263,8 @@ it0096: Test that plugin executions from >1 step of inheritance don't run multip it0097: Test that the implied relative path for the parent POM works, even two levels deep. +it0098: Test that quoted system properties are processed correctly. [MNG-1415] + ------------------------------------------------------------------------------- - generated sources diff --git a/maven-core-it/integration-tests.txt b/maven-core-it/integration-tests.txt index 7036ddc6fb..54c0c978ad 100644 --- a/maven-core-it/integration-tests.txt +++ b/maven-core-it/integration-tests.txt @@ -1,3 +1,4 @@ +it0098 it0097 it0096 it0095 diff --git a/maven-core-it/it0098/cli-options.txt b/maven-core-it/it0098/cli-options.txt new file mode 100644 index 0000000000..fcc742f004 --- /dev/null +++ b/maven-core-it/it0098/cli-options.txt @@ -0,0 +1 @@ +-DgroupId=org.someproject "-DartifactId=test project" diff --git a/maven-core-it/it0098/expected-results.txt b/maven-core-it/it0098/expected-results.txt new file mode 100644 index 0000000000..8d690b7b63 --- /dev/null +++ b/maven-core-it/it0098/expected-results.txt @@ -0,0 +1 @@ +test project/pom.xml diff --git a/maven-core-it/it0098/goals.txt b/maven-core-it/it0098/goals.txt new file mode 100644 index 0000000000..a5feaf7a9a --- /dev/null +++ b/maven-core-it/it0098/goals.txt @@ -0,0 +1 @@ +archetype:create diff --git a/maven-core-it/it0098/pom.xml b/maven-core-it/it0098/pom.xml new file mode 100644 index 0000000000..5c5b2073c1 --- /dev/null +++ b/maven-core-it/it0098/pom.xml @@ -0,0 +1,22 @@ + + 4.0.0 + org.apache.maven.it0098 + it0098-archetype-orchestration-test + 1 + + + + + org.apache.maven.plugins + maven-clean-plugin + + + + test project + + + + + + + diff --git a/maven-core-it/it0098/test project/pom.xml b/maven-core-it/it0098/test project/pom.xml new file mode 100644 index 0000000000..c8ea3fed63 --- /dev/null +++ b/maven-core-it/it0098/test project/pom.xml @@ -0,0 +1,18 @@ + + 4.0.0 + org.someproject + test project + jar + 1.0-SNAPSHOT + Maven Quick Start Archetype + http://maven.apache.org + + + junit + junit + 3.8.1 + test + + + diff --git a/maven-core-it/it0098/test project/src/main/java/org/someproject/App.java b/maven-core-it/it0098/test project/src/main/java/org/someproject/App.java new file mode 100644 index 0000000000..5764df60ec --- /dev/null +++ b/maven-core-it/it0098/test project/src/main/java/org/someproject/App.java @@ -0,0 +1,13 @@ +package org.someproject; + +/** + * Hello world! + * + */ +public class App +{ + public static void main( String[] args ) + { + System.out.println( "Hello World!" ); + } +} diff --git a/maven-core-it/it0098/test project/src/test/java/org/someproject/AppTest.java b/maven-core-it/it0098/test project/src/test/java/org/someproject/AppTest.java new file mode 100644 index 0000000000..c039fa2594 --- /dev/null +++ b/maven-core-it/it0098/test project/src/test/java/org/someproject/AppTest.java @@ -0,0 +1,38 @@ +package org.someproject; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class AppTest + extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testApp() + { + assertTrue( true ); + } +} diff --git a/maven-core-it/it0098/verifier.properties b/maven-core-it/it0098/verifier.properties new file mode 100644 index 0000000000..68cb43fc45 --- /dev/null +++ b/maven-core-it/it0098/verifier.properties @@ -0,0 +1 @@ +failOnErrorOutput=false diff --git a/maven-core/src/bin/m2 b/maven-core/src/bin/m2 index 6ccf29144e..1101995bbd 100755 --- a/maven-core/src/bin/m2 +++ b/maven-core/src/bin/m2 @@ -22,4 +22,5 @@ echo "" echo THE m2 COMMMAND IS DEPRECATED - PLEASE RUN mvn INSTEAD echo "" -exec "`dirname "$0"`/mvn" $@ +. `dirname "$0"`/mvn +exec "`dirname "$0"`/mvn" $QUOTED_ARGS diff --git a/maven-core/src/bin/mvn b/maven-core/src/bin/mvn index 014206b12b..8f3ceb3429 100755 --- a/maven-core/src/bin/mvn +++ b/maven-core/src/bin/mvn @@ -18,6 +18,14 @@ # Copyright (c) 2001-2002 The Apache Software Foundation. All rights # reserved. +QUOTED_ARGS="" +while [ "$1" != "" ] ; do + + QUOTED_ARGS="$QUOTED_ARGS \"$1\"" + shift + +done + if [ -f /etc/mavenrc ] ; then . /etc/mavenrc fi @@ -134,5 +142,5 @@ exec "$JAVACMD" \ -classpath "${M2_HOME}"/core/boot/classworlds-*.jar \ "-Dclassworlds.conf=${M2_HOME}/bin/m2.conf" \ "-Dmaven.home=${M2_HOME}" \ - ${CLASSWORLDS_LAUNCHER} $@ + ${CLASSWORLDS_LAUNCHER} $QUOTED_ARGS diff --git a/maven-core/src/main/java/org/apache/maven/cli/MavenCli.java b/maven-core/src/main/java/org/apache/maven/cli/MavenCli.java index 00f5ae82a6..7fcc1defa6 100644 --- a/maven-core/src/main/java/org/apache/maven/cli/MavenCli.java +++ b/maven-core/src/main/java/org/apache/maven/cli/MavenCli.java @@ -56,6 +56,8 @@ import org.codehaus.plexus.util.xml.pull.XmlPullParserException; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; import java.util.Properties; import java.util.StringTokenizer; @@ -703,8 +705,125 @@ public class MavenCli public CommandLine parse( String[] args ) throws ParseException { + // We need to eat any quotes surrounding arguments... + String[] cleanArgs = cleanArgs( args ); + CommandLineParser parser = new GnuParser(); - return parser.parse( options, args ); + return parser.parse( options, cleanArgs ); + } + + private String[] cleanArgs( String[] args ) + { + List cleaned = new ArrayList(); + + StringBuffer currentArg = null; + + for ( int i = 0; i < args.length; i++ ) + { + String arg = args[i]; + +// System.out.println( "Processing raw arg: " + arg ); + + boolean addedToBuffer = false; + + if ( arg.startsWith( "\"" ) ) + { + // if we're in the process of building up another arg, push it and start over. + // this is for the case: "-Dfoo=bar "-Dfoo2=bar two" (note the first unterminated quote) + if ( currentArg != null ) + { +// System.out.println( "Flushing last arg buffer: \'" + currentArg + "\' to cleaned list." ); + cleaned.add( currentArg.toString() ); + } + + // start building an argument here. + currentArg = new StringBuffer( arg.substring( 1 ) ); + addedToBuffer = true; + } + + // this has to be a separate "if" statement, to capture the case of: "-Dfoo=bar" + if ( arg.endsWith( "\"" ) ) + { + String cleanArgPart = arg.substring( 0, arg.length() - 1 ); + + // if we're building an argument, keep doing so. + if ( currentArg != null ) + { + // if this is the case of "-Dfoo=bar", then we need to adjust the buffer. + if ( addedToBuffer ) + { +// System.out.println( "Adjusting argument already appended to the arg buffer." ); + currentArg.setLength( currentArg.length() - 1 ); + } + // otherwise, we trim the trailing " and append to the buffer. + else + { +// System.out.println( "Appending arg part: \'" + cleanArgPart + "\' with preceding space to arg buffer." ); + // TODO: introducing a space here...not sure what else to do but collapse whitespace + currentArg.append( ' ' ).append( cleanArgPart ); + } + +// System.out.println( "Flushing completed arg buffer: \'" + currentArg + "\' to cleaned list." ); + + // we're done with this argument, so add it. + cleaned.add( currentArg.toString() ); + } + else + { +// System.out.println( "appending cleaned arg: \'" + cleanArgPart + "\' directly to cleaned list." ); + // this is a simple argument...just add it. + cleaned.add( cleanArgPart ); + } + +// System.out.println( "Clearing arg buffer." ); + // the currentArg MUST be finished when this completes. + currentArg = null; + continue; + } + + // if we haven't added this arg to the buffer, and we ARE building an argument + // buffer, then append it with a preceding space...again, not sure what else to + // do other than collapse whitespace. + // NOTE: The case of a trailing quote is handled by nullifying the arg buffer. + if ( !addedToBuffer ) + { + // append to the argument we're building, collapsing whitespace to a single space. + if ( currentArg != null ) + { +// System.out.println( "Append unquoted arg part: \'" + arg + "\' to arg buffer." ); + currentArg.append( ' ' ).append( arg ); + } + // this is a loner, just add it directly. + else + { +// System.out.println( "Append unquoted arg part: \'" + arg + "\' directly to cleaned list." ); + cleaned.add( arg ); + } + } + } + + // clean up. + if ( currentArg != null ) + { +// System.out.println( "Adding unterminated arg buffer: \'" + currentArg + "\' to cleaned list." ); + cleaned.add( currentArg.toString() ); + } + + int cleanedSz = cleaned.size(); + String[] cleanArgs = null; + + if ( cleanedSz == 0 ) + { + // if we didn't have any arguments to clean, simply pass the original array through + cleanArgs = args; + } + else + { +// System.out.println( "Cleaned argument list:\n" + cleaned ); + cleanArgs = (String[]) cleaned.toArray( new String[cleanedSz] ); + } + + return cleanArgs; } public void displayHelp()