Author: tchemit Date: 2009-10-16 14:25:15 +0200 (Fri, 16 Oct 2009) New Revision: 617 Modified: trunk/src/main/java/org/nuiton/plugin/AbstractPlugin.java Log: add usefull methods on AbstractPlugin Modified: trunk/src/main/java/org/nuiton/plugin/AbstractPlugin.java =================================================================== --- trunk/src/main/java/org/nuiton/plugin/AbstractPlugin.java 2009-10-13 12:43:34 UTC (rev 616) +++ trunk/src/main/java/org/nuiton/plugin/AbstractPlugin.java 2009-10-16 12:25:15 UTC (rev 617) @@ -33,6 +33,7 @@ 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; @@ -235,6 +236,23 @@ } /** + * Recupere le fichier donnée à partir de son chemin relatif sur le basedir + * 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 + */ + public File getFileFromBasedir(String... paths) { + File result = getProject().getBasedir(); + for (String path : paths) { + result = new File(result, path); + } + return result; + } + + /** * Copy a file to a given locationand logging. * * @param srcFile represents the file to copy. @@ -520,32 +538,33 @@ URLClassLoader result; try { + Set<String> dones = new HashSet<String>(); List<URL> lUrls = new ArrayList<URL>(); List<String> sources = project.getCompileSourceRoots(); if (addSourcesToClassPath) { for (String source : sources) { - lUrls.add(new File(source).toURI().toURL()); + addDirectoryToUrlsList(new File(source), lUrls, dones); } if (testPhase) { for (Object source : project.getTestCompileSourceRoots()) { - lUrls.add(new File(source.toString()).toURI().toURL()); + addDirectoryToUrlsList(new File(source.toString()), lUrls, dones); } } } if (addResourcesToClassPath) { for (Object source : project.getResources()) { Resource r = (Resource) source; - lUrls.add(new File(r.getDirectory()).toURI().toURL()); + addDirectoryToUrlsList(new File(r.getDirectory()), lUrls, dones); } } if (testPhase && addCompileClassPath) { if (project != null) { - lUrls.add(new File(project.getBuild().getOutputDirectory()).toURI().toURL()); + addDirectoryToUrlsList(new File(project.getBuild().getOutputDirectory()), lUrls, dones); } } if (src != null) { - lUrls.add(src.toURI().toURL()); + addDirectoryToUrlsList(src, lUrls, dones); } if (addProjectClassPath) { getLog().info("use project compile scope class-path"); @@ -553,7 +572,7 @@ Set<?> artifacts = project.getArtifacts(); for (Object o : artifacts) { Artifact a = (Artifact) o; - lUrls.add(a.getFile().toURI().toURL()); + addDirectoryToUrlsList(a.getFile(), lUrls, dones); } } @@ -579,6 +598,45 @@ } /** + * Add the given {@code directory} in {@code urls} if not already included. + * + * <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 + * @throws MalformedURLException if pb while converting file to url + * @since 1.1.0 + */ + protected void addDirectoryToUrlsList(File directory, List<URL> urls, Set<String> done) throws MalformedURLException { + // do the comparaison on a String to avoid url to be resolved (in URL.equals method) + addUrlToUrlsList(directory.toURI().toURL(), urls, done); + } + + /** + * Add the given {@code url} in {@code urls} if not already included. + * + * <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 urls list of urls + * @param done list of string representation of urls + * @since 1.1.0 + */ + protected void addUrlToUrlsList(URL url, List<URL> urls, Set<String> done) { + // do the comparaison on a String to avoid url to be resolved (in URL.equals method) + String u = url.toString(); + if (!done.contains(u)) { + done.add(u); + urls.add(url); + } + } + + /** * Obtain the url of a file, if file does not exist, try in the classPath. * * @param f the required resource file.