Maven:我的mojo如何访问自己的资源?

ano*_*ous 5 maven-2 mojo maven-plugin

我有一个项目(这里称为my-artifact)需要从模型文件生成源.我已经创建了一个maven-plugin(my-code-generator),如下面的pom.xml摘录中所述.它从my-artifact的资源加载和处理model.xml,并使用插件中存储的一些预定义模板生成代码.问题是我的代码生成器如何访问这些模板,因为它们不在项目资源中,而是在自己的资源中.

提前致谢

<plugin>
  <groupId> my-group </ groupId>
        <artifactId> my-code-generator </ artifactId>
        <version> 0.0.1-SNAPSHOT </ version>
        <configuration>
                <modelfile>
                        src/main/resources/model .xml
                </ modelDir>
        </ configuration>
        <executions>
                <execution>
                        <phase> generate-sources </ phase>
                        <goals>
                                <goal> generate-model </ goal>
                        </ goals>
                </ execution>
        </ executions >
</ plugin>
<plugin>
        <groupId> org.codehaus.mojo </ groupId>
        <artifactId> build-helper-maven-plugin </ artifactId>
        <executions>
                <execution>
                        <id> add-source </ id>
                        <phase> generate-sources </ phase>
                        <goals>
                                <goal> add-source </ goal>
                                <sources>
                                        <source> target/generated-sources </ source>
                                </ sources>
                        </ configuration>
                </ execution>
        </ executions>
</ plugin>

Ver*_*gen 5

只需使用ClassLoader,即可从MyCodeGenerator Maven插件中获取资源.

将这样的内容添加到MyCodeGeneratorMojo中

    URL getTemplate(String fileName) {
        return this.getClass().getResource(fileName);
    }
Run Code Online (Sandbox Code Playgroud)

在MyCodeGenerator Maven插件中,将模板添加到src/main/resources目录中(不要忘记在该目录中使用正确的包条目(目录)).


bma*_*ies 3

通过将它们包含在插件的 jar 文件中,并通过 ClassLoader.getResourceAsStream 通过类路径引用它们。

通过将它们打包为另一个工件,将它们声明为依赖项,并调用依赖项解析 API,这需要做更多的工作。