Skip to content

Commit

Permalink
fix #50 : provide a configuration "useCanonicalPath" to use Canonical…
Browse files Browse the repository at this point in the history
…Path (or not)
  • Loading branch information
davidB committed Aug 21, 2011
1 parent 5dbf3ea commit 0eff8a8
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 31 deletions.
11 changes: 9 additions & 2 deletions src/main/java/org_scala_tools_maven/AddSourceMojo.java
Expand Up @@ -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);
Expand Down
22 changes: 22 additions & 0 deletions 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();
}
}
2 changes: 1 addition & 1 deletion src/main/java/org_scala_tools_maven/ScalaCompileMojo.java
Expand Up @@ -49,7 +49,7 @@ public class ScalaCompileMojo extends ScalaCompilerSupport {
protected List<File> getSourceDirectories() throws Exception {
List<String> 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);
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org_scala_tools_maven/ScalaCompilerSupport.java
Expand Up @@ -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;

Expand Down Expand Up @@ -60,6 +59,7 @@ public abstract class ScalaCompilerSupport extends ScalaSourceMojoSupport {
*/
private boolean notifyCompilation = true;


abstract protected File getOutputDir() throws Exception;

abstract protected List<String> getClasspathElements() throws Exception;
Expand All @@ -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);
Expand Down Expand Up @@ -193,7 +193,7 @@ protected List<File> getFilesToCompile(List<File> sourceRootDirs, long lastSucce
private void notifyCompilation(List<File> 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)));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org_scala_tools_maven/ScalaConsoleMojo.java
Expand Up @@ -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());
}
Expand Down
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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");
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org_scala_tools_maven/ScalaDocMojo.java
Expand Up @@ -200,7 +200,7 @@ public class ScalaDocMojo extends ScalaSourceMojoSupport implements MavenReport
protected List<File> getSourceDirectories() throws Exception {
List<String> 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);
}
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org_scala_tools_maven/ScalaGenJsonMojo.java
Expand Up @@ -142,7 +142,7 @@ public ScalaGenJsonMojo() throws Exception {
protected List<File> getSourceDirectories() throws Exception {
List<String> 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);
}
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/org_scala_tools_maven/ScalaMojoSupport.java
Expand Up @@ -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.
*
Expand Down Expand Up @@ -314,10 +322,9 @@ public void addToClasspath(String groupId, String artifactId, String version, Se

protected void addToClasspath(Artifact artifact, Set<String> 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);
}
}
Expand Down
16 changes: 3 additions & 13 deletions src/main/java/org_scala_tools_maven/ScalaSourceMojoSupport.java
Expand Up @@ -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;
Expand Down Expand Up @@ -103,7 +102,7 @@ protected List<File> findSourceWithFilters(List<File> 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);
}
}
Expand All @@ -113,24 +112,15 @@ protected List<File> findSourceWithFilters(List<File> 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<File> normalize(List<String> compileSourceRootsList) {
protected List<File> normalize(List<String> compileSourceRootsList) throws Exception {
List<File> newCompileSourceRootsList = new ArrayList<File>();
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);
}
Expand Down

0 comments on commit 0eff8a8

Please sign in to comment.