Author: tchemit Date: 2009-12-28 10:16:26 +0100 (Mon, 28 Dec 2009) New Revision: 639 Modified: trunk/src/main/java/org/nuiton/plugin/AbstractPlugin.java trunk/src/main/java/org/nuiton/plugin/PluginHelper.java Log: add some usefull methods for io operations on files + reformat Modified: trunk/src/main/java/org/nuiton/plugin/AbstractPlugin.java =================================================================== --- trunk/src/main/java/org/nuiton/plugin/AbstractPlugin.java 2009-11-26 13:56:56 UTC (rev 638) +++ trunk/src/main/java/org/nuiton/plugin/AbstractPlugin.java 2009-12-28 09:16:26 UTC (rev 639) @@ -20,10 +20,14 @@ */ package org.nuiton.plugin; +import org.apache.maven.artifact.Artifact; +import org.apache.maven.model.Resource; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.project.MavenProject; +import org.codehaus.plexus.util.DirectoryScanner; +import org.nuiton.io.MirroredFileUpdater; import java.io.File; import java.io.IOException; @@ -31,16 +35,7 @@ import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; -import java.util.ArrayList; -import java.util.Date; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import org.apache.maven.artifact.Artifact; -import org.apache.maven.model.Resource; -import org.codehaus.plexus.util.DirectoryScanner; -import org.nuiton.io.MirroredFileUpdater; +import java.util.*; /** * Base mojo with usefull methods and implementing {@link Plugin} contract. @@ -54,9 +49,10 @@ * <p/> * <b>Note:</b> The method is invoked before the {@link #doAction()} method. * <p/> - * @return <code>true</code> if mojo is well initialize and something has - * to be done in the {@link #doAction()} method, <code>false</code> - * otherwise. + * + * @return <code>true</code> if mojo is well initialize and something has + * to be done in the {@link #doAction()} method, <code>false</code> + * otherwise. * @throws Exception if any */ protected abstract boolean init() throws Exception; @@ -73,6 +69,7 @@ * @throws Exception if any */ protected abstract void doAction() throws Exception; + /** * the message to display when will skip the goal */ @@ -126,16 +123,15 @@ * the {@link #execute()}. * <p/> * <b>Tip:</b> There is two method to simplify the packaging check : - * - * {@link #acceptPackaging(org.nuiton.plugin.Plugin.Packaging[])} - * + * <p/> + * {@link #acceptPackaging(org.nuiton.plugin.Plugin.Packaging...)} + * <p/> * and + * <p/> + * {@link #rejectPackaging(org.nuiton.plugin.Plugin.Packaging...)} * - * {@link #rejectPackaging(org.nuiton.plugin.Plugin.Packaging[])} - * - * * @return {@code true} if can execute the goal for the packaging of the - * project, {@code false} otherwise. + * project, {@code false} otherwise. */ protected boolean checkPackaging() { return true; @@ -143,7 +139,7 @@ /** * Accept the project's packaging between some given. - * + * * @param packages the accepted packaging * @return {@code true} if the project's packagin is one of the given ones. */ @@ -180,7 +176,6 @@ } /** - * * @return {@code true} if project is the root execution project */ protected boolean isExecutionRoot() { @@ -188,11 +183,12 @@ } /** + * Check if an execution was already done according to the given parameters. * - * @param runOnce - * @param onlyForRoot - * @param buildStartTime - * @param newStartTime + * @param runOnce the flag + * @param onlyForRoot a flag to + * @param buildStartTime the build statrt time (if none means must do it) + * @param newStartTime the current build start time * @return {@code true} if the goal was already invoked */ protected boolean checkRunOnceDone(boolean runOnce, boolean onlyForRoot, Date buildStartTime, Date newStartTime) { @@ -240,7 +236,6 @@ * du projet maven. * * @param paths les paths pour atteindre le fichier ou le répertoire - * * @return le fichier de la destination * @since 1.1.0 */ @@ -255,11 +250,10 @@ /** * Copy a file to a given locationand logging. * - * @param srcFile represents the file to copy. + * @param srcFile represents the file to copy. * @param destFile file name of destination file. - * * @throws MojoExecutionException with a message if an - * error occurs. + * error occurs. */ public void copyFile(File srcFile, File destFile) throws MojoExecutionException { @@ -278,7 +272,7 @@ * {@code encoding}. * * @param destFile location where to write the content - * @param content content ot write in the file + * @param content content ot write in the file * @param encoding the enconding of the file * @throws IOException if any pb while writing the content into the file */ @@ -300,14 +294,20 @@ /** * Collects some file. - * + * * @param includes includes * @param excludes excludes - * @param roots root directories to treate - * @param files cache of file detected indexed by their root directory - * @param updater + * @param roots root directories to treate + * @param files cache of file detected indexed by their root directory + * @param updater the updater used to detect changes (if none, will get all + * the files) */ - protected void getFilesToTreateForRoots(String[] includes, String[] excludes, List<String> roots, Map<File, String[]> files, MirroredFileUpdater updater) { + protected void getFilesToTreateForRoots( + String[] includes, + String[] excludes, + List<String> roots, Map<File, + String[]> files, + MirroredFileUpdater updater) { DirectoryScanner ds = new DirectoryScanner(); ds.setIncludes(includes); @@ -371,8 +371,8 @@ * * @param includes includes * @param excludes excludes - * @param srcDir the directory to treate - * @param updater an updater (will give the mirrored files) + * @param srcDir the directory to treate + * @param updater an updater (will give the mirrored files) * @return the map of mirrored files associated to their files in srcDir */ protected Map<String, String> getFilesToTreate(String[] includes, String[] excludes, File srcDir, MirroredFileUpdater updater) { @@ -496,8 +496,38 @@ /** * Add a new resource location to the maven project (in not already present). * + * @param dir the new resource location to add + * @param includes files to include + * @since 1.1.1 + */ + protected void addResourceDir(File dir, String... includes) { + boolean added = PluginHelper.addResourceDir(dir, getProject(), includes); + if (added && isVerbose()) { + getLog().info("add resource " + dir + " with includes " + Arrays.toString(includes)); + } + } + + /** + * Add a new test resource location to the maven project (in not already present). + * + * @param dir the new resource location to add + * @param includes files to include + * @since 1.1.1 + */ + protected void addTestResourceDir(File dir, String... includes) { + boolean added = PluginHelper.addTestResourceDir(dir, getProject(), includes); + if (added && isVerbose()) { + getLog().info("add test resource " + dir + " with includes " + Arrays.toString(includes)); + } + } + + /** + * Add a new resource location to the maven project (in not already present). + * * @param dir the new resource location to add + * @deprecated since 1.1.1, prefer use the method {@link #addResourceDir(java.io.File, String...)} */ + @Deprecated protected void addResourceDir(String dir) { boolean added = PluginHelper.addResourceDir(dir, getProject()); if (added) { @@ -509,7 +539,9 @@ * Add a new test resource location to the maven project (in not already present). * * @param dir the new resource location to add + * @deprecated since 1.1.1, prefer use the method {@link #addTestResourceDir(java.io.File, String...)} */ + @Deprecated protected void addTestResourceDir(String dir) { boolean added = PluginHelper.addTestResourceDir(dir, getProject()); if (added) { @@ -518,21 +550,57 @@ } /** + * Create the directory if necessary. + * + * @param dir the directory to create if not already existing + * @throws IOException if could not create the directory + * @since 1.1.1 + */ + protected void createDirectoryIfNecessary(File dir) throws IOException { + boolean b = PluginHelper.createDirectoryIfNecessary(dir); + if (b && isVerbose()) { + getLog().info("mkdir " + dir); + } + } + + /** + * Create a new file. + * + * @param file the file to create + * @throws IOException if could not create the file + * @since 1.1.1 + */ + protected void createNewFile(File file) throws IOException { + PluginHelper.createNewFile(file); + } + + /** + * Delete the given file. + * + * @param file the file to delete + * @throws IOException if could not delete the file + * @since 1.1.1 + */ + protected void deleteFile(File file) throws IOException { + PluginHelper.deleteFile(file); + } + + /** * Init mojo classLoader. * - * @param project the maven project - * @param src the source directory - * @param addSourcesToClassPath a flag to a maven sources to classLoader - * @param testPhase a flag to specify if we are in a test phase (changes the classLoader) + * @param project the maven project + * @param src the source directory + * @param addSourcesToClassPath a flag to a maven sources to classLoader + * @param testPhase a flag to specify if we are in a test phase (changes the classLoader) * @param addResourcesToClassPath flag to add maven's resources to classLoader - * @param addCompileClassPath flag to add maven's project compile classPath to classLoader - * @param addProjectClassPath flag to add maven'es project dependecies to classLoader + * @param addCompileClassPath flag to add maven's project compile classPath to classLoader + * @param addProjectClassPath flag to add maven'es project dependecies to classLoader * @return the new classLoader - * @throws MalformedURLException + * @throws MalformedURLException if an url was not correct */ @SuppressWarnings({"unchecked"}) protected URLClassLoader initClassLoader(MavenProject project, File src, boolean addSourcesToClassPath, boolean testPhase, boolean addResourcesToClassPath, boolean addCompileClassPath, boolean addProjectClassPath) throws MalformedURLException { - URLClassLoader loader = null; + URLClassLoader loader; if (project != null) { URLClassLoader result; @@ -559,9 +627,7 @@ } } if (testPhase && addCompileClassPath) { - if (project != null) { - addDirectoryToUrlsList(new File(project.getBuild().getOutputDirectory()), lUrls, dones); - } + addDirectoryToUrlsList(new File(project.getBuild().getOutputDirectory()), lUrls, dones); } if (src != null) { addDirectoryToUrlsList(src, lUrls, dones); @@ -599,14 +665,14 @@ /** * Add the given {@code directory} in {@code urls} if not already included. - * + * <p/> * <b>Note:</b> We use a extra list to store file string representation, since * we do NOT want any url resolution and the {@link URL#equals(java.lang.Object)} is * doing some... * * @param directory the directory to insert in {@code urls} - * @param urls list of urls - * @param done list of string representation of urls + * @param urls list of urls + * @param done list of string representation of urls * @throws MalformedURLException if pb while converting file to url * @since 1.1.0 */ @@ -617,12 +683,12 @@ /** * Add the given {@code url} in {@code urls} if not already included. - * + * <p/> * <b>Note:</b> We use a extra list to store file string representation, since * we do NOT want any url resolution and the {@link URL#equals(java.lang.Object)} is * doing some.. * - * @param url the url to insert in {@code urls} + * @param url the url to insert in {@code urls} * @param urls list of urls * @param done list of string representation of urls * @since 1.1.0 Modified: trunk/src/main/java/org/nuiton/plugin/PluginHelper.java =================================================================== --- trunk/src/main/java/org/nuiton/plugin/PluginHelper.java 2009-11-26 13:56:56 UTC (rev 638) +++ trunk/src/main/java/org/nuiton/plugin/PluginHelper.java 2009-12-28 09:16:26 UTC (rev 639) @@ -39,6 +39,7 @@ import java.util.TreeSet; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; + import org.apache.maven.model.Resource; import org.apache.maven.project.MavenProject; import org.codehaus.plexus.util.DirectoryScanner; @@ -71,7 +72,7 @@ * <p/> * Note : <b>Aucune liste n'est creee, ni recopiee</b> * - * @param <O> le type des objets de la liste + * @param <O> le type des objets de la liste * @param list la liste a convertir * @param type le type des elements de la liste * @return la liste typee @@ -90,6 +91,7 @@ } return (List<O>) list; } + static final protected double[] timeFactors = {1000000, 1000, 60, 60, 24}; static final protected String[] timeUnites = {"ns", "ms", "s", "m", "h", "d"}; static final protected double[] memoryFactors = {1024, 1024, 1024, 1024}; @@ -126,7 +128,7 @@ * Split the given {@code text} using the {@code separator} and trim * each part of result. * - * @param txt text to split + * @param txt text to split * @param separator the split separator * @return the splited text with trimmed parts. * @since 1.1.0 @@ -142,9 +144,9 @@ /** * Prefix the lines of the given content with a given prefix. * - * @param prefix prefix to add on each line of text + * @param prefix prefix to add on each line of text * @param prefixForEmpty prefix to add for empty lines - * @param content the text to treate + * @param content the text to treate * @return the text transformed * @throws IOException if any reading problem */ @@ -183,7 +185,6 @@ * d'un dictionnaire donne. * * @param map le dictionner a parcourir - * * @return la liste des clefs dont la valeur est nulle ou vide */ static public SortedSet<String> getEmptyKeys(Map<?, ?> map) { @@ -196,18 +197,110 @@ return result; } + /** + * Add the directory as a resource of the given project. + * + * @param dir the directory to add + * @param project the project to update + * @param includes the includes of the resource + * @return {@code true} if the resources was added (not already existing) + * @since 1.1.1 + */ + public static boolean addResourceDir(File dir, MavenProject project, String... includes) { + List<?> resources = project.getResources(); + boolean added = addResourceDir(dir, project, resources, includes); + return added; + } + + /** + * Add thedirectory as a test resource of the given project. + * + * @param dir the directory to add + * @param project the project to update + * @param includes the includes of the resource + * @return {@code true} if the resources was added (not already existing) + * @since 1.1.1 + */ + public static boolean addTestResourceDir(File dir, MavenProject project, String... includes) { + List<?> resources = project.getTestResources(); + boolean added = addResourceDir(dir, project, resources, includes); + return added; + } + + /** + * Add the directory as a resource in the given resource list. + * + * @param dir the directory to add + * @param project the project involved + * @param resources the list of existing resources + * @param includes includes of the new resources + * @return {@code true} if the resource was added (not already existing) + * @since 1.1.1 + */ + public static boolean addResourceDir(File dir, MavenProject project, List<?> resources, String... includes) { + String newresourceDir = dir.getAbsolutePath(); + boolean shouldAdd = true; + for (Object o : resources) { + Resource r = (Resource) o; + if (!r.getDirectory().equals(newresourceDir)) { + continue; + } + + for (String i : includes) { + if (!r.getIncludes().contains(i)) { + r.addInclude(i); + } + } + shouldAdd = false; + break; + } + if (shouldAdd) { + Resource r = new Resource(); + r.setDirectory(newresourceDir); + for (String i : includes) { + if (!r.getIncludes().contains(i)) { + r.addInclude(i); + } + } + project.addResource(r); + } + return shouldAdd; + } + + /** + * @param newresourceDir the new resource directory to add + * @param project the maven project to modifiy + * @return {@code true} if resources was added + * @deprecated since 1.1.1, prefer use the {@link #addResourceDir(java.io.File, org.apache.maven.project.MavenProject, String...)} + */ + @Deprecated public static boolean addResourceDir(String newresourceDir, MavenProject project) { List<?> resources = project.getResources(); boolean added = addResourceDir(newresourceDir, project, resources); return added; } + /** + * @param newresourceDir the new resource directory to add + * @param project the maven project to modifiy + * @return {@code true} if resources was added + * @deprecated since 1.1.1, prefer use the {@link #addTestResourceDir(java.io.File, org.apache.maven.project.MavenProject, String...)} + */ + @Deprecated public static boolean addTestResourceDir(String newresourceDir, MavenProject project) { List<?> resources = project.getTestResources(); boolean added = addResourceDir(newresourceDir, project, resources); return added; } + /** + * @param newresourceDir the new resource directory to add + * @param project the maven project to modifiy + * @param resources the known resources for the maven project + * @return {@code true} if resources was added + * @deprecated since 1.1.1, prefer use the {@link #addResourceDir(java.io.File, org.apache.maven.project.MavenProject, java.util.List, String...)} + */ + @Deprecated public static boolean addResourceDir(String newresourceDir, MavenProject project, List<?> resources) { boolean shouldAdd = true; for (Object o : resources) { @@ -231,6 +324,54 @@ } /** + * Cretae the directory (and his parents) if necessary. + * + * @param dir the directory to create if not exisiting + * @return {@code true} if directory was created, {@code false} if was no need to create it + * @throws IOException if could not create directory + * @since 1.1.1 + */ + public static boolean createDirectoryIfNecessary(File dir) throws IOException { + if (!dir.exists()) { + boolean b = dir.mkdirs(); + if (!b) { + throw new IOException("Could not create directory " + dir); + } + return true; + } + return false; + } + + /** + * Create the given file. + * + * @param file the file to create + * @throws IOException if could not create the file + * @since 1.1.1 + */ + public static void createNewFile(File file) throws IOException { + createDirectoryIfNecessary(file.getParentFile()); + boolean b = file.createNewFile(); + if (!b) { + throw new IOException("could not create new file " + file); + } + } + + /** + * Delete the given file. + * + * @param file the file to delete + * @throws IOException if could not delete the file + * @since 1.1.1 + */ + public static void deleteFile(File file) throws IOException { + boolean b = file.delete(); + if (!b) { + throw new IOException("could not delete file " + file); + } + } + + /** * Permet de copier le fichier source vers le fichier cible. * * @param source le fichier source @@ -238,7 +379,8 @@ * @throws java.io.IOException Erreur de copie */ public static void copy(File source, File target) throws IOException { - target.getParentFile().mkdirs(); + createDirectoryIfNecessary(target.getParentFile()); +// target.getParentFile().mkdirs(); FileUtils.copyFile(source, target); // FileChannel sourceChannel = new FileInputStream(source).getChannel(); // FileChannel targetChannel = new FileOutputStream(target).getChannel(); @@ -300,9 +442,11 @@ * @throws IOException if IO pb */ static public void writeString(File file, String content, String encoding) throws IOException { - file.getParentFile().mkdirs(); - BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), encoding)); + createDirectoryIfNecessary(file.getParentFile()); +// file.getParentFile().mkdirs(); + BufferedWriter out = null; try { + out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), encoding)); IOUtil.copy(content, out); // out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), encoding)); // out.write(content);
participants (1)
-
tchemit@users.nuiton.org