【问题标题】:Mojo development : access to javadoc com.sun.tools.javadoc.Main classMojo开发:访问javadoc com.sun.tools.javadoc.Main 类
【发布时间】:2014-10-17 09:16:15
【问题描述】:

问题:我想从 Mojo 插件中的代码访问 com.sun.tools.javadoc.Main。

我对这个问题有两个部分。

第 1 部分:

创建mojo插件时,最好在@Mojo里面使用注解或者参数 例如,您可以在两者中设置“requiresDependencyResolution”。

 /*
 * @goal install
 * @phase process-classes
 * @configurator include-project-dependencies
 * @requiresDependencyResolution compile+runtime
 */
@Mojo(name = "document", requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME)
public class CreatorMavenPlugin extends AbstractMojo

第 2 部分:(主要问题)

我想在我的插件中执行以下代码,我想挂钩到 Javadoc 生成。

 com.sun.tools.javadoc.Main.execute(new String[]
    {
        "-private",
        "-doclet",
        "com.test.tools.APIDocGenDoclet",
        javaFilePathAndName
    });

    return APIDocGenDoclet.getCurrentClassDocs();

问题在于 Eclipse 从它拥有的 JDK 中识别出 com.sun.tools.javadoc.Main。

Maven 运行时找不到类并报错......

Number of foreign imports: 1
import: Entry[import  from realm ClassRealm[maven.api, parent: null]]

-----------------------------------------------------

        at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:125)
        ... 20 more
Caused by: java.lang.NoClassDefFoundError: com/sun/tools/javadoc/Main

我尝试直接添加工具以作为依赖项开始...

    <dependency>
        <groupId>com.sun</groupId>
        <artifactId>tools</artifactId>
        <version>1.6.0</version>
        <scope>system</scope>
        <systemPath>${java.home}/../lib/tools.jar</systemPath>
    </dependency>

但这不起作用。 (同样的错误)

我已尝试将其添加为我的插件在其下运行的配置文件的依赖项...

    <profile>
        <id>auto-doc</id>
        <dependencies>
            <dependency>
                <groupId>com.sun</groupId>
                <artifactId>tools</artifactId>
                <version>1.6.0</version>
                <scope>system</scope>
                <systemPath>${java.home}/../lib/tools.jar</systemPath>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>com.test</groupId>
                    <artifactId>updater</artifactId>
                    <version>1.0.0-SNAPSHOT</version>
                    <executions>
                        <execution>
                            <phase>install</phase>
                            <goals>
                                <goal>document</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>

但没有改变,同样的错误。

我觉得这与为 Mojo 插件定义的类路径设置有关,但我尝试了许多不同的组合,但我被难住了。

有人可以帮忙吗? 注意:我在 Windows 上使用 Maven 3.0.4 和 JDK 1.6.0_43 32 位。

【问题讨论】:

    标签: java maven maven-3 maven-plugin


    【解决方案1】:

    第 1 部分:如果您不需要向后兼容古老的 Maven 版本,请使用注释。这是指定 Mojo 配置的新的更好的方法,您的 IDE 将为您提供自动完成和悬停。

    第 2 部分:

    看看existing Maven Javadoc plugin does是什么:

    它似乎使用Toolchain API 来定位要运行的适当Javadoc 工具。 Maven Compiler Plugin 也这样做是为了获取 javac。

    从工具链文档中总结:

    添加

    @Component
    private ToolchainManager toolchainManager;
    
    @Component
    private MavenSession session;
    

    到你的魔咒。然后在你的代码中

    Toolchain tc = toolchainManager.getToolchainFromBuildContext( "jdk", session );
    String javadocExecutable = tc.findTool( "javadoc" );
    

    然后你就可以执行它了。阅读链接中的工具链文档以获取更多详细信息。

    如果您使用的是 Java 8 或更高版本,则可以使用 ToolProvider.getSystemDocumentationTool()

    【讨论】:

    • Part2:我想挂钩到javadoc生成,而不是直接调用javadoc命令。为此,我希望调用我的方法“public static boolean start(RootDoc root)”,以便可以访问 ClassDoc 对象。对这个有任何想法吗?我可以正常调用代码,但在 Maven 中我得到的没有类 def com/sun/tools/javadoc/Main。
    • @jeffporter 我认为这是因为 Maven 为您的插件提供了一个在类路径中看不到 tools.jar 的类加载器。如果您需要您的插件来查看 tools.jar,您需要将 tools.jar 依赖项添加到您的插件本身(依赖项元素位于 plugin 元素下方,而不是直接位于项目依赖项下方)。
    • 感谢 Prunge !这让我找到了解决方案:-)
    【解决方案2】:

    我将 Profile 部分放在正在构建的项目中,而不是放在包含 Mojo 插件的项目中。

    这就是我在 Mojo 插件项目中得到的东西

    <profiles>
        <profile>
            <id>default-tools.jar</id>
            <activation>
                <property>
                    <name>java.vendor</name>
                    <value>Sun Microsystems Inc.</value>
                </property>
            </activation>
            <dependencies>
                <dependency>
                    <groupId>com.sun</groupId>
                    <artifactId>tools</artifactId>
                    <version>1.6.0</version>
                    <scope>system</scope>
                    <systemPath>${java.home}/../lib/tools.jar</systemPath>
                </dependency>
            </dependencies>
        </profile>
    </profiles>
    

    Mojo 插件是这样定义的...

    /**
     * @goal install
     * @phase process-classes
     * @configurator include-project-dependencies
     * @requiresDependencyResolution compile+runtime
     */
    @Mojo(name = "document", requiresDependencyResolution= ResolutionScope.COMPILE_PLUS_RUNTIME)
    public class DocumationUpdatorMavenPlugin extends AbstractMojo 
    

    我的项目使用了这个插件,在它的 POM 中有这个...

    <profile>
                <id>auto-doc</id>
                <build>
                    <plugins>
                        <plugin>
                            <groupId>com.test</groupId>
                            <artifactId>updater</artifactId>
                            <version>1.0.0-SNAPSHOT</version>
                            <executions>
                                <execution>
                                    <phase>install</phase>
                                    <goals>
                                        <goal>document</goal>
                                    </goals>
                                </execution>
                            </executions>
                        </plugin>
                    </plugins>
                </build>
            </profile>  
    

    然后我的命令“mvn install -Pauto-doc”启动我的 AutoDoc 插件。

    【讨论】:

      猜你喜欢
      • 2012-03-13
      • 2012-01-26
      • 1970-01-01
      • 2014-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-14
      • 1970-01-01
      相关资源
      最近更新 更多