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

发布于 2024-08-22 03:30:17 字数 3543 浏览 8 评论 0原文

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

预先感谢

<插件>
  我的组
       我的代码生成器
      <版本>0.0.1-SNAPSHOT
       <配置>
             <模型文件>
                  src/主/资源/model.xml
             
       
        <执行>
             <执行>
                  <阶段>生成源
                  <目标>
                    sp;       <目标>生成模型
                  < /目标>
              
       

<插件>
        org.codehaus.mojo
       build-helper-maven-plugin
        <执行>
             <执行>
                  < id>添加源
                  <阶段>生成源
                  <目标>
                    sp;       <目标>添加源
                    sp;       <来源>
                    sp;               <源>目标/生成源
                    sp;       
                  < /配置>
              
       

I have a project (here called my-artifact) which needs to generate sources from a model file. I've created a maven-plugin (my-code-generator) which is used as described in the pom.xml excerpt below. It loads and processes the model.xml from my-artifact's resources and generates code using some predefined templates stored within the plugin. The question is how my-code-generator could access these templates as they are not in the project resources but within its own resources.

Thanks in advance

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雪化雨蝶 2024-08-29 03:30:17

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

将类似的内容添加到您的 MyCodeGeneratorMojo

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

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

Just use the ClassLoader, to get resources from the MyCodeGenerator Maven plugin.

Add something like this to your MyCodeGeneratorMojo

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

Within the MyCodeGenerator Maven plugin, add the template(s) to the src/main/resources directory (don't forget to use the correct package entry (directories) within that directory).

横笛休吹塞上声 2024-08-29 03:30:17

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

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

By including them in the jar file for the plugin and referencing them via classpath, via ClassLoader.getResourceAsStream.

By packaging them as another artifact, declaring them as a dependency, and calling the dependency-resolution API, which is a lot more work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文