从自定义Doclet中的类路径加载资源

时间:2018-06-14 07:08:03

标签: java maven javadoc velocity doclet

我编写了自定义Doclet实现,并使用Maven javadoc运行它:

<build>
  <resources>
            <resource>
                <filtering>false</filtering>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
        <testResources>
            <testResource>
                <filtering>false</filtering>
                <directory>src/test/resources</directory>
            </testResource>
        </testResources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <optimize>true</optimize>
                    <debug>true</debug>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>test-jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <configuration>
                    <doclet>custom.doc.CustomDoclet</doclet>
                    <docletArtifact>
                        <groupId>custom.doc</groupId>
                        <artifactId>custom-doc</artifactId>
                        <version>1.0.0-SNAPSHOT</version>
                    </docletArtifact>
                    <useStandardDocletOptions>false</useStandardDocletOptions>
                    <destDir>custom-doc</destDir>
                </configuration>
            </plugin>
        </plugins>
    </build>

在customDoclet类中,我有以下几行:

public class CustomDoclet extends Doclet {

    public static boolean start(RootDoc root) {
        (1) System.out.println(Thread.currentThread().getContextClassLoader().getResource(".")); // returns null

        VelocityEngine velocityEngine = new VelocityEngine();
        velocityEngine.init();

        (2) Template template = velocityEngine.getTemplate("template/html_template.vm"); // no such file exception thrown

        return true;
    }

}

(1)行返回null,(2)在javadoc阶段抛出execption。

如何使用资源文件?

在带有CustomDoclet的jar中,有文件&#39; template / hmtl_template.vm&#39;在根。

0 个答案:

没有答案