</dependencies>
<build>
<plugins>
- <!--
- <plugin>
- <artifactId>maven-jar-plugin</artifactId>
- <executions>
- <execution>
- <id>test-jar</id>
- <goals>
- <goal>test-jar</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- -->
<plugin>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-maven-plugin</artifactId>
- <!--
- <executions>
- <execution>
- <id>merge</id>
- <goals>
- <goal>merge-descriptors</goal>
- </goals>
- <configuration>
- <descriptors>
- <descriptor>${basedir}/src/main/resources/META-INF/plexus/components.xml</descriptor>
- <descriptor>${project.build.directory}/generated-resources/plexus/META-INF/plexus/components.xml</descriptor>
- </descriptors>
- </configuration>
- </execution>
- </executions>
- -->
</plugin>
</plugins>
</build>
* under the License.
*/
+import org.apache.commons.lang.StringUtils;
+
import java.io.File;
+import java.net.MalformedURLException;
/**
* PathUtil - simple utility methods for path manipulation.
*/
public class PathUtil
{
+ public static String toUrl( String path )
+ {
+ // Is our work already done for us?
+ if ( path.startsWith( "file:/" ) )
+ {
+ return path;
+ }
+
+ return toUrl( new File( path ) );
+ }
+
+ public static String toUrl( File file )
+ {
+ try
+ {
+ return file.toURL().toExternalForm();
+ }
+ catch ( MalformedURLException e )
+ {
+ String pathCorrected = StringUtils.replaceChars( file.getAbsolutePath(), '\\', '/' );
+ if ( pathCorrected.startsWith( "file:/" ) )
+ {
+ return pathCorrected;
+ }
+
+ return "file://" + pathCorrected;
+ }
+ }
+
public static String getRelative( String basedir, File file )
{
return getRelative( basedir, file.getAbsolutePath() );
* under the License.
*/
-import org.apache.maven.archiva.common.utils.PathUtil;
+import org.apache.commons.lang.StringUtils;
+
+import java.io.File;
import junit.framework.TestCase;
assertEquals( "path/to/resource.xml", PathUtil.getRelative( "/home/user/foo/repository",
"/home/user/foo/repository/path/to/resource.xml" ) );
}
+
+ public void testToUrlRelativePath()
+ {
+ File workingDir = new File( "." );
+
+ String workingDirname = StringUtils.replaceChars( workingDir.getAbsolutePath(), '\\', '/' );
+
+ // Some JVM's retain the "." at the end of the path. Drop it.
+ if ( workingDirname.endsWith( "/." ) )
+ {
+ workingDirname = workingDirname.substring( 0, workingDirname.length() - 2 );
+ }
+
+ String path = "path/to/resource.xml";
+ String expectedPath = "file:" + workingDirname + "/" + path;
+
+ assertEquals( expectedPath, PathUtil.toUrl( path ) );
+ }
+
+ public void testToUrlUsingFileUrl()
+ {
+ File workingDir = new File( "." );
+
+ String workingDirname = StringUtils.replaceChars( workingDir.getAbsolutePath(), '\\', '/' );
+
+ // Some JVM's retain the "." at the end of the path. Drop it.
+ if ( workingDirname.endsWith( "/." ) )
+ {
+ workingDirname = workingDirname.substring( 0, workingDirname.length() - 2 );
+ }
+
+ String path = "path/to/resource.xml";
+ String expectedPath = "file:" + workingDirname + "/" + path;
+
+ assertEquals( expectedPath, PathUtil.toUrl( expectedPath ) );
+ }
}