diff --git a/src/it/projects/MJAVADOC-528/pom.xml b/src/it/projects/MJAVADOC-528/pom.xml index 816d94149..cc845df89 100644 --- a/src/it/projects/MJAVADOC-528/pom.xml +++ b/src/it/projects/MJAVADOC-528/pom.xml @@ -39,6 +39,7 @@ @project.version@ true + true diff --git a/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java b/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java index 7e62bcc6b..93bb7b712 100644 --- a/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java +++ b/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java @@ -3759,13 +3759,14 @@ private void addLinkofflineArguments(List arguments, Set of if (location == null || location.isEmpty()) { continue; } - if (isValidJavadocLink(location, false)) { - addArgIfNotEmpty( - arguments, - "-linkoffline", - JavadocUtil.quotedPathArgument(url) + " " + JavadocUtil.quotedPathArgument(location), - true); + if (validateLinks && !isValidJavadocLink(location, false)) { + continue; } + addArgIfNotEmpty( + arguments, + "-linkoffline", + JavadocUtil.quotedPathArgument(url) + " " + JavadocUtil.quotedPathArgument(location), + true); } } @@ -5617,7 +5618,11 @@ private List getDependenciesLinks() { } } - if (url != null && isValidJavadocLink(url, detected)) { + if (url != null) { + if (validateLinks && !isValidJavadocLink(url, detected)) { + continue; + } + getLog().debug("Added Javadoc link: " + url + " for " + artifact.getId()); dependenciesLinks.add(url); @@ -5814,14 +5819,14 @@ protected boolean isValidJavadocLink(String link, boolean detecting) { } try { - if (JavadocUtil.isValidElementList(elementListUri.toURL(), settings, validateLinks)) { + if (JavadocUtil.isValidElementList(elementListUri.toURL(), settings)) { return true; } } catch (IOException e) { // ignore this because it is optional } - if (JavadocUtil.isValidPackageList(packageListUri.toURL(), settings, validateLinks)) { + if (JavadocUtil.isValidPackageList(packageListUri.toURL(), settings)) { return true; } diff --git a/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java b/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java index 5bd3fb7a3..f5710ad88 100644 --- a/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java +++ b/src/main/java/org/apache/maven/plugins/javadoc/JavadocUtil.java @@ -1333,52 +1333,55 @@ protected static URL getRedirectUrl(URL url, Settings settings) throws IOExcepti } /** - * Validates an URL to point to a valid package-list resource. + * Validates the URL (content) to point to a valid package-list resource. * - * @param url The URL to validate. + * @param url The URL (content) to validate. * @param settings The user settings used to configure the connection to the URL or {@code null}. - * @param validateContent true to validate the content of the package-list resource; - * false to only check the existence of the package-list resource. * @return true if url points to a valid package-list resource; * false else. * @throws IOException if reading the resource fails. * @see #createHttpClient(org.apache.maven.settings.Settings, java.net.URL) * @since 2.8 */ - protected static boolean isValidPackageList(URL url, Settings settings, boolean validateContent) - throws IOException { + protected static boolean isValidPackageList(URL url, Settings settings) throws IOException { if (url == null) { - throw new IllegalArgumentException("The url is null"); + throw new NullPointerException("The url is null"); } try (BufferedReader reader = getReader(url, settings)) { - if (validateContent) { - for (String line = reader.readLine(); line != null; line = reader.readLine()) { - if (!isValidPackageName(line)) { - return false; - } + for (String line = reader.readLine(); line != null; line = reader.readLine()) { + if (!isValidPackageName(line)) { + return false; } } return true; } } - protected static boolean isValidElementList(URL url, Settings settings, boolean validateContent) - throws IOException { + /** + * Validates the URL (content) to point to a valid element-list resource. + * + * @param url The URL (content) to validate. + * @param settings The user settings used to configure the connection to the URL or {@code null}. + * @return true if url points to a valid element-list resource; + * false else. + * @throws IOException if reading the resource fails. + * @see #createHttpClient(org.apache.maven.settings.Settings, java.net.URL) + * @since 3.1.0 + */ + protected static boolean isValidElementList(URL url, Settings settings) throws IOException { if (url == null) { - throw new IllegalArgumentException("The url is null"); + throw new NullPointerException("The url is null"); } try (BufferedReader reader = getReader(url, settings)) { - if (validateContent) { - for (String line = reader.readLine(); line != null; line = reader.readLine()) { - if (line.startsWith("module:")) { - continue; - } + for (String line = reader.readLine(); line != null; line = reader.readLine()) { + if (line.startsWith("module:")) { + continue; + } - if (!isValidPackageName(line)) { - return false; - } + if (!isValidPackageName(line)) { + return false; } } return true; diff --git a/src/test/java/org/apache/maven/plugins/javadoc/JavadocUtilTest.java b/src/test/java/org/apache/maven/plugins/javadoc/JavadocUtilTest.java index a421706ff..5516c308a 100644 --- a/src/test/java/org/apache/maven/plugins/javadoc/JavadocUtilTest.java +++ b/src/test/java/org/apache/maven/plugins/javadoc/JavadocUtilTest.java @@ -249,17 +249,17 @@ public void testIsValidPackageList() throws Exception { URL url = null; URL wrongUrl; try { - JavadocUtil.isValidPackageList(url, settings, false); + JavadocUtil.isValidPackageList(url, settings); fail(); - } catch (IllegalArgumentException e) { + } catch (NullPointerException e) { assertTrue(true); } url = new File(getBasedir(), "/pom.xml").toURI().toURL(); - assertTrue(JavadocUtil.isValidPackageList(url, settings, false)); + assertFalse(JavadocUtil.isValidPackageList(url, settings)); try { - assertFalse(JavadocUtil.isValidPackageList(url, settings, true)); + assertFalse(JavadocUtil.isValidPackageList(url, settings)); } catch (IOException e) { assertTrue(true); } @@ -268,14 +268,14 @@ public void testIsValidPackageList() throws Exception { .getResource("/JavadocUtilTest-package-list.txt") .toURI() .toURL(); - assertTrue(JavadocUtil.isValidPackageList(url, settings, true)); + assertTrue(JavadocUtil.isValidPackageList(url, settings)); url = new URL("http://maven.apache.org/plugins-archives/maven-javadoc-plugin-3.5.0/apidocs/package-list"); - assertTrue(JavadocUtil.isValidPackageList(url, settings, true)); + assertTrue(JavadocUtil.isValidPackageList(url, settings)); wrongUrl = new URL("http://maven.apache.org/plugins/maven-javadoc-plugin/apidocs/package-list2"); try { - JavadocUtil.isValidPackageList(wrongUrl, settings, false); + JavadocUtil.isValidPackageList(wrongUrl, settings); fail(); } catch (IOException e) { assertTrue(true); @@ -291,10 +291,10 @@ public void testIsValidPackageList() throws Exception { settings = new Settings(); - assertTrue(JavadocUtil.isValidPackageList(url, settings, true)); + assertTrue(JavadocUtil.isValidPackageList(url, settings)); try { - JavadocUtil.isValidPackageList(wrongUrl, settings, false); + JavadocUtil.isValidPackageList(wrongUrl, settings); fail(); } catch (IOException e) { assertTrue(true); @@ -321,7 +321,7 @@ public void testIsValidPackageList() throws Exception { proxy.setProtocol("http"); settings.addProxy(proxy); - JavadocUtil.isValidPackageList(url, settings, false); + JavadocUtil.isValidPackageList(url, settings); fail(); } catch (FileNotFoundException e) { assertTrue(true); @@ -345,10 +345,10 @@ public void testIsValidPackageList() throws Exception { proxy.setPassword("bar"); settings.addProxy(proxy); - assertTrue(JavadocUtil.isValidPackageList(url, settings, true)); + assertTrue(JavadocUtil.isValidPackageList(url, settings)); try { - JavadocUtil.isValidPackageList(wrongUrl, settings, false); + JavadocUtil.isValidPackageList(wrongUrl, settings); fail(); } catch (IOException e) { assertTrue(true); @@ -373,7 +373,7 @@ public void testIsValidPackageList() throws Exception { proxy.setPassword("bar"); settings.addProxy(proxy); - JavadocUtil.isValidPackageList(url, settings, true); + JavadocUtil.isValidPackageList(url, settings); fail(); } catch (SocketTimeoutException e) { assertTrue(true); @@ -398,7 +398,7 @@ public void testIsValidPackageList() throws Exception { proxy.setNonProxyHosts("maven.apache.org"); settings.addProxy(proxy); - assertTrue(JavadocUtil.isValidPackageList(url, settings, true)); + assertTrue(JavadocUtil.isValidPackageList(url, settings)); } finally { proxyServer.stop(); }