Generate an empty -javadoc JAR file

If you read Sonatype OSS requirements to publish Maven artifacts on Maven Central, all the Maven modules with a JAR packaging must provide both -sources and -javadoc JAR files.

Using a profile for this is quite easy.
I handled it within my parent (this is a multi-module project and I wanted to write my configuration only once).

<!-- For releases, we need to generate javadoc and sources JAR -->
<profile>
	<id>release</id>
	<activation>
		<activeByDefault>false</activeByDefault>
	</activation>
	
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-source-plugin</artifactId>
				<version>2.3</version>
				<executions>
					<execution>
						<id>attach-sources</id>
						<goals>
							<goal>jar-no-fork</goal>
						</goals>
					</execution>
				</executions>
			</plugin>

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-javadoc-plugin</artifactId>
				<version>2.9.1</version>
				<executions>
					<execution>
						<id>attach-javadoc</id>
						<goals>
							<goal>jar</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</profile>

However, I had two children modules that were packaged as JAR but that did not contain any source file. Actually, they only contained resources. The default behavior of the maven-javadoc-plugin is then to generate nothing.

To comply with Sonatype requirements, I had to generate an empty JAR file.
In fact, I simply added the following code in the POM of the concerned modules.

<!-- We must generate a -javadoc JAR file to publish on Maven Central -->
<build>
	<plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-jar-plugin</artifactId>
			<executions>
				<execution>
					<id>empty-javadoc-jar</id>
					<phase>package</phase>
					<goals>
						<goal>jar</goal>
					</goals>
					<configuration>
						<classifier>javadoc</classifier>
						<classesDirectory>${basedir}/javadoc</classesDirectory>
					</configuration>
				</execution>
			</executions>
		</plugin>
	</plugins>
</build>

In these modules, I created a directory called javadoc at the same level than my POM, and I put a readme.md file. Now, their build generates a -javadoc.jar file that contains my readme.

To be completely clean, I should have attached this to my release profile. But given the time it takes, I can generate these almost empty JAR on every build.


About this entry