You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by rf...@apache.org on 2019/01/19 15:37:12 UTC

[maven-javadoc-plugin] 01/01: [MJAVADOC-566] Use patterns for defaultJavadocApiLinks

This is an automated email from the ASF dual-hosted git repository.

rfscholte pushed a commit to branch MJAVADOC-566
in repository https://gitbox.apache.org/repos/asf/maven-javadoc-plugin.git

commit 3c380406ae84f6b299478caf43c6ead91f672b4a
Author: rfscholte <rf...@apache.org>
AuthorDate: Sat Jan 19 16:37:05 2019 +0100

    [MJAVADOC-566] Use patterns for defaultJavadocApiLinks
---
 .../maven/plugins/javadoc/AbstractJavadocMojo.java | 164 +++++++++------------
 .../maven/plugins/javadoc/JavadocReportTest.java   |   3 +-
 2 files changed, 69 insertions(+), 98 deletions(-)

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 3446c73..8d3f92c 100644
--- a/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java
+++ b/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java
@@ -132,8 +132,6 @@ import java.util.Map.Entry;
 import java.util.Properties;
 import java.util.Set;
 import java.util.StringTokenizer;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
 
 import static org.apache.maven.plugins.javadoc.JavadocUtil.toRelative;
 import static org.apache.maven.plugins.javadoc.JavadocUtil.toList;
@@ -171,43 +169,6 @@ public abstract class AbstractJavadocMojo
     public static final String TEST_JAVADOC_RESOURCES_ATTACHMENT_CLASSIFIER = "test-javadoc-resources";
 
     /**
-     * The default Javadoc API urls according the
-     * <a href="http://www.oracle.com/technetwork/java/javase/documentation/api-jsp-136079.html">Sun API
-     * Specifications</a>:
-     * <pre>
-     * &lt;javaApiLinks&gt;
-     *   &lt;property&gt;
-     *     &lt;name&gt;api_1.3&lt;/name&gt;
-     *     &lt;value&gt;http://docs.oracle.com/javase/1.3/docs/api/&lt;/value&gt;
-     *   &lt;/property&gt;
-     *   &lt;property&gt;
-     *     &lt;name&gt;api_1.4&lt;/name&gt;
-     *     &lt;value&gt;http://docs.oracle.com/javase/1.4.2/docs/api/&lt;/value&gt;
-     *   &lt;/property&gt;
-     *   &lt;property&gt;
-     *     &lt;name&gt;api_1.5&lt;/name&gt;
-     *     &lt;value&gt;http://docs.oracle.com/javase/1.5.0/docs/api/&lt;/value&gt;
-     *   &lt;/property&gt;
-     *   &lt;property&gt;
-     *     &lt;name&gt;api_1.6&lt;/name&gt;
-     *     &lt;value&gt;http://docs.oracle.com/javase/6/docs/api/&lt;/value&gt;
-     *   &lt;/property&gt;
-     *   &lt;property&gt;
-     *     &lt;name&gt;api_1.7&lt;/name&gt;
-     *     &lt;value&gt;http://docs.oracle.com/javase/7/docs/api/&lt;/value&gt;
-     *   &lt;/property&gt;
-     *   &lt;property&gt;
-     *     &lt;name&gt;api_1.8&lt;/name&gt;
-     *     &lt;value&gt;http://docs.oracle.com/javase/8/docs/api/&lt;/value&gt;
-     *   &lt;/property&gt;
-     * &lt;/javaApiLinks&gt;
-     * </pre>
-     *
-     * @since 2.6
-     */
-    public static final Properties DEFAULT_JAVA_API_LINKS = new Properties();
-
-    /**
      * The Javadoc script file name when <code>debug</code> parameter is on, i.e. javadoc.bat or javadoc.sh
      */
     protected static final String DEBUG_JAVADOC_SCRIPT_NAME = "javadoc." + ( SystemUtils.IS_OS_WINDOWS ? "bat" : "sh" );
@@ -606,15 +567,19 @@ public abstract class AbstractJavadocMojo
     private boolean detectJavaApiLink;
 
     /**
-     * Use this parameter <b>only</b> if the <a href="http://java.sun.com/reference/api/index.html">Sun Javadoc API</a>
-     * urls have been changed or to use custom urls for Javadoc API url.
-     * <br/>
-     * See
-     * <a href="./apidocs/org/apache/maven/plugin/javadoc/AbstractJavadocMojo.html#DEFAULT_JAVA_API_LINKS">Javadoc</a>
-     * for the default values.
-     * <br/>
-     *
-     * @see #DEFAULT_JAVA_API_LINKS
+     * Use this parameter <b>only</b> if if you want to override the default URLs.
+     * 
+     * The key should match {@code api_x}, where {@code x} matches the Java version.  
+     * 
+     *  For example:
+     *  <dl>
+     *   <dt>api_1.5</dt>
+     *   <dd>https://docs.oracle.com/javase/1.5.0/docs/api/</dd>
+     *   <dt>api_1.8<dt>
+     *   <dd>https://docs.oracle.com/javase/8/docs/api/</dd>
+     *   <dt>api_9</dd>
+     *   <dd>https://docs.oracle.com/javase/9/docs/api/</dd>
+     * </dl>
      * @since 2.6
      */
     @Parameter( property = "javaApiLinks" )
@@ -1765,21 +1730,6 @@ public abstract class AbstractJavadocMojo
     private Map<String, String> jdkToolchain;
 
     // ----------------------------------------------------------------------
-    // static
-    // ----------------------------------------------------------------------
-
-    static
-    {
-        DEFAULT_JAVA_API_LINKS.put( "api_1.5", "https://docs.oracle.com/javase/1.5.0/docs/api/" );
-        DEFAULT_JAVA_API_LINKS.put( "api_1.6", "https://docs.oracle.com/javase/6/docs/api/" );
-        DEFAULT_JAVA_API_LINKS.put( "api_1.7", "https://docs.oracle.com/javase/7/docs/api/" );
-        DEFAULT_JAVA_API_LINKS.put( "api_1.8", "https://docs.oracle.com/javase/8/docs/api/" );
-        DEFAULT_JAVA_API_LINKS.put( "api_9",   "https://docs.oracle.com/javase/9/docs/api/" );
-        DEFAULT_JAVA_API_LINKS.put( "api_10",  "https://docs.oracle.com/javase/10/docs/api/" );
-        DEFAULT_JAVA_API_LINKS.put( "api_11",  "https://docs.oracle.com/en/java/javase/11/docs/api/" );
-    }
-
-    // ----------------------------------------------------------------------
     // protected methods
     // ----------------------------------------------------------------------
 
@@ -4861,12 +4811,6 @@ public abstract class AbstractJavadocMojo
         {
             throw new MavenReportException( "Option <stylesheet/> supports only \"maven\" or \"java\" value." );
         }
-
-        // default java api links
-        if ( javaApiLinks == null || javaApiLinks.size() == 0 )
-        {
-            javaApiLinks = DEFAULT_JAVA_API_LINKS;
-        }
     }
 
     /**
@@ -6054,53 +5998,72 @@ public abstract class AbstractJavadocMojo
      * @see <a href="http://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html#source">source parameter</a>
      * @since 2.6
      */
-    private OfflineLink getDefaultJavadocApiLink()
+    protected final OfflineLink getDefaultJavadocApiLink()
     {
         if ( !detectJavaApiLink )
         {
             return null;
         }
 
-        final String pluginId = "org.apache.maven.plugins:maven-compiler-plugin";
-        JavaVersion sourceVersion = javadocRuntimeVersion;
-        String sourceConfigured = getPluginParameter( project, pluginId, "source" );
-        if ( sourceConfigured != null )
+        final JavaVersion javaApiversion;
+        if ( release != null )
         {
-            try
+            javaApiversion = JavaVersion.parse( release );
+        }
+        else
+        {
+            final String pluginId = "org.apache.maven.plugins:maven-compiler-plugin";
+            String sourceConfigured = getPluginParameter( project, pluginId, "source" );
+            if ( sourceConfigured != null )
             {
-                sourceVersion = JavaVersion.parse( sourceConfigured );
+                javaApiversion = JavaVersion.parse( sourceConfigured );
             }
-            catch ( NumberFormatException e )
+            else
             {
-                getLog().debug(
-                    "NumberFormatException for the source parameter in the maven-compiler-plugin. " + "Ignored it", e );
+                getLog().debug( "No maven-compiler-plugin defined in ${build.plugins} or in "
+                                    + "${project.build.pluginManagement} for the " + project.getId()
+                                    + ". Added Javadoc API link according the javadoc executable version i.e.: "
+                                    + javadocRuntimeVersion );
+                
+                javaApiversion = javadocRuntimeVersion;
             }
         }
+        
+        final String javaApiKey;
+        if ( javaApiversion.asMajor().isAtLeast( "9" ) )
+        {
+            javaApiKey = "api_" + javaApiversion.asMajor();
+        }
         else
         {
-            getLog().debug( "No maven-compiler-plugin defined in ${build.plugins} or in "
-                                + "${project.build.pluginManagement} for the " + project.getId()
-                                + ". Added Javadoc API link according the javadoc executable version i.e.: "
-                                + javadocRuntimeVersion );
+            javaApiKey = "api_1." + javaApiversion.asMajor().toString().charAt( 0 );
         }
-
-        String apiVersion;
         
-        Matcher apiMatcher = Pattern.compile( "(1\\.\\d|\\d\\d*)" ).matcher( sourceVersion.toString() );
-        if ( apiMatcher.find() )
+        final String javaApiLink;
+        if ( javaApiLinks != null && javaApiLinks.containsKey( javaApiKey ) )
         {
-            apiVersion = apiMatcher.group( 1 );
+            javaApiLink = javaApiLinks.getProperty( javaApiKey );  
+        }
+        else if ( javaApiversion.isAtLeast( "11" ) )
+        {
+            javaApiLink = String.format( "https://docs.oracle.com/en/java/javase/%s/docs/api/", javaApiversion );
+        }
+        else if ( javaApiversion.asMajor().isAtLeast( "6" ) )
+        {
+            javaApiLink = String.format( "https://docs.oracle.com/javase/%s/docs/api/", javaApiversion.asMajor() );
+        }
+        else if ( javaApiversion.isAtLeast( "1.5" ) )
+        {
+            javaApiLink = "https://docs.oracle.com/javase/1.5.0/docs/api/";
         }
         else
         {
-            apiVersion = null;
+            javaApiLink = null;
         }
-
-        String javaApiLink = javaApiLinks.getProperty( "api_" + apiVersion, null );
-
+        
         if ( getLog().isDebugEnabled() )
         {
-            if ( StringUtils.isNotEmpty( javaApiLink ) )
+            if ( javaApiLink != null )
             {
                 getLog().debug( "Found Java API link: " + javaApiLink );
             }
@@ -6117,16 +6080,23 @@ public abstract class AbstractJavadocMojo
 
         final Path javaApiListFile;
         final String resourceName;
-        if ( JavaVersion.parse( apiVersion ).isAtLeast( "10" ) )
+        if ( javaApiversion.isAtLeast( "10" ) )
         {
             javaApiListFile = getJavadocOptionsFile().getParentFile().toPath().resolve( "element-list" );
-            resourceName = "java-api-element-list-" + apiVersion;
+            resourceName = "java-api-element-list-" + javaApiversion;
+        }
+        else if ( javaApiversion.asMajor().isAtLeast( "9" ) )
+        {
+            javaApiListFile = getJavadocOptionsFile().getParentFile().toPath().resolve( "package-list" );
+            resourceName = "java-api-package-list-9";
         }
         else
         {
             javaApiListFile = getJavadocOptionsFile().getParentFile().toPath().resolve( "package-list" );
-            resourceName = "java-api-package-list-" + apiVersion;
+            resourceName = "java-api-package-list-1." + javaApiversion.asMajor().toString().charAt( 0 );
         }
+        
+        System.out.println( "ResourceName: " + resourceName );
 
         OfflineLink link = new OfflineLink();
         link.setLocation( javaApiListFile.getParent().toAbsolutePath().toString() );
@@ -6139,7 +6109,7 @@ public abstract class AbstractJavadocMojo
         }
         catch ( IOException ioe )
         {
-            logError( "Can't get java-api-package-list-" + apiVersion + ": " + ioe.getMessage(), ioe );
+            logError( "Can't get " + resourceName + ": " + ioe.getMessage(), ioe );
             return null;
         }
 
diff --git a/src/test/java/org/apache/maven/plugins/javadoc/JavadocReportTest.java b/src/test/java/org/apache/maven/plugins/javadoc/JavadocReportTest.java
index 0afc13e..0f2cec2 100644
--- a/src/test/java/org/apache/maven/plugins/javadoc/JavadocReportTest.java
+++ b/src/test/java/org/apache/maven/plugins/javadoc/JavadocReportTest.java
@@ -237,7 +237,8 @@ public class JavadocReportTest
         assertTrue( generatedFile.exists() );
 
         // only test when URL can be reached
-        String url = JavadocReport.DEFAULT_JAVA_API_LINKS.getProperty( "api_9" );
+        
+        String url = mojo.getDefaultJavadocApiLink().getUrl();
         HttpURLConnection connection = (HttpURLConnection) new URL( url ).openConnection();
         connection.setRequestMethod( "HEAD" );
         if ( connection.getResponseCode() == 200 )