简体   繁体   English

Mojo开发:访问javadoc com.sun.tools.javadoc.Main类

[英]Mojo development : access to javadoc com.sun.tools.javadoc.Main class

Problem : I want to access com.sun.tools.javadoc.Main from code in a Mojo plugin. 问题:我想从Mojo插件中的代码访问com.sun.tools.javadoc.Main。

I have two parts to this question. 对于这个问题,我分为两个部分。

Part 1: 第1部分:

When creating a mojo plugin, is it best to use annotations or parameters inside the @Mojo For example you can set 'requiresDependencyResolution' in both. 创建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

Part 2: (Main question) 第2部分:(主要问题)

I want to execute the following code in my plugin, I want to hook into the Javadoc generation. 我想在我的插件中执行以下代码,我想加入Javadoc一代。

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

    return APIDocGenDoclet.getCurrentClassDocs();

The problem is that eclipse recognises com.sun.tools.javadoc.Main from the JDK it has. 问题在于eclipse可以从其拥有的JDK识别com.sun.tools.javadoc.Main。

Maven when it runs can't find the class and gives the error.... 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

I've tried adding tools directly to start with as a dependency... 我尝试直接添加工具作为依赖项开始...

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

But this doesn't work. 但这是行不通的。 (Same error) (相同的错误)

I've tried adding it as a dependency of the profile my plugin runs under... 我尝试将其添加为我的插件在...下运行的配置文件的依赖项...

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

But not change, same error. 但不变,同样的错误。

I feel this is something to do with the classpath setup defined for the Mojo Plugin, but I've tried many different combinations, but I'm stumped. 我觉得这与为Mojo插件定义的类路径设置有关,但是我尝试了许多不同的组合,但是我很困惑。

Can anyone help please? 有人可以帮忙吗? Note: I'm using Maven 3.0.4 & JDK 1.6.0_43 32 bit on Windows. 注意:我在Windows上使用Maven 3.0.4&JDK 1.6.0_43 32位。

Part 1: if you don't need backward compatibility with ancient Maven versions, go with the annotations. 第1部分:如果您不需要与旧版Maven版本向后兼容,请使用批注。 It's the new and better way of specifying Mojo configuration, and your IDE will give you autocomplete and hovers. 这是指定Mojo配置的新的更好的方法,IDE将为您提供自动完成和悬停功能。

Part 2: 第2部分:

Taking a look at what the existing Maven Javadoc plugin does : 看一下现有的Maven Javadoc插件的功能

It seems it uses the Toolchain API to locate the appropriate Javadoc tool to run. 似乎它使用工具链API来找到要运行的适当Javadoc工具。 The Maven Compiler Plugin also does this to get javac. Maven编译器插件也这样做来获取javac。

Summarizing from the toolchain documentation: 从工具链文档中总结:

add

@Component
private ToolchainManager toolchainManager;

@Component
private MavenSession session;

to your Mojo. 给你的魔咒 Then in your code 然后在你的代码中

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

and then you can execute it. 然后可以执行它。 Read the toolchain documentation in the link for more detailed information. 阅读链接中的工具链文档以获取更多详细信息。

If you were using Java 8 or later, you could use ToolProvider.getSystemDocumentationTool() . 如果您使用的是Java 8或更高版本,则可以使用ToolProvider.getSystemDocumentationTool()

I was putting the Profile section in the project that was being built, not in the project that contains the Mojo Plugin. 我将“个人档案”部分放在正在构建的项目中,而不是放在包含Mojo插件的项目中。

This is what I've got in Mojo Plugin Project 这就是我在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>

The Mojo plugin is defined as this... 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 

My project that uses the plugin, has this in its POM... 我的使用插件的项目在其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>  

Then my command "mvn install -Pauto-doc" kicks off my AutoDoc plugin. 然后,我的命令“ mvn install -Pauto-doc”启动了我的AutoDoc插件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 com.sun.tools.javadoc.Main.execute在jdk 11中运行Doclet的替代方法是什么? - What is the alternative of com.sun.tools.javadoc.Main.execute to run Doclet in jdk 11? Javadoc生成失败:ClassCastException:com.sun.tools.javadoc.ClassDocImpl无法强制转换为com.sun.javadoc.AnnotationTypeDoc - Javadoc generation failed : ClassCastException: com.sun.tools.javadoc.ClassDocImpl cannot be cast to com.sun.javadoc.AnnotationTypeDoc tools.jar 未打包到 maven package 中。 获取 java.lang.NoClassDefFoundError: com/sun/tools/javadoc/Main - tools.jar not packaged into the maven package. Getting java.lang.NoClassDefFoundError: com/sun/tools/javadoc/Main 取决于Eclipse中tools.jar(Sun JDK)的com.sun.javadoc - Depending on com.sun.javadoc from tools.jar (Sun JDK) in Eclipse 查找Maven依赖项“ com.sun.javadoc” - Find maven dependency “com.sun.javadoc” 如何在eclipse中导入com.sun.javadoc。*? - How to import com.sun.javadoc.* in eclipse? Android javadoc,com.sun.tools.javac.util.FatalError:致命错误:无法在类路径或引导类路径中找到包java.lang - Android javadoc, com.sun.tools.javac.util.FatalError: Fatal Error: Unable to find package java.lang in classpath or bootclasspath 导入com.sun.javadoc无法在Eclipse中解析 - The import com.sun.javadoc cannot be resolved in Eclipse 如何在com.sun.javadoc.Taglet中获取{@docroot}的值? - How to get value of {@docroot} in a com.sun.javadoc.Taglet? 导入com.sun.javadoc,与Eclipse和Ant结合使用 - Import com.sun.javadoc, using with Eclipse and Ant
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM