diff --git a/core/src/main/java/hudson/util/IOUtils.java b/core/src/main/java/hudson/util/IOUtils.java index a5275232c8b60f64d204666a950ebae91662875e..475b20f7bc52ae4ed1c20bd82513b7070a758388 100644 --- a/core/src/main/java/hudson/util/IOUtils.java +++ b/core/src/main/java/hudson/util/IOUtils.java @@ -1,11 +1,7 @@ package hudson.util; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; +import java.io.*; +import java.util.regex.Pattern; /** * Adds more to commons-io. @@ -72,4 +68,30 @@ public class IOUtils extends org.apache.commons.io.IOUtils { size -= in.skip(size); return in; } + + /** + * Resolves the given path with respect to given base. If the path represents an absolute path, a file representing + * it is returned, otherwise a file representing a path relative to base is returned. + *

+ * It would be nice if File#File(File, String) were doing this. + * @param base File that represents the parent, may be null if path is absolute + * @param path Path of the file, may not be null + * @return new File(name) if name represents an absolute path, new File(base, name) otherwise + * @see hudson.FilePath#absolutize() + */ + public static File absolutize(File base, String path) { + if (isAbsolute(path)) + return new File(path); + return new File(base, path); + } + + /** + * See {@link hudson.FilePath#isAbsolute(String)}. + * @param path String representing Platform Specific (unlike FilePath, which may get Platform agnostic paths), may not be null + * @return true if String represents absolute path on this platform, false otherwise + */ + public static boolean isAbsolute(String path) { + Pattern DRIVE_PATTERN = Pattern.compile("[A-Za-z]:[\\\\/].*"); + return path.startsWith("/") || DRIVE_PATTERN.matcher(path).matches(); + } } diff --git a/maven-plugin/src/main/java/hudson/maven/MavenModuleSet.java b/maven-plugin/src/main/java/hudson/maven/MavenModuleSet.java index 46881fb3abe3dfdfa051ed4408d60567a8547a82..2d03edcbab626e11b6a82f11fbd9655efc28250d 100644 --- a/maven-plugin/src/main/java/hudson/maven/MavenModuleSet.java +++ b/maven-plugin/src/main/java/hudson/maven/MavenModuleSet.java @@ -26,30 +26,30 @@ package hudson.maven; import hudson.*; import hudson.model.*; import hudson.model.Descriptor.FormException; -import static hudson.model.ItemGroupMixIn.loadChildren; import hudson.model.Queue; import hudson.model.Queue.Task; import hudson.search.CollectionSearchIndex; import hudson.search.SearchIndexBuilder; -import hudson.tasks.Maven.MavenInstallation; import hudson.tasks.*; +import hudson.tasks.Maven.MavenInstallation; import hudson.tasks.junit.JUnitResultArchiver; -import static hudson.Util.fixEmpty; import hudson.util.CopyOnWriteMap; import hudson.util.DescribableList; -import hudson.util.Function1; import hudson.util.FormValidation; +import hudson.util.Function1; +import net.sf.json.JSONObject; +import org.kohsuke.stapler.QueryParameter; import org.kohsuke.stapler.StaplerRequest; import org.kohsuke.stapler.StaplerResponse; -import org.kohsuke.stapler.QueryParameter; +import org.kohsuke.stapler.export.Exported; import javax.servlet.ServletException; import java.io.File; import java.io.IOException; import java.util.*; -import net.sf.json.JSONObject; -import org.kohsuke.stapler.export.Exported; +import static hudson.Util.fixEmpty; +import static hudson.model.ItemGroupMixIn.loadChildren; /** * Group of {@link MavenModule}s. @@ -198,8 +198,10 @@ public final class MavenModuleSet extends AbstractMavenProject invoke(File ws, VirtualChannel channel) throws IOException { - File pom = new File(ws,rootPOM); + File pom; PrintStream logger = listener.getLogger(); - // choice of module root ('ws' in this method) is somewhat arbitrary - // when multiple CVS/SVN modules are checked out, so also check - // the path against the workspace root if that seems like what the user meant (see issue #1293) - File parentLoc = new File(ws.getParentFile(),rootPOM); - if(!pom.exists() && parentLoc.exists()) - pom = parentLoc; + if (IOUtils.isAbsolute(rootPOM)) { + pom = new File(rootPOM); + } else { + // choice of module root ('ws' in this method) is somewhat arbitrary + // when multiple CVS/SVN modules are checked out, so also check + // the path against the workspace root if that seems like what the user meant (see issue #1293) + pom = new File(ws, rootPOM); + File parentLoc = new File(ws.getParentFile(),rootPOM); + if(!pom.exists() && parentLoc.exists()) + pom = parentLoc; + } if(!pom.exists()) throw new AbortException(Messages.MavenModuleSetBuild_NoSuchPOMFile(pom)); diff --git a/test/src/test/java/hudson/maven/MavenProjectTest.java b/test/src/test/java/hudson/maven/MavenProjectTest.java index 50a60a324a273ff8e3b72f6bc7c7e92dcebc4327..cb871200faafff5fe0be17fdae9b5b5479936f3e 100755 --- a/test/src/test/java/hudson/maven/MavenProjectTest.java +++ b/test/src/test/java/hudson/maven/MavenProjectTest.java @@ -25,11 +25,11 @@ package hudson.maven; import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; import hudson.tasks.Maven.MavenInstallation; +import org.jvnet.hudson.test.Bug; import org.jvnet.hudson.test.ExtractResourceSCM; import org.jvnet.hudson.test.HudsonTestCase; -import org.jvnet.hudson.test.Bug; -import java.io.IOException; +import java.io.File; /** * @author huybrechts @@ -47,7 +47,7 @@ public class MavenProjectTest extends HudsonTestCase { return createProject("/simple-projects.zip"); } - private MavenModuleSet createProject(final String scmResource) throws IOException, Exception { + private MavenModuleSet createProject(final String scmResource) throws Exception { MavenModuleSet project = createMavenProject(); MavenInstallation mi = configureDefaultMaven(); project.setScm(new ExtractResourceSCM(getClass().getResource( @@ -132,4 +132,14 @@ public class MavenProjectTest extends HudsonTestCase { assertEquals(1, project.getModule("org.jvnet.hudson.main.test.multimod:moduleA").getBuilds().size()); assertEquals(1, project.getModule("org.jvnet.hudson.main.test.multimod:moduleB").getBuilds().size()); } + @Bug(7261) + public void testAbsolutePathPom() throws Exception { + File pom = new File(this.getClass().getResource("test-pom-7162.xml").toURI()); + MavenModuleSet project = createMavenProject(); + MavenInstallation mi = configureDefaultMaven(); + project.setMaven(mi.getName()); + project.setRootPOM(pom.getAbsolutePath()); + project.setGoals("install"); + buildAndAssertSuccess(project); + } } diff --git a/test/src/test/resources/hudson/maven/test-pom-7162.xml b/test/src/test/resources/hudson/maven/test-pom-7162.xml new file mode 100644 index 0000000000000000000000000000000000000000..2b6f1903981d39608bbc65bccb40ccd28da4b9e7 --- /dev/null +++ b/test/src/test/resources/hudson/maven/test-pom-7162.xml @@ -0,0 +1,26 @@ + + 4.0.0 + + com.infradna.support + query + 1.0-SNAPSHOT + jar + + query + http://maven.apache.org + + + UTF-8 + bar + + + + + junit + junit + 3.8.1 + test + + +