Search in sources :

Example 31 with MavenReportException

use of org.apache.maven.reporting.MavenReportException in project maven-plugins by apache.

the class AbstractJavadocMojo method getArtifactsAbsolutePath.

/**
     * Return the Javadoc artifact path and its transitive dependencies path from the local repository
     *
     * @param javadocArtifact not null
     * @return a list of locale artifacts absolute path
     * @throws MavenReportException if any
     */
private List<String> getArtifactsAbsolutePath(JavadocPathArtifact javadocArtifact) throws MavenReportException {
    if ((StringUtils.isEmpty(javadocArtifact.getGroupId())) && (StringUtils.isEmpty(javadocArtifact.getArtifactId())) && (StringUtils.isEmpty(javadocArtifact.getVersion()))) {
        return Collections.emptyList();
    }
    List<String> path = new ArrayList<String>();
    try {
        Artifact artifact = createAndResolveArtifact(javadocArtifact);
        path.add(artifact.getFile().getAbsolutePath());
        DefaultDependableCoordinate coordinate = new DefaultDependableCoordinate();
        coordinate.setGroupId(javadocArtifact.getGroupId());
        coordinate.setArtifactId(javadocArtifact.getArtifactId());
        coordinate.setVersion(javadocArtifact.getVersion());
        Iterable<ArtifactResult> deps = dependencyResolver.resolveDependencies(session.getProjectBuildingRequest(), coordinate, ScopeFilter.including("compile", "provided"));
        for (ArtifactResult a : deps) {
            path.add(a.getArtifact().getFile().getAbsolutePath());
        }
        return path;
    } catch (ArtifactResolverException e) {
        throw new MavenReportException("Unable to resolve artifact:" + javadocArtifact, e);
    } catch (DependencyResolverException e) {
        throw new MavenReportException("Unable to resolve dependencies for:" + javadocArtifact, e);
    }
}
Also used : ArtifactResolverException(org.apache.maven.shared.artifact.resolve.ArtifactResolverException) DefaultDependableCoordinate(org.apache.maven.shared.dependencies.DefaultDependableCoordinate) ArrayList(java.util.ArrayList) DependencyResolverException(org.apache.maven.shared.dependencies.resolve.DependencyResolverException) 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) ArtifactResult(org.apache.maven.shared.artifact.resolve.ArtifactResult) MavenReportException(org.apache.maven.reporting.MavenReportException)

Example 32 with MavenReportException

use of org.apache.maven.reporting.MavenReportException in project maven-plugins by apache.

the class AbstractJavadocMojo method executeReport.

/**
     * The <a href="package-summary.html">package documentation</a> details the
     * Javadoc Options used by this Plugin.
     *
     * @param unusedLocale the wanted locale (actually unused).
     * @throws MavenReportException if any
     */
protected void executeReport(Locale unusedLocale) throws MavenReportException {
    if (skip) {
        getLog().info("Skipping javadoc generation");
        return;
    }
    if (isAggregator() && !project.isExecutionRoot()) {
        return;
    }
    if (getLog().isDebugEnabled()) {
        this.debug = true;
    }
    // useDependencySources in a distro module build.
    try {
        buildJavadocOptions();
    } catch (IOException e) {
        throw new MavenReportException("Failed to generate javadoc options file: " + e.getMessage(), e);
    }
    List<String> sourcePaths = getSourcePaths();
    List<String> files = getFiles(sourcePaths);
    if (!canGenerateReport(files)) {
        return;
    }
    List<String> packageNames = getPackageNames(sourcePaths, files);
    List<String> filesWithUnnamedPackages = getFilesWithUnnamedPackages(sourcePaths, files);
    // ----------------------------------------------------------------------
    // Find the javadoc executable and version
    // ----------------------------------------------------------------------
    String jExecutable;
    try {
        jExecutable = getJavadocExecutable();
    } catch (IOException e) {
        throw new MavenReportException("Unable to find javadoc command: " + e.getMessage(), e);
    }
    setFJavadocVersion(new File(jExecutable));
    // ----------------------------------------------------------------------
    // Javadoc output directory as File
    // ----------------------------------------------------------------------
    File javadocOutputDirectory = new File(getOutputDirectory());
    if (javadocOutputDirectory.exists() && !javadocOutputDirectory.isDirectory()) {
        throw new MavenReportException("IOException: " + getOutputDirectory() + " is not a directory.");
    }
    if (javadocOutputDirectory.exists() && !javadocOutputDirectory.canWrite()) {
        throw new MavenReportException("IOException: " + getOutputDirectory() + " is not writable.");
    }
    javadocOutputDirectory.mkdirs();
    // ----------------------------------------------------------------------
    // Copy all resources
    // ----------------------------------------------------------------------
    copyAllResources(javadocOutputDirectory);
    // ----------------------------------------------------------------------
    // Create command line for Javadoc
    // ----------------------------------------------------------------------
    Commandline cmd = new Commandline();
    // for Javadoc JVM args
    cmd.getShell().setQuotedArgumentsEnabled(false);
    cmd.setWorkingDirectory(javadocOutputDirectory.getAbsolutePath());
    cmd.setExecutable(jExecutable);
    // ----------------------------------------------------------------------
    // Wrap Javadoc JVM args
    // ----------------------------------------------------------------------
    addMemoryArg(cmd, "-Xmx", this.maxmemory);
    addMemoryArg(cmd, "-Xms", this.minmemory);
    addProxyArg(cmd);
    if (StringUtils.isNotEmpty(additionalJOption)) {
        cmd.createArg().setValue(additionalJOption);
    }
    if (additionalJOptions != null && additionalJOptions.length != 0) {
        for (String jo : additionalJOptions) {
            cmd.createArg().setValue(jo);
        }
    }
    List<String> arguments = new ArrayList<String>();
    // ----------------------------------------------------------------------
    // Wrap Javadoc options
    // ----------------------------------------------------------------------
    addJavadocOptions(arguments, sourcePaths);
    if (StringUtils.isEmpty(doclet) || useStandardDocletOptions) {
        addStandardDocletOptions(javadocOutputDirectory, arguments);
    }
    if (arguments.size() > 0) {
        addCommandLineOptions(cmd, arguments, javadocOutputDirectory);
    }
    // ----------------------------------------------------------------------
    // Write packages file and include it in the command line
    // ----------------------------------------------------------------------
    // MJAVADOC-365 if includes/excludes are specified, these take precedence over the default
    // package-based mode and force javadoc into file-based mode unless subpackages are
    // specified. Subpackages take precedence over file-based include/excludes. Why? Because
    // getFiles(...) returns an empty list when subpackages are specified.
    boolean includesExcludesActive = (sourceFileIncludes != null && !sourceFileIncludes.isEmpty()) || (sourceFileExcludes != null && !sourceFileExcludes.isEmpty());
    if (includesExcludesActive && !StringUtils.isEmpty(subpackages)) {
        getLog().warn("sourceFileIncludes and sourceFileExcludes have no effect when subpackages are specified!");
        includesExcludesActive = false;
    }
    if (!packageNames.isEmpty() && !includesExcludesActive) {
        addCommandLinePackages(cmd, javadocOutputDirectory, packageNames);
        if (!filesWithUnnamedPackages.isEmpty()) {
            addCommandLineArgFile(cmd, javadocOutputDirectory, filesWithUnnamedPackages);
        }
    } else {
        if (!files.isEmpty()) {
            addCommandLineArgFile(cmd, javadocOutputDirectory, files);
        }
    }
    // ----------------------------------------------------------------------
    // Execute command line
    // ----------------------------------------------------------------------
    executeJavadocCommandLine(cmd, javadocOutputDirectory);
    // prevent these files from making their way into archives.
    if (!debug) {
        for (int i = 0; i < cmd.getArguments().length; i++) {
            String arg = cmd.getArguments()[i].trim();
            if (!arg.startsWith("@")) {
                continue;
            }
            File argFile = new File(javadocOutputDirectory, arg.substring(1));
            if (argFile.exists()) {
                argFile.delete();
            }
        }
        File scriptFile = new File(javadocOutputDirectory, DEBUG_JAVADOC_SCRIPT_NAME);
        if (scriptFile.exists()) {
            scriptFile.delete();
        }
    }
    if (applyJavadocSecurityFix) {
        // finally, patch the Javadoc vulnerability in older Javadoc tools (CVE-2013-1571):
        try {
            final int patched = fixFrameInjectionBug(javadocOutputDirectory, getDocencoding());
            if (patched > 0) {
                getLog().info(String.format("Fixed Javadoc frame injection vulnerability (CVE-2013-1571) in %d files.", patched));
            }
        } catch (IOException e) {
            throw new MavenReportException("Failed to patch javadocs vulnerability: " + e.getMessage(), e);
        }
    } else {
        getLog().info("applying javadoc security fix has been disabled");
    }
}
Also used : Commandline(org.codehaus.plexus.util.cli.Commandline) ArrayList(java.util.ArrayList) IOException(java.io.IOException) File(java.io.File) MavenReportException(org.apache.maven.reporting.MavenReportException)

Example 33 with MavenReportException

use of org.apache.maven.reporting.MavenReportException in project maven-plugins by apache.

the class AbstractJavadocMojo method getClasspath.

/**
     * Method that sets the classpath elements that will be specified in the javadoc <code>-classpath</code>
     * parameter. Since we have all the sources of the current reactor, it is sufficient to consider the
     * dependencies of the reactor modules, excluding the module artifacts which may not yet be available
     * when the reactor project is built for the first time.
     *
     * @return a String that contains the concatenated classpath elements, separated by the System pathSeparator
     *         string (colon (<code>:</code>) on Solaris or semi-colon (<code>;</code>) on Windows).
     * @throws MavenReportException if any.
     * @see File#pathSeparator
     */
private String getClasspath() throws MavenReportException {
    List<String> classpathElements = new ArrayList<String>();
    Map<String, Artifact> compileArtifactMap = new HashMap<String, Artifact>();
    if (isTest()) {
        classpathElements.addAll(getProjectBuildOutputDirs(project));
    }
    populateCompileArtifactMap(compileArtifactMap, getProjectArtifacts(project));
    if (isAggregator() && project.isExecutionRoot()) {
        List<Artifact> reactorArtifacts = new ArrayList<Artifact>();
        for (MavenProject p : reactorProjects) {
            reactorArtifacts.add(p.getArtifact());
        }
        try {
            for (MavenProject subProject : reactorProjects) {
                if (subProject != project) {
                    classpathElements.addAll(getProjectBuildOutputDirs(subProject));
                    Set<Artifact> dependencyArtifacts = subProject.createArtifacts(factory, null, null);
                    // do not attempt to resolve artifacts of the current reactor which may not exist yet
                    dependencyArtifacts.removeAll(reactorArtifacts);
                    if (!dependencyArtifacts.isEmpty()) {
                        ArtifactResolutionResult result = null;
                        try {
                            result = resolver.resolveTransitively(dependencyArtifacts, subProject.getArtifact(), subProject.getManagedVersionMap(), localRepository, subProject.getRemoteArtifactRepositories(), artifactMetadataSource);
                        } catch (ArtifactNotFoundException e) {
                            throw new MavenReportException(e.getMessage(), e);
                        } catch (ArtifactResolutionException e) {
                            throw new MavenReportException(e.getMessage(), e);
                        }
                        if (result == null) {
                            continue;
                        }
                        populateCompileArtifactMap(compileArtifactMap, getCompileArtifacts(result.getArtifacts()));
                        if (getLog().isDebugEnabled()) {
                            StringBuilder sb = new StringBuilder();
                            sb.append("Compiled artifacts for ");
                            sb.append(subProject.getGroupId()).append(":");
                            sb.append(subProject.getArtifactId()).append(":");
                            sb.append(subProject.getVersion()).append('\n');
                            for (Artifact a : compileArtifactMap.values()) {
                                sb.append(a.getFile()).append('\n');
                            }
                            getLog().debug(sb.toString());
                        }
                    }
                }
            }
        } catch (InvalidDependencyVersionException e) {
            throw new MavenReportException(e.getMessage(), e);
        }
    }
    for (Artifact a : compileArtifactMap.values()) {
        classpathElements.add(a.getFile().toString());
    }
    if (additionalDependencies != null) {
        for (Dependency dependency : additionalDependencies) {
            Artifact artifact = resolveDependency(dependency);
            String path = artifact.getFile().toString();
            getLog().debug("add additional artifact with path " + path);
            classpathElements.add(path);
        }
    }
    return StringUtils.join(classpathElements.iterator(), File.pathSeparator);
}
Also used : HashMap(java.util.HashMap) InvalidDependencyVersionException(org.apache.maven.project.artifact.InvalidDependencyVersionException) ArrayList(java.util.ArrayList) Dependency(org.apache.maven.model.Dependency) 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) ArtifactResolutionException(org.apache.maven.artifact.resolver.ArtifactResolutionException) MavenProject(org.apache.maven.project.MavenProject) ArtifactResolutionResult(org.apache.maven.artifact.resolver.ArtifactResolutionResult) ArtifactNotFoundException(org.apache.maven.artifact.resolver.ArtifactNotFoundException) MavenReportException(org.apache.maven.reporting.MavenReportException)

Example 34 with MavenReportException

use of org.apache.maven.reporting.MavenReportException 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 35 with MavenReportException

use of org.apache.maven.reporting.MavenReportException in project maven-plugins by apache.

the class AbstractJavadocMojo method populateCompileArtifactMap.

/**
     * Method to put the artifacts in the hashmap.
     *
     * @param compileArtifactMap the hashmap that will contain the artifacts
     * @param artifactList       the list of artifacts that will be put in the map
     * @throws MavenReportException if any
     */
private void populateCompileArtifactMap(Map<String, Artifact> compileArtifactMap, Collection<Artifact> artifactList) throws MavenReportException {
    if (artifactList == null) {
        return;
    }
    for (Artifact newArtifact : artifactList) {
        File file = newArtifact.getFile();
        if (file == null) {
            throw new MavenReportException("Error in plugin descriptor - " + "dependency was not resolved for artifact: " + newArtifact.getGroupId() + ":" + newArtifact.getArtifactId() + ":" + newArtifact.getVersion());
        }
        if (compileArtifactMap.get(newArtifact.getDependencyConflictId()) != null) {
            Artifact oldArtifact = compileArtifactMap.get(newArtifact.getDependencyConflictId());
            ArtifactVersion oldVersion = new DefaultArtifactVersion(oldArtifact.getVersion());
            ArtifactVersion newVersion = new DefaultArtifactVersion(newArtifact.getVersion());
            if (newVersion.compareTo(oldVersion) > 0) {
                compileArtifactMap.put(newArtifact.getDependencyConflictId(), newArtifact);
            }
        } else {
            compileArtifactMap.put(newArtifact.getDependencyConflictId(), newArtifact);
        }
    }
}
Also used : DefaultArtifactVersion(org.apache.maven.artifact.versioning.DefaultArtifactVersion) ArtifactVersion(org.apache.maven.artifact.versioning.ArtifactVersion) DefaultArtifactVersion(org.apache.maven.artifact.versioning.DefaultArtifactVersion) 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) MavenReportException(org.apache.maven.reporting.MavenReportException)

Aggregations

MavenReportException (org.apache.maven.reporting.MavenReportException)48 File (java.io.File)23 IOException (java.io.IOException)23 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)14 ArrayList (java.util.ArrayList)10 Artifact (org.apache.maven.artifact.Artifact)7 FileOutputStream (java.io.FileOutputStream)6 Locale (java.util.Locale)6 BootclasspathArtifact (org.apache.maven.plugin.javadoc.options.BootclasspathArtifact)6 DocletArtifact (org.apache.maven.plugin.javadoc.options.DocletArtifact)6 JavadocPathArtifact (org.apache.maven.plugin.javadoc.options.JavadocPathArtifact)6 ResourcesArtifact (org.apache.maven.plugin.javadoc.options.ResourcesArtifact)6 TagletArtifact (org.apache.maven.plugin.javadoc.options.TagletArtifact)6 FileNotFoundException (java.io.FileNotFoundException)5 Writer (java.io.Writer)5 LinkedHashSet (java.util.LinkedHashSet)5 SiteRendererSink (org.apache.maven.doxia.siterenderer.sink.SiteRendererSink)5 OutputStreamWriter (java.io.OutputStreamWriter)4 MalformedURLException (java.net.MalformedURLException)3 HashMap (java.util.HashMap)3