Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 384
Misha Narinsky Avatar asked Feb 20 '13 19:02

Misha Narinsky


People also ask

Which command should you use to package javadoc into a jar file?

The “maven-javadoc” plugin uses “ JDK\bin\javadoc.exe ” command to generate javadocs, pack in jar file and deploy along with your project.

What is javadoc jar file?

The javadoc. jar contains a static html site which content is extracted from all the javadocs which are present in the Java source files.

How do I find javadoc jar?

jamdoc.jar To view an HTML documentation file, open your web browser and specify the file name of the javadoc you want to view, taken from the classdocs directory. Any of the following files are good for getting started: classdocs/AllNames. html.

How do I publish a javadoc?

In the Goals field, place javadoc:javadoc —this will tell Maven to generate the Javadoc documentation. Now go to the “Post-build Action” and tick the “Publish Javadoc” checkbox. This project is a multimodule project, so a separate subdirectory is generated for each module (core, services, web and so forth).


2 Answers

Well, as a matter of fact things can actually be done in a simpler way.

Create a sources jar artifact using the maven-source-plugin:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-source-plugin</artifactId>
        <version>2.2.1</version>
        <executions>
          <execution>
            <id>attach-sources</id>
            <phase>verify</phase>
            <goals>
              <goal>jar-no-fork</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  ...
</project>

This way your project will deploy the sources to your artifact repository.

Then in your IDE's Maven configuration, you can simply turn on downloading of source artifacts, effectively meaning that your IDE will have javadocs, if, of course, you have proper comments in your code.

This way you can have properly separated artifacts as well.

Other than that, David W. and ben75's answers are also valid ways.

like image 171
carlspring Avatar answered Oct 21 '22 19:10

carlspring


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>
like image 22
ben75 Avatar answered Oct 21 '22 19:10

ben75