如何在项目中编译和运行Custom Doclet类?

时间:2013-02-20 21:04:25

标签: maven maven-2 javadoc maven-javadoc-plugin doclet

我正在尝试在编译时将所有类javadoc注释(最好是库WebPage类的子类)转换为classname=comment格式的.properties文件。

到目前为止,我有:

  • 创建了doclet类SiteMapDoclet
    • 该类被定义为扫描项目中的所有javadoc并将它们转储到.properties文件
  • 为我的pom.xml添加了必要的配置以使其正常工作。

版本:Java 1.6.0.21,Maven 2.2.1

问题:
mvn site返回:

Embedded error: Error rendering Maven report: 
Exit code: 1 - java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    at us.ak.state.revenue.cssd.Personnel.utils.SiteMapDoclet.<clinit>(SiteMapDoclet.java:27)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)

我尝试将jar设置为AdditionalDependencies,即使它们是我项目的正常依赖项。 我也尝试将路径添加到jar中,我希望我的类需要作为bootclasspath的一部分。

我的pom.xml的报告部分如下所示:

<reporting>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-javadoc-plugin</artifactId>
      <version>2.9</version>
      <reportSets>
        <reportSet>
          <id>html</id>
          <reports>
            <report>javadoc</report>
          </reports>
        </reportSet>
        <reportSet>
          <id>siteMap</id>
          <configuration>
            <doclet>
              us.ak.state.revenue.cssd.Personnel.utils.SiteMapDoclet
            </doclet>
            <docletPath>${project.build.outputDirectory}</docletPath>
            <destDir>SiteMap</destDir>

            <author>false</author>
            <useStandardDocletOptions>false</useStandardDocletOptions>
            <!-- there has got to be a better way to do this! -->
            <!-- how can I fix the CSSD-Web - Base to use a proper manifest file? -->
            <bootclasspath>
              ${bootClassPath};
              ${env.CLASSPATH};
              ${m2Repository}/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar;
              ${m2Repository}/org/apache/wicket/wicket-core/${wicket.version}/wicket-core-${wicket.version}.jar;
              ${m2Repository}/us/ak/state/revenue/cssd/CSSD-Web/${CSSDWebBase.version}/CSSD-Web-${CSSDWebBase.version}.jar
            </bootclasspath>
            <additionalDependencies>
              <additionalDependency>
                <groupId>us.ak.state.revenue.cssd</groupId>
                <artifactId>CSSD-Web</artifactId>
                <version>${CSSDWebBase.version}</version>
              </additionalDependency>
              <additionalDependency>
                <groupId>org.apache.wicket</groupId>
                <artifactId>wicket-core</artifactId>
                <version>${wicket.version}</version>
              </additionalDependency>
              <additionalDependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.16</version>
              </additionalDependency>
              <additionalDependency>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
                <version>1.1.1</version>
              </additionalDependency>
            </additionalDependencies>

            <name>SiteMapDoclet</name>
            <description>Page Descriptions for SiteMap generation</description>
          </configuration>
          <reports>
            <report>javadoc</report>
          </reports>
        </reportSet>
      </reportSets>
    </plugin>
  </plugins>
</reporting>

注意:
${m2Repository}被定义为文件上方的属性,
定义为${env.USERPROFILE}/.m2/repository
${bootClassPath}被定义为文件上方的属性,
定义为${env.JRE_6_HOME}\lib\rt.jar;${env.JAVA_HOME}\lib\tools.jar;

如何修复NoClassDefFoundError

此外,我希望我的SiteMap文件作为正常构建过程的一部分运行, 在compile之后但在package之前。

我已尝试在build中定义此内容,但javadoc未创建,我也没有看到来自我的Doclet的任何日志记录输出。

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-javadoc-plugin</artifactId>
    <version>2.9</version>
    <executions>
      <execution>
        <id>build-siteMap-Descriptions</id>
        <phase>process-classes</phase>
      </execution>
    </executions>
  </plugin>

更新
感谢@ ben75的建议。我已删除 pom.xml <reporting>部分,现在在构建过程中进程失败。我添加了<goals>并从<configuration>复制了<reporting>部分。

它仍在抛出NoClassDefFoundError,但它正在我想要的构建中发生。我尝试添加:

<includeDependencySources>true</includeDependencySources>
<dependencySourceIncludes>
  <dependencySourceInclude>org.apache.wicket:wicket-core:*</dependencySourceInclude>
  <dependencySourceInclude>org.apache.commons.logging:*</dependencySourceInclude>
  <dependencySourceInclude>us.ak.state.revenue.cssd:CSSD-Web:*</dependencySourceInclude>
</dependencySourceIncludes>

到配置部分,但是没有用。

2 个答案:

答案 0 :(得分:1)

您可以尝试将<additionalDependencies>作为插件依赖项:

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-javadoc-plugin</artifactId>
      <version>2.9</version>
      <dependencies>
         <dependency>
            <groupId>us.ak.state.revenue.cssd</groupId>
            <artifactId>CSSD-Web</artifactId>
            <version>${CSSDWebBase.version}</version>
         </dependency>
         ...

要将javadoc插件附加到正常的构建过程中,我认为您只需要指定目标并最好将其附加到prepare-package阶段(以便在您只运行测试阶段时不会生成javadoc):

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

答案 1 :(得分:0)

建立@ ben75s优秀的建议我能够让它最终运行。
这“有效”。感觉不对,我很想看到更好的方法。

这就是我的所作所为:

  • 使用javadoc目标在构建部分中定义插件。
    • 确保tools.jar和rt.jar位于<bootclasspath>
    • <docletPath>定义为\;.;${project.build.outputDirectory};
      • \;.;是必要的,因为maven无法正确追加
      • 还必须在此处明确添加某些包的路径,以防止冲突版本的继承。 (特别是Log4J)
    • 使用投放<docletArtifacts>
    • 的类的包定义NoClassDefFoundError

我的插件现在看起来像这样:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-javadoc-plugin</artifactId>
    <version>2.9</version>
    <executions>
      <execution>
        <id>build-siteMap-Descriptions</id>
        <phase>process-classes</phase>
        <goals>
          <!--<goal>aggregate</goal>-->
          <goal>javadoc</goal>
        </goals>
        <configuration>
          <doclet>
            us.ak.state.revenue.cssd.Personnel.utils.SiteMapDoclet
          </doclet>
          <!-- the initial '\;.;' is required
               because maven doesn't separate the path statements properly

               The 5 packages are necessary
               because otherwise slf4j complains about multiple bindings
          -->
          <docletPath>
            \;.;${project.build.outputDirectory};
            ${m2Repository}/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar;
            ${m2Repository}/log4j/log4j/1.2.16/log4j-1.2.16.jar;
            ${m2Repository}/log4j/apache-log4j-extras/1.1/apache-log4j-extras-1.1.jar;
            ${m2Repository}/us/ak/state/revenue/cssd/CSSD-Web/${CSSDWebBase.version}/CSSD-Web-${CSSDWebBase.version}.jar;
            ${m2Repository}/org/apache/wicket/wicket-core/${wicket.version}/wicket-core-${wicket.version}.jar;
            ${m2Repository}/org/apache/wicket/wicket-util/${wicket.version}/wicket-util-${wicket.version}.jar;
          </docletPath>
          <docletArtifacts>
            <!--
            <docletArtifact>
              <groupId>commons-logging</groupId>
              <artifactId>commons-logging</artifactId>
              <version>1.1.1</version>
            </docletArtifact>
            -->
            <docletArtifact>
              <groupId>org.slf4j</groupId>
              <artifactId>slf4j-log4j12</artifactId>
              <version>1.6.2</version>
            </docletArtifact>
            <!-- how do I fix the download errors? -->
            <!--
            <docletArtifact>
              <groupId>org.slf4j</groupId>
              <artifactId>slf4j-api</artifactId>
              <version>1.6.2</version>
            </docletArtifact>
            -->
            <!--
            <artifact>
              <groupId>log4j</groupId>
              <artifactId>log4j</artifactId>
              <version>1.2.16</version>
            </artifact>
            -->
            <!--
            <docletArtifact>
              <groupId>log4j</groupId>
              <artifactId>apache-log4j-extras</artifactId>
              <version>1.1</version>
            </docletArtifact>
            <docletArtifact>
              <groupId>us.ak.state.revenue.cssd</groupId>
              <artifactId>CSSD-Web</artifactId>
              <version>${CSSDWebBase.version}</version>
            </docletArtifact>
            <docletArtifact>
              <groupId>org.apache.wicket</groupId>
              <artifactId>wicket-core</artifactId>
              <version>${wicket.version}</version>
            </docletArtifact>
            -->
          </docletArtifacts>

          <!-- the initial '\;.;' is required
               because maven doesn't separate the path statements properly -->
          <bootclasspath>
            \;.;
            ${bootClassPath};
            ${env.CLASSPATH};
          </bootclasspath>

          <destDir>SiteMap</destDir>

          <author>false</author>
          <!-- don't print the packages/classes it's running on -->
          <quiet>true</quiet>
          <debug>true</debug> <!-- save options -->
          <useStandardDocletOptions>false</useStandardDocletOptions>

          <name>SiteMapDoclet</name>
          <description>Page Descriptions for SiteMap generation</description>
        </configuration>
      </execution>
    </executions>
  </plugin>