【问题标题】:How can I compile and run my Custom Doclet class in my project?如何在我的项目中编译和运行我的自定义 Doclet 类?
【发布时间】:2013-02-06 01:05:50
【问题描述】:

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

到目前为止我有:

  • 创建了 doclet 类SiteMapDoclet
    • 该类被定义为扫描项目中的所有 javadocs 并将它们转储到 .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 中。

我的 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&lt;reporting&gt; 部分,现在该过程在构建过程中失败了。我添加了&lt;goals&gt; 并从&lt;reporting&gt; 复制了&lt;configuration&gt; 部分。

它仍在抛出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>

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

【问题讨论】:

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


    【解决方案1】:

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

    这就是我所做的:

    • 在构建部分中使用 javadoc 目标定义了插件。
      • 确保 tools.jar 和 rt.jar 在&lt;bootclasspath&gt;
      • &lt;docletPath&gt; 定义为\;.;${project.build.outputDirectory};
        • \;.; 是必需的,因为 maven 没有正确附加
        • 还必须在此处显式添加某些包的路径,以防止继承冲突版本。 (特别是 Log4J)
      • 定义了&lt;docletArtifacts&gt;,其中包含抛出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>
    

    【讨论】:

      【解决方案2】:

      您可以尝试将您的&lt;additionalDependencies&gt; 作为插件依赖项:

          <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>
      

      【讨论】:

      • 我仍然收到NoNoClassDefFoundError,但现在我在mvn package 上收到了它:) 我只需要从报告复制/粘贴&lt;configuration&gt; 即可构建。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多