diff --git a/src/it/MLICENSE-44/pom.xml b/src/it/MLICENSE-44/pom.xml index ae6799944..4f9c20b53 100644 --- a/src/it/MLICENSE-44/pom.xml +++ b/src/it/MLICENSE-44/pom.xml @@ -19,7 +19,7 @@ test CodeHaus - file:${basedir}/src/license + ${project.baseUri}/src/license ${basedir}/src/license/test/descriptionTemplate.ftl 2010 diff --git a/src/it/add-third-party-missing-file/pom.xml b/src/it/add-third-party-missing-file/pom.xml index ed2279107..c7c0da6da 100644 --- a/src/it/add-third-party-missing-file/pom.xml +++ b/src/it/add-third-party-missing-file/pom.xml @@ -103,7 +103,7 @@ add-third-party - file:///${basedir}/missing-licenses.properties + ${project.baseUri}/missing-licenses.properties THIRD-PARTY-by-file.txt diff --git a/src/main/java/org/codehaus/mojo/license/AbstractFileHeaderMojo.java b/src/main/java/org/codehaus/mojo/license/AbstractFileHeaderMojo.java index 69450bb42..78ba9d0be 100644 --- a/src/main/java/org/codehaus/mojo/license/AbstractFileHeaderMojo.java +++ b/src/main/java/org/codehaus/mojo/license/AbstractFileHeaderMojo.java @@ -37,6 +37,7 @@ import java.util.concurrent.ConcurrentHashMap; import freemarker.template.Template; +import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.Component; @@ -758,7 +759,7 @@ private boolean processFile(FileHeaderProcessor processor, File file, File proce // this is a costy operation // TODO-TC-20100411 We should process always from the read content not reading again from file - content = FileUtil.readAsString(file, getEncoding()); + content = IOUtils.toString(file.toURI(), getEncoding()); } catch (IOException e) { throw new IOException("Could not obtain content of file " + file); @@ -859,7 +860,7 @@ private void finalizeFile(File file, File processFile) throws IOException { } else { try { // replace file with the updated one - String updatedContent = FileUtil.readAsString(processFile, getEncoding()); + String updatedContent = IOUtils.toString(processFile.toURI(), getEncoding()); FileUtil.printString(file, updatedContent, getEncoding()); Files.deleteIfExists(processFile.toPath()); } catch (IOException e) { diff --git a/src/main/java/org/codehaus/mojo/license/RemoveFileHeaderMojo.java b/src/main/java/org/codehaus/mojo/license/RemoveFileHeaderMojo.java index 9809d14c5..c0eb09b9b 100644 --- a/src/main/java/org/codehaus/mojo/license/RemoveFileHeaderMojo.java +++ b/src/main/java/org/codehaus/mojo/license/RemoveFileHeaderMojo.java @@ -34,6 +34,7 @@ import java.util.TreeMap; import freemarker.template.Template; +import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.apache.maven.plugins.annotations.Component; import org.apache.maven.plugins.annotations.Mojo; @@ -408,7 +409,7 @@ private boolean processFile(FileHeaderTransformer transformer, File file, File p String content; try { - content = FileUtil.readAsString(file, getEncoding()); + content = IOUtils.toString(file.toURI(), getEncoding()); } catch (IOException e) { throw new IOException("Could not obtain content of file " + file); } diff --git a/src/main/java/org/codehaus/mojo/license/model/License.java b/src/main/java/org/codehaus/mojo/license/model/License.java index 12169ce6e..c44009ad9 100644 --- a/src/main/java/org/codehaus/mojo/license/model/License.java +++ b/src/main/java/org/codehaus/mojo/license/model/License.java @@ -22,16 +22,14 @@ * #L% */ -import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStreamReader; -import java.io.Reader; +import java.io.InputStream; import java.net.URL; +import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; import org.codehaus.mojo.license.utils.MojoHelper; -import org.codehaus.plexus.util.IOUtil; /** * The model of a license. @@ -113,11 +111,8 @@ public String getLicenseContent(String encoding) throws IOException { throw new IllegalStateException("no baseURL defined, can not obtain license content in " + this); } - Reader r = new BufferedReader(new InputStreamReader(getLicenseURL().openStream(), encoding)); - try { - return IOUtil.toString(r); - } finally { - r.close(); + try (InputStream in = getLicenseURL().openStream()) { + return IOUtils.toString(in, encoding); } } @@ -125,11 +120,9 @@ public String getHeaderContent(String encoding) throws IOException { if (baseURL == null) { throw new IllegalStateException("no baseURL defined, can not obtain header content in " + this); } - Reader r = new BufferedReader(new InputStreamReader(getHeaderURL().openStream(), encoding)); - try { - return IOUtil.toString(r); - } finally { - r.close(); + + try (InputStream in = getHeaderURL().openStream()) { + return IOUtils.toString(in, encoding); } } diff --git a/src/main/java/org/codehaus/mojo/license/model/LicenseRepository.java b/src/main/java/org/codehaus/mojo/license/model/LicenseRepository.java index 6ad14b00c..8fd744024 100644 --- a/src/main/java/org/codehaus/mojo/license/model/LicenseRepository.java +++ b/src/main/java/org/codehaus/mojo/license/model/LicenseRepository.java @@ -23,6 +23,7 @@ */ import java.io.IOException; +import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; @@ -106,7 +107,9 @@ public void load() throws IOException { "no licenses.properties found with url [" + definitionURL + "] for resolver " + this); } Properties p = new Properties(); - p.load(definitionURL.openStream()); + try (InputStream in = definitionURL.openStream()) { + p.load(in); + } for (Entry entry : p.entrySet()) { String licenseName = (String) entry.getKey(); diff --git a/src/main/java/org/codehaus/mojo/license/model/LicenseStore.java b/src/main/java/org/codehaus/mojo/license/model/LicenseStore.java index c83b53404..259213d85 100644 --- a/src/main/java/org/codehaus/mojo/license/model/LicenseStore.java +++ b/src/main/java/org/codehaus/mojo/license/model/LicenseStore.java @@ -23,6 +23,7 @@ */ import java.io.IOException; +import java.net.URI; import java.net.URL; import java.util.ArrayList; import java.util.Iterator; @@ -160,7 +161,7 @@ public void addRepository(String extraResolver) throws IOException { URL baseURL = getClass().getClassLoader().getResource(extraResolver); addRepository(baseURL); } else { - URL baseURL = new URL(extraResolver); + URL baseURL = URI.create(extraResolver).toURL(); addRepository(baseURL); } } diff --git a/src/main/java/org/codehaus/mojo/license/utils/FileUtil.java b/src/main/java/org/codehaus/mojo/license/utils/FileUtil.java index bc2db0d7a..63642a1c9 100644 --- a/src/main/java/org/codehaus/mojo/license/utils/FileUtil.java +++ b/src/main/java/org/codehaus/mojo/license/utils/FileUtil.java @@ -31,7 +31,6 @@ import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.StringReader; -import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; import java.security.MessageDigest; @@ -44,8 +43,7 @@ import java.util.Locale; import org.apache.commons.codec.binary.Hex; -import org.codehaus.plexus.util.FileUtils; -import org.codehaus.plexus.util.IOUtil; +import org.apache.commons.io.FileUtils; /** * Some basic file io utilities @@ -66,12 +64,12 @@ public class FileUtil { public static void renameFile(File file, File destination) throws IOException { try { try { - org.apache.commons.io.FileUtils.forceDelete(destination); + FileUtils.forceDelete(destination); } catch (FileNotFoundException ex) { // Just do nothing } - org.apache.commons.io.FileUtils.moveFile(file, destination); + FileUtils.moveFile(file, destination); } catch (IOException ex) { throw new IOException(String.format("could not rename '%s' to '%s'", file, destination)); } @@ -109,22 +107,6 @@ public static void backupFile(File f) throws IOException { copyFile(f, dst); } - /** - * Permet de lire un fichier et de retourner sont contenu sous forme d'une - * chaine de carateres. - * - * @param file le fichier a lire - * @param encoding encoding to read file - * @return the content of the file - * @throws IOException if IO pb - */ - public static String readAsString(File file, String encoding) throws IOException { - - try (BufferedReader in = Files.newBufferedReader(file.toPath(), Charset.forName(encoding))) { - return IOUtil.toString(in); - } - } - /** * Print content to file. This method ensures that a platform specific line ending is used. * @@ -136,18 +118,13 @@ public static String readAsString(File file, String encoding) throws IOException public static void printString(File file, String content, String encoding) throws IOException { Files.createDirectories(file.getParentFile().toPath()); - BufferedReader in; - PrintWriter out; - in = new BufferedReader(new StringReader(content)); - out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), encoding))); - try { + try (BufferedReader in = new BufferedReader(new StringReader(content)); + PrintWriter out = new PrintWriter( + new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), encoding)))) { String line; while ((line = in.readLine()) != null) { out.println(line); } - } finally { - in.close(); - out.close(); } } diff --git a/src/main/java/org/codehaus/mojo/license/utils/MojoHelper.java b/src/main/java/org/codehaus/mojo/license/utils/MojoHelper.java index 2f5dbf383..1725ae494 100644 --- a/src/main/java/org/codehaus/mojo/license/utils/MojoHelper.java +++ b/src/main/java/org/codehaus/mojo/license/utils/MojoHelper.java @@ -24,6 +24,7 @@ import java.io.File; import java.net.MalformedURLException; +import java.net.URI; import java.net.URL; import java.text.MessageFormat; import java.util.Arrays; @@ -164,7 +165,7 @@ public static String convert(long value, double[] factors, String[] unites) { public static URL getUrl(URL baseUrl, String suffix) { String url = baseUrl.toString() + "/" + suffix; try { - return new URL(url); + return URI.create(url).toURL(); } catch (MalformedURLException ex) { throw new IllegalArgumentException("could not obtain url " + url, ex); } diff --git a/src/main/java/org/codehaus/mojo/license/utils/UrlRequester.java b/src/main/java/org/codehaus/mojo/license/utils/UrlRequester.java index 20712c0fa..5cdd2f481 100644 --- a/src/main/java/org/codehaus/mojo/license/utils/UrlRequester.java +++ b/src/main/java/org/codehaus/mojo/license/utils/UrlRequester.java @@ -25,7 +25,9 @@ import java.io.BufferedReader; import java.io.CharArrayReader; import java.io.IOException; +import java.io.InputStream; import java.net.MalformedURLException; +import java.net.URI; import java.net.URL; import java.nio.charset.Charset; import java.util.ArrayList; @@ -64,9 +66,9 @@ public static boolean isStringUrl(String data) { return true; } try { - new URL(data); + URI.create(data).toURL(); return true; - } catch (MalformedURLException e) { + } catch (IllegalArgumentException | MalformedURLException e) { return false; } } @@ -153,7 +155,9 @@ public static String getFromUrl(String url, String encoding) throws IOException } } } else { - result = IOUtils.toString(new URL(url).openStream(), charset); + try (InputStream in = URI.create(url).toURL().openStream()) { + result = IOUtils.toString(in, charset); + } } return result; } @@ -172,10 +176,8 @@ public static String getFromUrl(String url, String encoding) throws IOException */ public static List downloadList(String url) throws MojoExecutionException { List list = new ArrayList<>(); - BufferedReader bufferedReader = null; - try { - bufferedReader = - new BufferedReader(new CharArrayReader(getFromUrl(url).toCharArray())); + try (BufferedReader bufferedReader = + new BufferedReader(new CharArrayReader(getFromUrl(url).toCharArray()))) { String line; while ((line = bufferedReader.readLine()) != null) { if (StringUtils.isNotBlank(line) && !StringUtils.startsWith(line, "#") && !list.contains(line)) { @@ -184,14 +186,6 @@ public static List downloadList(String url) throws MojoExecutionExceptio } } catch (IOException e) { throw new MojoExecutionException("Could not open connection to URL: " + url, e); - } finally { - if (bufferedReader != null) { - try { - bufferedReader.close(); - } catch (IOException e) { - throw new MojoExecutionException(e.getMessage(), e); - } - } } return list; }