Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MJAVADOC-329] Allow generation of empty Javadoc JARs #331

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions src/main/java/org/apache/maven/plugins/javadoc/JavadocJarMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,15 @@ public class JavadocJarMojo extends AbstractJavadocMojo {
@Parameter(property = "maven.javadoc.classifier", defaultValue = "javadoc", required = true)
private String classifier;

/**
* Whether creating the archive should be forced. If set to true, the jar will always be created. If set to false,
* the jar will only be created when Javadoc files exist.
*
* @since 3.11.0
*/
@Parameter(property = "maven.javadoc.forceCreation", defaultValue = "false")
protected boolean forceCreation;

/** {@inheritDoc} */
@Override
protected void doExecute() throws MojoExecutionException {
Expand All @@ -172,7 +181,7 @@ protected void doExecute() throws MojoExecutionException {
}

File javadocOutputDirectory = new File(getPluginReportOutputDirectory());
if (javadocOutputDirectory.exists()) {
if (javadocOutputDirectory.exists() || forceCreation) {
try {
File outputFile = generateArchive(javadocOutputDirectory, finalName + "-" + getClassifier() + ".jar");

Expand Down Expand Up @@ -221,23 +230,14 @@ protected String getClassifier() {
* @throws IOException {@link IOException}
*/
private File generateArchive(File javadocFiles, String jarFileName) throws ArchiverException, IOException {
File javadocJar = new File(jarOutputDirectory, jarFileName);

if (javadocJar.exists()) {
javadocJar.delete();
}

MavenArchiver archiver = new MavenArchiver();
archiver.setCreatedBy("Maven Javadoc Plugin", "org.apache.maven.plugins", "maven-javadoc-plugin");
archiver.setArchiver(jarArchiver);
archiver.setOutputFile(javadocJar);
archiver.setCreatedBy("Maven Javadoc Plugin", "org.apache.maven.plugins", "maven-javadoc-plugin");

// configure for Reproducible Builds based on outputTimestamp value
archiver.configureReproducibleBuild(outputTimestamp);

if (!javadocFiles.exists()) {
getLog().warn("JAR will be empty - no content was marked for inclusion!");
} else {
if (javadocFiles.exists()) {
archiver.getArchiver().addDirectory(javadocFiles, DEFAULT_INCLUDES, DEFAULT_EXCLUDES);
}

Expand All @@ -254,14 +254,21 @@ private File generateArchive(File javadocFiles, String jarFileName) throws Archi
archive.setManifestFile(defaultManifestFile);
}

File outputFile = new File(jarOutputDirectory, jarFileName);

// Why do we do this?
if (outputFile.exists()) {
outputFile.delete();
}
archiver.setOutputFile(outputFile);
archive.setForced(forceCreation);

try {
archiver.createArchive(session, project, archive);
} catch (ManifestException e) {
throw new ArchiverException("ManifestException: " + e.getMessage(), e);
} catch (DependencyResolutionRequiredException e) {
throw new ArchiverException("DependencyResolutionRequiredException: " + e.getMessage(), e);
} catch (ManifestException | DependencyResolutionRequiredException e) {
throw new ArchiverException("Error creating Javadoc archive: " + e.getMessage(), e);
}

return javadocJar;
return outputFile;
}
}