From 0eff8a88b1c26cfab69ceb1a37f4f6d4eed6a265 Mon Sep 17 00:00:00 2001 From: David Bernard Date: Sun, 21 Aug 2011 20:11:07 +0200 Subject: [PATCH] fix #50 : provide a configuration "useCanonicalPath" to use CanonicalPath (or not) --- .../org_scala_tools_maven/AddSourceMojo.java | 11 ++++++++-- .../java/org_scala_tools_maven/FileUtils.java | 22 +++++++++++++++++++ .../ScalaCompileMojo.java | 2 +- .../ScalaCompilerSupport.java | 8 +++---- .../ScalaConsoleMojo.java | 2 +- .../ScalaContinuousCompileMojo.java | 9 ++++---- .../org_scala_tools_maven/ScalaDocMojo.java | 4 ++-- .../ScalaGenJsonMojo.java | 2 +- .../ScalaMojoSupport.java | 11 ++++++++-- .../ScalaSourceMojoSupport.java | 16 +++----------- 10 files changed, 56 insertions(+), 31 deletions(-) create mode 100644 src/main/java/org_scala_tools_maven/FileUtils.java diff --git a/src/main/java/org_scala_tools_maven/AddSourceMojo.java b/src/main/java/org_scala_tools_maven/AddSourceMojo.java index d4b0d386..a3baf2f3 100644 --- a/src/main/java/org_scala_tools_maven/AddSourceMojo.java +++ b/src/main/java/org_scala_tools_maven/AddSourceMojo.java @@ -37,17 +37,24 @@ public class AddSourceMojo extends AbstractMojo { */ protected File testSourceDir; + /** + * Should use CanonicalPath to normalize path (true => getCanonicalPath, false => getAbsolutePath) + * @see https://github.com/davidB/maven-scala-plugin/issues/50 + * @parameter expression="${maven.scala.useCanonicalPath}" default-value="true" + */ + protected boolean useCanonicalPath = true; + public void execute() throws MojoExecutionException { try { if (sourceDir != null) { - String path = sourceDir.getCanonicalPath(); + String path = FileUtils.pathOf(sourceDir, useCanonicalPath); if (!project.getCompileSourceRoots().contains(path)) { getLog().info("Add Source directory: " + path); project.addCompileSourceRoot(path); } } if (testSourceDir != null) { - String path = testSourceDir.getCanonicalPath(); + String path = FileUtils.pathOf(testSourceDir, useCanonicalPath); if (!project.getTestCompileSourceRoots().contains(path)) { getLog().info("Add Test Source directory: " + path); project.addTestCompileSourceRoot(path); diff --git a/src/main/java/org_scala_tools_maven/FileUtils.java b/src/main/java/org_scala_tools_maven/FileUtils.java new file mode 100644 index 00000000..fb493666 --- /dev/null +++ b/src/main/java/org_scala_tools_maven/FileUtils.java @@ -0,0 +1,22 @@ +package org_scala_tools_maven; + +import java.io.File; + +class FileUtils extends org.codehaus.plexus.util.FileUtils { + + /** + * @param canonical Should use CanonicalPath to normalize path (true => getCanonicalPath, false => getAbsolutePath) + * @see https://github.com/davidB/maven-scala-plugin/issues/50 + */ + public static String pathOf(File f, boolean canonical) throws Exception { + return canonical? f.getCanonicalPath() : f.getAbsolutePath(); + } + + /** + * @param canonical Should use CanonicalPath to normalize path (true => getCanonicalPath, false => getAbsolutePath) + * @see https://github.com/davidB/maven-scala-plugin/issues/50 + */ + public static File fileOf(File f, boolean canonical) throws Exception { + return canonical? f.getCanonicalFile() : f.getAbsoluteFile(); + } +} \ No newline at end of file diff --git a/src/main/java/org_scala_tools_maven/ScalaCompileMojo.java b/src/main/java/org_scala_tools_maven/ScalaCompileMojo.java index fe9973f5..7384774b 100644 --- a/src/main/java/org_scala_tools_maven/ScalaCompileMojo.java +++ b/src/main/java/org_scala_tools_maven/ScalaCompileMojo.java @@ -49,7 +49,7 @@ public class ScalaCompileMojo extends ScalaCompilerSupport { protected List getSourceDirectories() throws Exception { List sources = project.getCompileSourceRoots(); //Quick fix in case the user has not added the "add-source" goal. - String scalaSourceDir = sourceDir.getCanonicalPath(); + String scalaSourceDir = FileUtils.pathOf(sourceDir, useCanonicalPath); if(!sources.contains(scalaSourceDir)) { sources.add(scalaSourceDir); } diff --git a/src/main/java/org_scala_tools_maven/ScalaCompilerSupport.java b/src/main/java/org_scala_tools_maven/ScalaCompilerSupport.java index 92551ca2..526b1c69 100644 --- a/src/main/java/org_scala_tools_maven/ScalaCompilerSupport.java +++ b/src/main/java/org_scala_tools_maven/ScalaCompilerSupport.java @@ -20,7 +20,6 @@ import java.util.Arrays; import java.util.List; -import org.codehaus.plexus.util.FileUtils; import org_scala_tools_maven_executions.JavaMainCaller; import org_scala_tools_maven_executions.MainHelper; @@ -60,6 +59,7 @@ public abstract class ScalaCompilerSupport extends ScalaSourceMojoSupport { */ private boolean notifyCompilation = true; + abstract protected File getOutputDir() throws Exception; abstract protected List getClasspathElements() throws Exception; @@ -68,13 +68,13 @@ public abstract class ScalaCompilerSupport extends ScalaSourceMojoSupport { @Override protected void doExecute() throws Exception { - File outputDir = normalize(getOutputDir()); + File outputDir = FileUtils.fileOf(getOutputDir(), useCanonicalPath); if (!outputDir.exists()) { outputDir.mkdirs(); } if (getLog().isDebugEnabled()) { for(File directory : getSourceDirectories()) { - getLog().debug(directory.getCanonicalPath()); + getLog().debug(FileUtils.pathOf(directory, useCanonicalPath)); } } int nbFiles = compile(getSourceDirectories(), outputDir, getClasspathElements(), false); @@ -193,7 +193,7 @@ protected List getFilesToCompile(List sourceRootDirs, long lastSucce private void notifyCompilation(List files) throws Exception { if (notifyCompilation) { for (File f : files) { - getLog().info(String.format("%s:-1: info: compiling", f.getCanonicalPath())); + getLog().info(String.format("%s:-1: info: compiling", FileUtils.pathOf(f, useCanonicalPath))); } } } diff --git a/src/main/java/org_scala_tools_maven/ScalaConsoleMojo.java b/src/main/java/org_scala_tools_maven/ScalaConsoleMojo.java index 8be0f670..e59c7c80 100644 --- a/src/main/java/org_scala_tools_maven/ScalaConsoleMojo.java +++ b/src/main/java/org_scala_tools_maven/ScalaConsoleMojo.java @@ -109,7 +109,7 @@ protected void doExecute() throws Exception { addCompilerPluginOptions(jcmd); if (javaRebelPath != null) { if (!javaRebelPath.exists()) { - getLog().warn("javaRevelPath '"+javaRebelPath.getCanonicalPath()+"' not found"); + getLog().warn("javaRevelPath '"+ javaRebelPath.getCanonicalPath()+"' not found"); } else { jcmd.addJvmArgs("-noverify", "-javaagent:" + javaRebelPath.getCanonicalPath()); } diff --git a/src/main/java/org_scala_tools_maven/ScalaContinuousCompileMojo.java b/src/main/java/org_scala_tools_maven/ScalaContinuousCompileMojo.java index 8f1c891e..2bb51375 100644 --- a/src/main/java/org_scala_tools_maven/ScalaContinuousCompileMojo.java +++ b/src/main/java/org_scala_tools_maven/ScalaContinuousCompileMojo.java @@ -18,7 +18,6 @@ import java.io.File; import java.util.List; -import org.codehaus.plexus.util.FileUtils; import org_scala_tools_maven_executions.JavaMainCaller; /** @@ -105,17 +104,17 @@ protected JavaMainCaller getScalaCommand() throws Exception { @Override protected final void doExecute() throws Exception { - mainOutputDir = normalize(mainOutputDir); + mainOutputDir = FileUtils.fileOf(mainOutputDir, useCanonicalPath); if (!mainOutputDir.exists()) { mainOutputDir.mkdirs(); } - mainSourceDir = normalize(mainSourceDir); + mainSourceDir = FileUtils.fileOf(mainSourceDir, useCanonicalPath); - testOutputDir = normalize(testOutputDir); + testOutputDir = FileUtils.fileOf(testOutputDir, useCanonicalPath); if (!testOutputDir.exists()) { testOutputDir.mkdirs(); } - testSourceDir = normalize(testSourceDir); + testSourceDir = FileUtils.fileOf(testSourceDir, useCanonicalPath); if (useFsc) { getLog().info("use fsc for compilation"); diff --git a/src/main/java/org_scala_tools_maven/ScalaDocMojo.java b/src/main/java/org_scala_tools_maven/ScalaDocMojo.java index 73a3905a..4aeb6147 100644 --- a/src/main/java/org_scala_tools_maven/ScalaDocMojo.java +++ b/src/main/java/org_scala_tools_maven/ScalaDocMojo.java @@ -200,7 +200,7 @@ public class ScalaDocMojo extends ScalaSourceMojoSupport implements MavenReport protected List getSourceDirectories() throws Exception { List sources = project.getCompileSourceRoots(); //Quick fix in case the user has not added the "add-source" goal. - String scalaSourceDir = sourceDir.getCanonicalPath(); + String scalaSourceDir = FileUtils.pathOf(sourceDir, useCanonicalPath); if(!sources.contains(scalaSourceDir)) { sources.add(scalaSourceDir); } @@ -353,7 +353,7 @@ public void generate(@SuppressWarnings("unused") Sink sink, @SuppressWarnings("u JavaMainCaller jcmd = getScalaCommand(); jcmd.addOption("-d", reportOutputDir.getAbsolutePath()); for (File x : sources) { - jcmd.addArgs(x.getCanonicalPath()); + jcmd.addArgs(FileUtils.pathOf(x, useCanonicalPath)); } jcmd.run(displayCmd); } diff --git a/src/main/java/org_scala_tools_maven/ScalaGenJsonMojo.java b/src/main/java/org_scala_tools_maven/ScalaGenJsonMojo.java index 3d1b6e38..52673fce 100644 --- a/src/main/java/org_scala_tools_maven/ScalaGenJsonMojo.java +++ b/src/main/java/org_scala_tools_maven/ScalaGenJsonMojo.java @@ -142,7 +142,7 @@ public ScalaGenJsonMojo() throws Exception { protected List getSourceDirectories() throws Exception { List sources = project.getCompileSourceRoots(); // Quick fix in case the user has not added the "add-source" goal. - String scalaSourceDir = sourceDir.getCanonicalPath(); + String scalaSourceDir = FileUtils.pathOf(sourceDir, useCanonicalPath); if (!sources.contains(scalaSourceDir)) { sources.add(scalaSourceDir); } diff --git a/src/main/java/org_scala_tools_maven/ScalaMojoSupport.java b/src/main/java/org_scala_tools_maven/ScalaMojoSupport.java index 1d0360d5..aee085a8 100644 --- a/src/main/java/org_scala_tools_maven/ScalaMojoSupport.java +++ b/src/main/java/org_scala_tools_maven/ScalaMojoSupport.java @@ -202,6 +202,14 @@ public abstract class ScalaMojoSupport extends AbstractMojo { * @parameter default-value="false" */ protected boolean failOnMultipleScalaVersions = false; + + /** + * Should use CanonicalPath to normalize path (true => getCanonicalPath, false => getAbsolutePath) + * @see https://github.com/davidB/maven-scala-plugin/issues/50 + * @parameter expression="${maven.scala.useCanonicalPath}" default-value="true" + */ + protected boolean useCanonicalPath = true; + /** * Artifact factory, needed to download source jars. * @@ -314,10 +322,9 @@ public void addToClasspath(String groupId, String artifactId, String version, Se protected void addToClasspath(Artifact artifact, Set classpath, boolean addDependencies) throws Exception { resolver.resolve(artifact, remoteRepos, localRepo); - classpath.add(artifact.getFile().getCanonicalPath()); + classpath.add(FileUtils.pathOf(artifact.getFile(), useCanonicalPath)); if (addDependencies) { for (Artifact dep : resolveArtifactDependencies(artifact)) { - //classpath.add(dep.getFile().getCanonicalPath()); addToClasspath(dep, classpath, addDependencies); } } diff --git a/src/main/java/org_scala_tools_maven/ScalaSourceMojoSupport.java b/src/main/java/org_scala_tools_maven/ScalaSourceMojoSupport.java index 799a9038..ca03c8a8 100644 --- a/src/main/java/org_scala_tools_maven/ScalaSourceMojoSupport.java +++ b/src/main/java/org_scala_tools_maven/ScalaSourceMojoSupport.java @@ -4,7 +4,6 @@ package org_scala_tools_maven; import java.io.File; -import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; @@ -103,7 +102,7 @@ protected List findSourceWithFilters(List sourceRootDirs) throws Exc for (File dir : sourceRootDirs) { String[] tmpFiles = MainHelper.findFiles(dir, includes.toArray(new String[includes.size()]), excludes.toArray(new String[excludes.size()])); for (String tmpLocalFile : tmpFiles) { - File tmpAbsFile = normalize(new File(dir, tmpLocalFile)); + File tmpAbsFile = FileUtils.fileOf(new File(dir, tmpLocalFile), useCanonicalPath); sourceFiles.add(tmpAbsFile); } } @@ -113,24 +112,15 @@ protected List findSourceWithFilters(List sourceRootDirs) throws Exc return sourceFiles; } - protected File normalize(File f) { - try { - f = f.getCanonicalFile(); - } catch (IOException exc) { - f = f.getAbsoluteFile(); - } - return f; - } - /** * This limits the source directories to only those that exist for real. */ - protected List normalize(List compileSourceRootsList) { + protected List normalize(List compileSourceRootsList) throws Exception { List newCompileSourceRootsList = new ArrayList(); if (compileSourceRootsList != null) { // copy as I may be modifying it for (String srcDir : compileSourceRootsList) { - File srcDirFile = normalize(new File(srcDir)); + File srcDirFile = FileUtils.fileOf(new File(srcDir), useCanonicalPath); if (!newCompileSourceRootsList.contains(srcDirFile) && srcDirFile.exists()) { newCompileSourceRootsList.add(srcDirFile); }