Search in sources :

Example 1 with OfflineLink

use of org.apache.maven.plugins.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.plugins.javadoc.options.OfflineLink)

Example 2 with OfflineLink

use of org.apache.maven.plugins.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 #javadocRuntimeVersion}, 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";
    JavadocVersion sourceVersion = javadocRuntimeVersion;
    String sourceConfigured = getPluginParameter(project, pluginId, "source");
    if (sourceConfigured != null) {
        try {
            sourceVersion = JavadocVersion.parse(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.: " + javadocRuntimeVersion);
    }
    String apiVersion;
    Matcher apiMatcher = Pattern.compile("(1\\.\\d|\\d\\d*)").matcher(sourceVersion.toString());
    if (apiMatcher.find()) {
        apiVersion = apiMatcher.group(1);
    } else {
        apiVersion = null;
    }
    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 : Matcher(java.util.regex.Matcher) 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.plugins.javadoc.options.OfflineLink)

Example 3 with OfflineLink

use of org.apache.maven.plugins.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 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.plugins.javadoc.resolver.JavadocBundle) JavadocOptions(org.apache.maven.plugins.javadoc.options.JavadocOptions) IOException(java.io.IOException) OfflineLink(org.apache.maven.plugins.javadoc.options.OfflineLink) MavenReportException(org.apache.maven.reporting.MavenReportException)

Example 4 with OfflineLink

use of org.apache.maven.plugins.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<>();
    final Set<Artifact> dependencyArtifacts = project.getDependencyArtifacts();
    for (Artifact artifact : dependencyArtifacts) {
        dependencyArtifactIds.add(artifact.getId());
    }
    List<OfflineLink> modulesLinks = new ArrayList<>();
    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.plugins.javadoc.options.OfflineLink) File(java.io.File) BootclasspathArtifact(org.apache.maven.plugins.javadoc.options.BootclasspathArtifact) JavadocPathArtifact(org.apache.maven.plugins.javadoc.options.JavadocPathArtifact) ResourcesArtifact(org.apache.maven.plugins.javadoc.options.ResourcesArtifact) Artifact(org.apache.maven.artifact.Artifact) TagletArtifact(org.apache.maven.plugins.javadoc.options.TagletArtifact) DocletArtifact(org.apache.maven.plugins.javadoc.options.DocletArtifact) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) MavenReportException(org.apache.maven.reporting.MavenReportException)

Aggregations

OfflineLink (org.apache.maven.plugins.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 Matcher (java.util.regex.Matcher)1 Artifact (org.apache.maven.artifact.Artifact)1 BootclasspathArtifact (org.apache.maven.plugins.javadoc.options.BootclasspathArtifact)1 DocletArtifact (org.apache.maven.plugins.javadoc.options.DocletArtifact)1 JavadocOptions (org.apache.maven.plugins.javadoc.options.JavadocOptions)1 JavadocPathArtifact (org.apache.maven.plugins.javadoc.options.JavadocPathArtifact)1 ResourcesArtifact (org.apache.maven.plugins.javadoc.options.ResourcesArtifact)1 TagletArtifact (org.apache.maven.plugins.javadoc.options.TagletArtifact)1 JavadocBundle (org.apache.maven.plugins.javadoc.resolver.JavadocBundle)1 MavenProject (org.apache.maven.project.MavenProject)1