@@ -147,6 +147,15 @@ public class JavadocJarMojo extends AbstractJavadocMojo {
147147 @ Parameter (property = "maven.javadoc.classifier" , defaultValue = "javadoc" , required = true )
148148 private String classifier ;
149149
150+ /**
151+ * Whether creating the archive should be forced. If set to true, the jar will always be created. If set to false,
152+ * the jar will only be created when Javadoc files exist.
153+ *
154+ * @since 3.11.0
155+ */
156+ @ Parameter (property = "maven.javadoc.forceCreation" , defaultValue = "false" )
157+ protected boolean forceCreation ;
158+
150159 /** {@inheritDoc} */
151160 @ Override
152161 protected void doExecute () throws MojoExecutionException {
@@ -172,7 +181,7 @@ protected void doExecute() throws MojoExecutionException {
172181 }
173182
174183 File javadocOutputDirectory = new File (getPluginReportOutputDirectory ());
175- if (javadocOutputDirectory .exists ()) {
184+ if (javadocOutputDirectory .exists () || forceCreation ) {
176185 try {
177186 File outputFile = generateArchive (javadocOutputDirectory , finalName + "-" + getClassifier () + ".jar" );
178187
@@ -221,23 +230,14 @@ protected String getClassifier() {
221230 * @throws IOException {@link IOException}
222231 */
223232 private File generateArchive (File javadocFiles , String jarFileName ) throws ArchiverException , IOException {
224- File javadocJar = new File (jarOutputDirectory , jarFileName );
225-
226- if (javadocJar .exists ()) {
227- javadocJar .delete ();
228- }
229-
230233 MavenArchiver archiver = new MavenArchiver ();
231- archiver .setCreatedBy ("Maven Javadoc Plugin" , "org.apache.maven.plugins" , "maven-javadoc-plugin" );
232234 archiver .setArchiver (jarArchiver );
233- archiver .setOutputFile ( javadocJar );
235+ archiver .setCreatedBy ( "Maven Javadoc Plugin" , "org.apache.maven.plugins" , "maven-javadoc-plugin" );
234236
235237 // configure for Reproducible Builds based on outputTimestamp value
236238 archiver .configureReproducibleBuild (outputTimestamp );
237239
238- if (!javadocFiles .exists ()) {
239- getLog ().warn ("JAR will be empty - no content was marked for inclusion!" );
240- } else {
240+ if (javadocFiles .exists ()) {
241241 archiver .getArchiver ().addDirectory (javadocFiles , DEFAULT_INCLUDES , DEFAULT_EXCLUDES );
242242 }
243243
@@ -254,14 +254,21 @@ private File generateArchive(File javadocFiles, String jarFileName) throws Archi
254254 archive .setManifestFile (defaultManifestFile );
255255 }
256256
257+ File outputFile = new File (jarOutputDirectory , jarFileName );
258+
259+ // Why do we do this?
260+ if (outputFile .exists ()) {
261+ outputFile .delete ();
262+ }
263+ archiver .setOutputFile (outputFile );
264+ archive .setForced (forceCreation );
265+
257266 try {
258267 archiver .createArchive (session , project , archive );
259- } catch (ManifestException e ) {
260- throw new ArchiverException ("ManifestException: " + e .getMessage (), e );
261- } catch (DependencyResolutionRequiredException e ) {
262- throw new ArchiverException ("DependencyResolutionRequiredException: " + e .getMessage (), e );
268+ } catch (ManifestException | DependencyResolutionRequiredException e ) {
269+ throw new ArchiverException ("Error creating Javadoc archive: " + e .getMessage (), e );
263270 }
264271
265- return javadocJar ;
272+ return outputFile ;
266273 }
267274}
0 commit comments