You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by br...@apache.org on 2007/07/09 01:03:42 UTC

svn commit: r554463 - in /maven/shared/trunk/maven-invoker/src: main/java/org/apache/maven/shared/invoker/MavenCommandLineBuilder.java test/java/org/apache/maven/shared/invoker/MavenCommandLineBuilderTest.java

Author: brianf
Date: Sun Jul  8 16:03:41 2007
New Revision: 554463

URL: http://svn.apache.org/viewvc?view=rev&rev=554463
Log:
MNG-3096: wrapped paths and properties containing spaces with quotes

Modified:
    maven/shared/trunk/maven-invoker/src/main/java/org/apache/maven/shared/invoker/MavenCommandLineBuilder.java
    maven/shared/trunk/maven-invoker/src/test/java/org/apache/maven/shared/invoker/MavenCommandLineBuilderTest.java

Modified: maven/shared/trunk/maven-invoker/src/main/java/org/apache/maven/shared/invoker/MavenCommandLineBuilder.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-invoker/src/main/java/org/apache/maven/shared/invoker/MavenCommandLineBuilder.java?view=diff&rev=554463&r1=554462&r2=554463
==============================================================================
--- maven/shared/trunk/maven-invoker/src/main/java/org/apache/maven/shared/invoker/MavenCommandLineBuilder.java (original)
+++ maven/shared/trunk/maven-invoker/src/main/java/org/apache/maven/shared/invoker/MavenCommandLineBuilder.java Sun Jul  8 16:03:41 2007
@@ -39,10 +39,12 @@
         // handling for OS-level envars
         setShellEnvironment( request, cli );
 
-        // interactive, offline, update-snapshots, debug/show-errors, checksum policy
+        // interactive, offline, update-snapshots,
+        // debug/show-errors, checksum policy
         setFlags( request, cli );
 
-        // failure behavior and [eventually] forced-reactor includes/excludes, etc.
+        // failure behavior and [eventually] forced-reactor
+        // includes/excludes, etc.
         setReactorBehavior( request, cli );
 
         // working directory and local repository location
@@ -93,7 +95,7 @@
             }
 
             cli.createArg().setValue( "-s" );
-            cli.createArg().setValue( userSettingsFile.getPath() );
+            cli.createArg().setValue( wrapStringWithQuotes( userSettingsFile.getPath() ) );
         }
     }
 
@@ -154,7 +156,7 @@
                 String key = (String) entry.getKey();
                 String value = (String) entry.getValue();
 
-                cli.createArg().setValue( "-D" + key + "=" + value );
+                cli.createArg().setValue( "-D" + wrapStringWithQuotes( key ) + "=" + wrapStringWithQuotes(value) );
             }
         }
     }
@@ -213,8 +215,7 @@
                 logger
                     .debug( "Specified POM file is not named \'pom.xml\'. Using the \'-f\' command-line option to accommodate non-standard filename..." );
 
-                // FIXME: Handle quotes in localRepo directory path...
-                cli.createArgument().setLine( "-f " + pom.getName() );
+                cli.createArgument().setLine( "-f " + wrapStringWithQuotes(pom.getName()) );
             }
         }
     }
@@ -281,7 +282,7 @@
                     + "\' is NOT a directory." );
             }
 
-            cli.createArg().setValue( "-Dmaven.repo.local=" + localRepositoryDirectory.getPath() );
+            cli.createArg().setValue( "-Dmaven.repo.local=" + wrapStringWithQuotes(localRepositoryDirectory.getPath()) );
         }
     }
 
@@ -354,18 +355,16 @@
         if ( mavenHome == null )
         {
             String mavenHomeProperty = System.getProperty( "maven.home" );
-
             if ( mavenHomeProperty != null )
             {
                 mavenHome = new File( mavenHomeProperty );
-
                 if ( !mavenHome.isDirectory() )
                 {
                     File binDir = mavenHome.getParentFile();
-
                     if ( "bin".equals( binDir.getName() ) )
                     {
-                        // ah, they specified the mvn executable instead...
+                        // ah, they specified the mvn
+                        // executable instead...
                         mavenHome = binDir.getParentFile();
                     }
                     else
@@ -412,6 +411,25 @@
         }
 
         return mvnCommand;
+    }
+
+    /**
+     * Wraps a path with quotes to handle paths with spaces. If no spaces are found,
+     * the original string is returned.
+     * 
+     * @param path string to wrap if containing spaces
+     * @return quote wrapped string
+     */
+    public String wrapStringWithQuotes( String path )
+    {
+        if (path.contains( " " ))
+        {
+            return "\"" + path + "\"";
+        }
+        else
+        {
+            return path;
+        }
     }
 
     public File getLocalRepositoryDirectory()

Modified: maven/shared/trunk/maven-invoker/src/test/java/org/apache/maven/shared/invoker/MavenCommandLineBuilderTest.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-invoker/src/test/java/org/apache/maven/shared/invoker/MavenCommandLineBuilderTest.java?view=diff&rev=554463&r1=554462&r2=554463
==============================================================================
--- maven/shared/trunk/maven-invoker/src/test/java/org/apache/maven/shared/invoker/MavenCommandLineBuilderTest.java (original)
+++ maven/shared/trunk/maven-invoker/src/test/java/org/apache/maven/shared/invoker/MavenCommandLineBuilderTest.java Sun Jul  8 16:03:41 2007
@@ -25,6 +25,19 @@
 
     private Properties sysProps;
 
+    public void testWrapwithQuotes()
+    {
+        TestCommandLineBuilder tcb = new TestCommandLineBuilder();
+        String test = "noSpacesInHere";
+        
+        assertSame( test, tcb.wrapStringWithQuotes( test ) );
+        assertEquals("noSpacesInHere",tcb.wrapStringWithQuotes( test ));
+        
+        test = "bunch of spaces in here";
+        assertNotSame( test, tcb.wrapStringWithQuotes( test ) );
+        assertEquals("\"bunch of spaces in here\"",tcb.wrapStringWithQuotes( test ));
+        
+    }
     public void testShouldFailToSetLocalRepoLocationGloballyWhenItIsAFile()
         throws IOException
     {
@@ -91,7 +104,7 @@
 
         tcb.setEnvironmentPaths( new DefaultInvocationRequest(), cli );
 
-        assertArgumentsPresent( Collections.singleton( "-Dmaven.repo.local=" + lrd.getPath() ), cli );
+        assertArgumentsPresent( Collections.singleton( "-Dmaven.repo.local=" + tcb.wrapStringWithQuotes( lrd.getPath()) ), cli );
     }
 
     public void testShouldSetLocalRepoLocationFromRequest()
@@ -112,7 +125,7 @@
 
         tcb.setEnvironmentPaths( new DefaultInvocationRequest().setLocalRepositoryDirectory( lrd ), cli );
 
-        assertArgumentsPresent( Collections.singleton( "-Dmaven.repo.local=" + lrd.getPath() ), cli );
+        assertArgumentsPresent( Collections.singleton( "-Dmaven.repo.local=" + tcb.wrapStringWithQuotes(lrd.getPath()) ), cli );
     }
 
     public void testRequestProvidedLocalRepoLocationShouldOverrideGlobal()
@@ -138,7 +151,7 @@
 
         tcb.setEnvironmentPaths( new DefaultInvocationRequest().setLocalRepositoryDirectory( lrd ), cli );
 
-        assertArgumentsPresent( Collections.singleton( "-Dmaven.repo.local=" + lrd.getPath() ), cli );
+        assertArgumentsPresent( Collections.singleton( "-Dmaven.repo.local=" + tcb.wrapStringWithQuotes( lrd.getPath()) ), cli );
     }
 
     public void testShouldSetWorkingDirectoryGlobally()
@@ -677,7 +690,7 @@
 
         Set args = new HashSet();
         args.add( "-s" );
-        args.add( settingsFile.getCanonicalPath() );
+        args.add( tcb.wrapStringWithQuotes( settingsFile.getCanonicalPath()) );
 
         assertArgumentsPresent( args, cli );
     }
@@ -711,7 +724,7 @@
         TestCommandLineBuilder tcb = new TestCommandLineBuilder();
         tcb.setProperties( new DefaultInvocationRequest().setProperties( properties ), cli );
 
-        assertArgumentsPresent( Collections.singleton( "-Dkey=value with spaces" ), cli );
+        assertArgumentsPresent( Collections.singleton( "-Dkey=\"value with spaces\"" ), cli );
     }
 
     public void testShouldSpecifyCustomPropertyWithSpacesInKeyFromRequest()
@@ -727,7 +740,7 @@
         TestCommandLineBuilder tcb = new TestCommandLineBuilder();
         tcb.setProperties( new DefaultInvocationRequest().setProperties( properties ), cli );
 
-        assertArgumentsPresent( Collections.singleton( "-Dkey with spaces=value with spaces" ), cli );
+        assertArgumentsPresent( Collections.singleton( "-D\"key with spaces\"=\"value with spaces\"" ), cli );
     }
 
     public void testShouldSpecifySingleGoalFromRequest()