使用“template-maven-plugin”时如何处理构建路径源

问题描述 投票:0回答:1

我正在从 Ivy+Ant 迁移到 Maven,我对 Maven 还很陌生,所以请耐心等待...

我需要“过滤”包含 @@replaceme@@ 占位符的单个源文件 (.java),然后编译结果并将整个文件打包为 JAR。我已经设法使用 org.codehaus.mojo:templatating-maven-plugin 对源文件进行过滤。该插件将生成源创建到 ${project.build.directory}/ generated-sources/java-templates 中,并按应有的方式替换 java 文件内容,然后编译正确完成,一切正常应该如此。

但是,在 IDE (Eclipse) 中,构建路径源现在指向 ${project.build.directory}/ generated-sources/java-templates = target/ generated-sources/java-templates。我不希望它成为我的主要构建路径源,因为现在它包含替换的数据,并且需要 this 直接指向原始源“agent/src”。 如果我将原始源“agent/src”添加到 POM 中(并且在 IDE 中可见),那么编译将由于重复的类错误而失败。

POM相关部分:

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>templating-maven-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <id>filter-src</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>filter-sources</goal>
                        </goals>
                        <configuration>
                            <delimiters>@@</delimiters>
                            <overwrite>true</overwrite>
                            <sourceDirectory>agent/src</sourceDirectory>
                            <outputDirectory>${project.build.directory}/generated-sources/java-templates</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>3.5.0</version>
                <executions>
                    <execution>
                        <id>add-sources</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>loader/src</source>
                                <!--<source>agent/src</source> duplicate class -errors with this-->
                            </sources>
                        </configuration>
                    </execution>
                    <execution>
                        <id>add-test-sources</id>
                        <phase>generate-test-sources</phase>
                        <goals>
                            <goal>add-test-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>test/src</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

这就是 Eclipse 向我显示源代码的方式,因为它“丢失”了原始源代码“agent/src”:

我做错了什么,还是这个插件本来就是这样的?我内心深处感觉有一个非常简单的解决方案可以解决我的问题......

java maven pom.xml maven-plugin
1个回答
0
投票
  1. 删除 build-helper-maven-plugin
  2. 从 tempating-maven-plugin 的配置中删除 sourceDirectory 和 outputDirectory 的配置
  3. 从 tempating-maven-plugin 中删除了阶段的定义,以及 id 的定义

将你的模板类放入

src/main/java-templates/<package>

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>templating-maven-plugin</artifactId>
    <executions>
      <execution>
        <goals>
          <goal>filter-sources</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

这就是所需要的...哦,只需在插件管理中定义版本或在上面的设置中明确定义版本即可。

© www.soinside.com 2019 - 2024. All rights reserved.