Search in sources :

Example 1 with OfflineLink

use of org.apache.maven.plugin.javadoc.options.OfflineLink in project maven-plugins by apache.

the class AbstractJavadocMojo method getDefaultJavadocApiLink.

/**
     * @return if {@link #detectJavaApiLink}, the Java API link based on the {@link #javaApiLinks} properties and the
     *         value of the <code>source</code> parameter in the
     *         <code>org.apache.maven.plugins:maven-compiler-plugin</code>
     *         defined in <code>${project.build.plugins}</code> or in <code>${project.build.pluginManagement}</code>,
     *         or the {@link #fJavadocVersion}, or <code>null</code> if not defined.
     * @see #detectJavaApiLink
     * @see #javaApiLinks
     * @see #DEFAULT_JAVA_API_LINKS
     * @see <a href="http://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html#source">source parameter</a>
     * @since 2.6
     */
private OfflineLink getDefaultJavadocApiLink() {
    if (!detectJavaApiLink) {
        return null;
    }
    final String pluginId = "org.apache.maven.plugins:maven-compiler-plugin";
    float sourceVersion = fJavadocVersion;
    String sourceConfigured = getPluginParameter(project, pluginId, "source");
    if (sourceConfigured != null) {
        try {
            sourceVersion = Float.parseFloat(sourceConfigured);
        } catch (NumberFormatException e) {
            getLog().debug("NumberFormatException for the source parameter in the maven-compiler-plugin. " + "Ignored it", e);
        }
    } 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.: " + fJavadocVersion);
    }
    // CHECKSTYLE_OFF: MagicNumber
    String apiVersion = null;
    if (sourceVersion >= 1.3f && sourceVersion < 1.4f) {
        apiVersion = "1.3";
    } else if (sourceVersion >= 1.4f && sourceVersion < 1.5f) {
        apiVersion = "1.4";
    } else if (sourceVersion >= 1.5f && sourceVersion < 1.6f) {
        apiVersion = "1.5";
    } else if (sourceVersion >= 1.6f && sourceVersion < 1.7f) {
        apiVersion = "1.6";
    } else if (sourceVersion >= 1.7f && sourceVersion < 1.8f) {
        apiVersion = "1.7";
    } else if (sourceVersion >= 1.8f) {
        apiVersion = "1.8";
    }
    String javaApiLink = javaApiLinks.getProperty("api_" + apiVersion, null);
    if (getLog().isDebugEnabled()) {
        if (StringUtils.isNotEmpty(javaApiLink)) {
            getLog().debug("Found Java API link: " + javaApiLink);
        } else {
            getLog().debug("No Java API link found.");
        }
    }
    if (javaApiLink == null) {
        return null;
    }
    File javaApiPackageListFile = new File(getJavadocOptionsFile().getParentFile(), "package-list");
    OfflineLink link = new OfflineLink();
    link.setLocation(javaApiPackageListFile.getParentFile().getAbsolutePath());
    link.setUrl(javaApiLink);
    InputStream in = null;
    OutputStream out = null;
    try {
        in = this.getClass().getResourceAsStream("java-api-package-list-" + apiVersion);
        out = new FileOutputStream(javaApiPackageListFile);
        IOUtil.copy(in, out);
        out.close();
        out = null;
        in.close();
        in = null;
    } catch (IOException ioe) {
        logError("Can't get java-api-package-list-" + apiVersion + ": " + ioe.getMessage(), ioe);
        return null;
    } finally {
        IOUtil.close(in);
        IOUtil.close(out);
    }
    return link;
}
Also used : InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) OfflineLink(org.apache.maven.plugin.javadoc.options.OfflineLink)

Example 2 with OfflineLink

use of org.apache.maven.plugin.javadoc.options.OfflineLink in project maven-plugins by apache.

the class AbstractJavadocMojo method collectOfflineLinks.

private Set<OfflineLink> collectOfflineLinks() throws MavenReportException {
    Set<OfflineLink> result = new LinkedHashSet<OfflineLink>();
    OfflineLink javaApiLink = getDefaultJavadocApiLink();
    if (javaApiLink != null) {
        result.add(javaApiLink);
    }
    if (includeDependencySources) {
        try {
            resolveDependencyBundles();
        } catch (IOException e) {
            throw new MavenReportException("Failed to resolve javadoc bundles from dependencies: " + e.getMessage(), e);
        }
        if (isNotEmpty(dependencyJavadocBundles)) {
            for (JavadocBundle bundle : dependencyJavadocBundles) {
                JavadocOptions options = bundle.getOptions();
                if (options != null && isNotEmpty(options.getOfflineLinks())) {
                    result.addAll(options.getOfflineLinks());
                }
            }
        }
    }
    if (this.offlineLinks != null && this.offlineLinks.length > 0) {
        result.addAll(Arrays.asList(this.offlineLinks));
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) JavadocBundle(org.apache.maven.plugin.javadoc.resolver.JavadocBundle) JavadocOptions(org.apache.maven.plugin.javadoc.options.JavadocOptions) IOException(java.io.IOException) OfflineLink(org.apache.maven.plugin.javadoc.options.OfflineLink) MavenReportException(org.apache.maven.reporting.MavenReportException)

Example 3 with OfflineLink

use of org.apache.maven.plugin.javadoc.options.OfflineLink in project maven-plugins by apache.

the class AbstractJavadocMojo method getModulesLinks.

/**
     * Using Maven, a Javadoc link is given by <code>${project.url}/apidocs</code>.
     *
     * @return the detected Javadoc links using the Maven conventions for all modules defined in the current project
     *         or an empty list.
     * @throws MavenReportException if any
     * @see #detectOfflineLinks
     * @see #reactorProjects
     * @since 2.6
     */
private List<OfflineLink> getModulesLinks() throws MavenReportException {
    if (!detectOfflineLinks || isAggregator() || reactorProjects == null) {
        return Collections.emptyList();
    }
    getLog().debug("Trying to add links for modules...");
    Set<String> dependencyArtifactIds = new HashSet<String>();
    final Set<Artifact> dependencyArtifacts = project.getDependencyArtifacts();
    for (Artifact artifact : dependencyArtifacts) {
        dependencyArtifactIds.add(artifact.getId());
    }
    List<OfflineLink> modulesLinks = new ArrayList<OfflineLink>();
    String javadocDirRelative = PathUtils.toRelative(project.getBasedir(), getOutputDirectory());
    for (MavenProject p : reactorProjects) {
        if (!dependencyArtifactIds.contains(p.getArtifact().getId()) || (p.getUrl() == null)) {
            continue;
        }
        File location = new File(p.getBasedir(), javadocDirRelative);
        if (!location.exists()) {
            if (getLog().isDebugEnabled()) {
                getLog().debug("Javadoc directory not found: " + location);
            }
            String javadocGoal = getFullJavadocGoal();
            getLog().info("The goal '" + javadocGoal + "' has not been previously called for the module: '" + p.getId() + "'. Trying to invoke it...");
            File invokerDir = new File(project.getBuild().getDirectory(), "invoker");
            invokerDir.mkdirs();
            File invokerLogFile = FileUtils.createTempFile("maven-javadoc-plugin", ".txt", invokerDir);
            try {
                JavadocUtil.invokeMaven(getLog(), new File(localRepository.getBasedir()), p.getFile(), Collections.singletonList(javadocGoal), null, invokerLogFile);
            } catch (MavenInvocationException e) {
                logError("MavenInvocationException: " + e.getMessage(), e);
                String invokerLogContent = JavadocUtil.readFile(invokerLogFile, null);
                //   the JVM won't start (opposite of what it was).
                if (invokerLogContent != null && invokerLogContent.contains(JavadocUtil.ERROR_INIT_VM)) {
                    throw new MavenReportException(e.getMessage(), e);
                }
            } finally {
                // just create the directory to prevent repeated invocations..
                if (!location.exists()) {
                    getLog().warn("Creating fake javadoc directory to prevent repeated invocations: " + location);
                    location.mkdirs();
                }
            }
        }
        if (location.exists()) {
            String url = getJavadocLink(p);
            OfflineLink ol = new OfflineLink();
            ol.setUrl(url);
            ol.setLocation(location.getAbsolutePath());
            if (getLog().isDebugEnabled()) {
                getLog().debug("Added Javadoc offline link: " + url + " for the module: " + p.getId());
            }
            modulesLinks.add(ol);
        }
    }
    return modulesLinks;
}
Also used : MavenProject(org.apache.maven.project.MavenProject) MavenInvocationException(org.apache.maven.shared.invoker.MavenInvocationException) ArrayList(java.util.ArrayList) OfflineLink(org.apache.maven.plugin.javadoc.options.OfflineLink) File(java.io.File) JavadocPathArtifact(org.apache.maven.plugin.javadoc.options.JavadocPathArtifact) Artifact(org.apache.maven.artifact.Artifact) DocletArtifact(org.apache.maven.plugin.javadoc.options.DocletArtifact) BootclasspathArtifact(org.apache.maven.plugin.javadoc.options.BootclasspathArtifact) ResourcesArtifact(org.apache.maven.plugin.javadoc.options.ResourcesArtifact) TagletArtifact(org.apache.maven.plugin.javadoc.options.TagletArtifact) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) MavenReportException(org.apache.maven.reporting.MavenReportException)

Example 4 with OfflineLink

use of org.apache.maven.plugin.javadoc.options.OfflineLink in project maven-plugins by apache.

the class AbstractJavadocMojo method addLinkofflineArguments.

/**
     * Convenience method to process {@link #offlineLinks} values as individual <code>-linkoffline</code>
     * javadoc options.
     * <br/>
     * If {@link #detectOfflineLinks}, try to add javadoc apidocs according Maven conventions for all modules given
     * in the project.
     *
     * @param arguments a list of arguments, not null
     * @throws MavenReportException if any
     * @see #offlineLinks
     * @see #getModulesLinks()
     * @see <a href="http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javadoc.html#package-list">package-list spec</a>
     */
private void addLinkofflineArguments(List<String> arguments) throws MavenReportException {
    Set<OfflineLink> offlineLinksList = collectOfflineLinks();
    offlineLinksList.addAll(getModulesLinks());
    for (OfflineLink offlineLink : offlineLinksList) {
        String url = offlineLink.getUrl();
        if (StringUtils.isEmpty(url)) {
            continue;
        }
        url = cleanUrl(url);
        String location = offlineLink.getLocation();
        if (StringUtils.isEmpty(location)) {
            continue;
        }
        if (isValidJavadocLink(location, false)) {
            addArgIfNotEmpty(arguments, "-linkoffline", JavadocUtil.quotedPathArgument(url) + " " + JavadocUtil.quotedPathArgument(location), true);
        }
    }
}
Also used : OfflineLink(org.apache.maven.plugin.javadoc.options.OfflineLink)

Aggregations

OfflineLink (org.apache.maven.plugin.javadoc.options.OfflineLink)4 File (java.io.File)2 IOException (java.io.IOException)2 LinkedHashSet (java.util.LinkedHashSet)2 MavenReportException (org.apache.maven.reporting.MavenReportException)2 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Artifact (org.apache.maven.artifact.Artifact)1 BootclasspathArtifact (org.apache.maven.plugin.javadoc.options.BootclasspathArtifact)1 DocletArtifact (org.apache.maven.plugin.javadoc.options.DocletArtifact)1 JavadocOptions (org.apache.maven.plugin.javadoc.options.JavadocOptions)1 JavadocPathArtifact (org.apache.maven.plugin.javadoc.options.JavadocPathArtifact)1 ResourcesArtifact (org.apache.maven.plugin.javadoc.options.ResourcesArtifact)1 TagletArtifact (org.apache.maven.plugin.javadoc.options.TagletArtifact)1 JavadocBundle (org.apache.maven.plugin.javadoc.resolver.JavadocBundle)1 MavenProject (org.apache.maven.project.MavenProject)1 MavenInvocationException (org.apache.maven.shared.invoker.MavenInvocationException)1