6

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

提前致谢

<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>
        </执行>
</插件>

4

2 回答 2

5

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

将这样的内容添加到您的 MyCodeGeneratorMojo

    URL getTemplate(String fileName) {
        return this.getClass().getResource(fileName);
    }

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

于 2010-02-20T20:31:46.043 回答
3

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

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

于 2010-02-20T20:19:11.670 回答