Search code examples
javamavenjarjavadoc

Create a single jar that will contain compiled classes and javadoc with Maven


I'm trying to generate a jar file that will contain an API SDK for our product, so our customers can create plugins and compile it against our API. All classes/interfaces that we provide as part of our API SDK jar are also included into our main product, so API developers won't need to include our API SDK jar into their plugin jar. Hence, I'm not worried about the size of our API SDK jar. However, I would like to make plugin developers' life easier and just provide one jar file that will contain both the compiled classes and the javadoc (so developers can see inline comments as part of the auto-complete feature as they develop).

We use Maven to compile and I added the following configuration to the API SDK pom.xml:

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

This works, however this generates two jar files - one with compiled classes and one with javadoc. Instead I would like to generate just one jar file with everything.

We currently use Maven to compile this project, however we are free to use other build tools.


Solution

  • You can do this :

    • attach the javadoc goal to the prepare package
    • specify the javadoc output directory to target/classes

    The jar plugin will create a jar with everything inside target/classes (including the generated javadocs)

        <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>2.7</version>
                <executions>
                    <execution>
                        <id>attach-javadoc</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>javadoc</goal>
                        </goals>
                        <configuration>
                            <reportOutputDirectory>${project.build.directory}/classes/</reportOutputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>