1

I had a customized java doclet, it will call newInstance() on some classes:

clazz.newInstance()

When I ran it in Eclipse as Junit, it works well.

But When I ran it in maven , it throws an exception: NoClassDefFoundError

I checked that class, it is not in my current project, it is inside a maven dependency (third party jar).

I knew I can set classpath in maven-javadoc-plugin. But my project has more than 50 third party jars. How can I set those jars easily?

This is my code in maven pom:

            <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
            <version>3.0.0-M1</version>
            <configuration>
                <doclet>my.company.common.doclet.MyDoclet</doclet>

                <docletPath>${project.build.directory}/../../shared-java/target/classes;${project.build.directory}/classes</docletPath>
                <sourcePath>${project.build.directory}/../../shared-java/src/java;${project.build.directory}/../src/java</sourcePath>
                <encoding>UTF-8</encoding>
                <show>public</show>
                <subpackages>my.company.api</subpackages>
                <useStandardDocletOptions>false</useStandardDocletOptions>
                <docletArtifacts>                       
                    <dependency>
                        <groupId>log4j</groupId>
                        <artifactId>log4j</artifactId>
                        <version>${log4j.version}</version>
                    </dependency>                       
                </docletArtifacts>

            </configuration>
            <executions>
                <execution>
                    <id>attach-javadoc</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

Thanks!

Justin
  • 1,050
  • 11
  • 26

2 Answers2

1

What if adding them with the help of <additionalDependencies>?

        <configuration>
          ...
          <additionalDependencies>
            <additionalDependency>
              <groupId>log4j</groupId>
              <artifactId>log4j</artifactId>
              <version>${log4j.version}</version>
            </additionalDependency>
          </additionalDependencies>
          ...
        </configuration>

See: https://maven.apache.org/plugins/maven-javadoc-plugin/examples/additional-dependencies.html

Radoslav Ivanov
  • 970
  • 8
  • 23
  • I confirm that your solution works: https://jogamp.org/cgit/ardor3d.git/commit/?id=44201145ac6f6f282bcb9f6fd229179503626fc0 It helped me to fix "mvn site". This change wasn't necessary in older versions of Maven one year ago. Actually, in my humble opinion, this answer should be the accepted answer, it's more accurate and it solves the problem. – gouessej Sep 03 '21 at 22:02
0

I found the maven-javadoc-plugin do pass the whole classpath to my doclet. But the doclet ClassLoader.getSystemClassLoader() didn't use that classpath.

So I have to manually add those classpath into the current classLoader.

In Eclipse, it will automatically set those classpath.

Justin
  • 1,050
  • 11
  • 26