![](/img/trans.png)
[英]What is the alternative of com.sun.tools.javadoc.Main.execute to run Doclet in jdk 11?
[英]Mojo development : access to javadoc com.sun.tools.javadoc.Main class
問題:我想從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位。
第1部分:如果您不需要與舊版Maven版本向后兼容,請使用批注。 這是指定Mojo配置的新的更好的方法,IDE將為您提供自動完成和懸停功能。
第2部分:
似乎它使用工具鏈API來找到要運行的適當Javadoc工具。 Maven編譯器插件也這樣做來獲取javac。
從工具鏈文檔中總結:
加
@Component
private ToolchainManager toolchainManager;
@Component
private MavenSession session;
給你的魔咒 然后在你的代碼中
Toolchain tc = toolchainManager.getToolchainFromBuildContext( "jdk", session );
String javadocExecutable = tc.findTool( "javadoc" );
然后可以執行它。 閱讀鏈接中的工具鏈文檔以獲取更多詳細信息。
如果您使用的是Java 8或更高版本,則可以使用ToolProvider.getSystemDocumentationTool() 。
我將“個人檔案”部分放在正在構建的項目中,而不是放在包含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插件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.