Author: tchemit Date: 2009-09-25 23:40:16 +0200 (Fri, 25 Sep 2009) New Revision: 587 Modified: trunk/src/main/java/org/nuiton/plugin/CollectFilesMojo.java Log: finalize mojo, ready for release Modified: trunk/src/main/java/org/nuiton/plugin/CollectFilesMojo.java =================================================================== --- trunk/src/main/java/org/nuiton/plugin/CollectFilesMojo.java 2009-09-21 10:52:14 UTC (rev 586) +++ trunk/src/main/java/org/nuiton/plugin/CollectFilesMojo.java 2009-09-25 21:40:16 UTC (rev 587) @@ -20,16 +20,17 @@ */ package org.nuiton.plugin; +import java.io.BufferedReader; +import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; -import java.io.FileOutputStream; +import java.io.FileWriter; import java.io.IOException; +import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; -import java.util.Properties; import java.util.regex.Pattern; import org.apache.maven.artifact.Artifact; -import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; @@ -48,12 +49,6 @@ public class CollectFilesMojo extends AbstractMojo { /** - * @parameter expression="${session}" - * @required - * @readonly - */ - private MavenSession mavenSession; - /** * Dependance du projet. * * @parameter default-value="${project}" @@ -110,7 +105,7 @@ */ protected String outputDirectory; /** - * Properties file will all files collected indexed by their path. + * File with all files collected (one file by line in absolute path). * <p/> * <b>Note :</b> If not Set, will not generate the description file. * @@ -159,11 +154,12 @@ if (skip) { getLog().info("Skip flag in on, goal will not be executed"); + return; } List<File> files = getFiles(); if (files.isEmpty()) { - getLog().warn("No files to collect."); + getLog().warn("No file to collect."); return; } @@ -182,15 +178,17 @@ getLog().info("\n dryRun flag is on, no file will be copied!\n"); } else { - if (!output.exists()) { + if (copyFiles && !output.exists()) { output.mkdirs(); } } File description = null; - Properties p = new Properties(); + List<File> incomingFiles = null; + boolean withDescriptionFile = false; + if (descriptionFile != null && !descriptionFile.trim().isEmpty()) { description = new File(rootProject.getBasedir(), descriptionFile); @@ -200,11 +198,13 @@ if (description.exists()) { // reload existing try { - p.load(new FileInputStream(description)); - getLog().info("Load " + description); + incomingFiles = getFiles(description); + getLog().info("Loaded " + description); } catch (IOException ex) { throw new MojoExecutionException("could not load file " + description, ex); } + } else { + incomingFiles = new ArrayList<File>(); } } @@ -218,11 +218,15 @@ String absolutePath = f.getAbsolutePath(); String path = absolutePath.substring(basedirLength + 1); File dst = new File(output, f.getName()); - if (withDescriptionFile && copyFiles) { - p.put(path, dst.getAbsolutePath()); - } else { - p.put(path, f.getAbsolutePath()); + + if (withDescriptionFile) { + if (copyFiles) { + incomingFiles.add(dst); + } else { + incomingFiles.add(f); + } } + getLog().info("collected file " + path); if (!dryRun && copyFiles) { try { @@ -234,16 +238,54 @@ } } - if (description != null) { + if (!dryRun && withDescriptionFile) { try { - p.store(new FileOutputStream(description), "Generated by " + this.getClass()); - getLog().info("Save " + description); + setFiles(description, incomingFiles); + getLog().info("Saved " + description); } catch (IOException ex) { throw new MojoExecutionException("could not save file " + description, ex); } } } + /** + * Read a file containing on each line the path of a file. + * + * @param input the file containing the list of files + * @return the list of files read from the given file + * @throws IOException if any pb while reading file + */ + public static List<File> getFiles(File input) throws IOException { + List<File> result = new ArrayList<File>(); + BufferedReader stream = new BufferedReader(new InputStreamReader(new FileInputStream(input))); + while (stream.ready()) { + String line = stream.readLine().trim(); + if (!line.isEmpty()) { + File f = new File(line); + result.add(f); + } + } + return result; + } + + /** + * Save the list of files in the given output file. + * <p/> + * Each line is the absolute path of each files of the list + * + * @param output the file when to write + * @param files the files to store + * @throws IOException if any pb when writing file + */ + public static void setFiles(File output, List<File> files) throws IOException { + BufferedWriter writer = new BufferedWriter(new FileWriter(output)); + for (File f : files) { + writer.write(f.getAbsolutePath()); + writer.newLine(); + } + writer.close(); + } + protected List<File> getFiles() { Pattern includePattern = includes == null || includes.trim().isEmpty() ? null : Pattern.compile(includes.trim()); @@ -272,6 +314,7 @@ File basedir = project.getBasedir(); for (String path : extraFiles.split(",")) { + path = path.trim(); if (path.startsWith(basedir.getAbsolutePath())) { file = new File(path.trim()); } else { @@ -299,14 +342,7 @@ protected MavenProject getExecutionRootProject() { MavenProject root = project; - String executionRootDirectory = mavenSession.getExecutionRootDirectory(); - while (root != null) { - File basedir = root.getBasedir(); - getLog().debug("Current Folder:" + basedir); - boolean result = executionRootDirectory.equalsIgnoreCase(basedir.toString()); - if (result) { - break; - } + while (!root.isExecutionRoot()) { root = root.getParent(); } if (verbose) { @@ -320,7 +356,7 @@ return; } if (!f.exists()) { - getLog().warn("skip unexisting file " + f); + getLog().warn("skip unexisting file " + f + " (" + msg + ")"); return; } if (excludePattern != null) {
participants (1)
-
tchemit@users.nuiton.org