使用Maven Replace Plugin替换Target / generated-sources

时间:2016-11-04 16:32:02

标签: java json eclipse maven replace

我花了好几个小时研究这个问题,搜索了几个Google和SO条目,我有一些想法,但没有得到结果。

我有一个像这样的maven文件:

  1. 抓住一个包含JSON模式的jar,然后解压缩它们。

  2. 使用Maven Replacer插件(v 1.5.3),替换名为“MySchema.json”的模式文件中的一行:

    “你好”: “HelloWorld”:

  3. 然后Maven将使用另一个插件来编译一个名为“converter.java”的类,并运行此类以输出基于“MySchema.json”的Java文件。让我们调用生成的Java文件“MyPojo.java”。

  4. 现在,我希望Maven替换“MyPojo.java”中的一行,但无论我做什么,我都无法做到这一点。

  5. 我试过了:

    • 在将模式转换为Java的插件之后为步骤4包含一个单独的替换插件条目,但是当然这导致Maven在步骤2中抱怨具有相同工件/组ID的现有替换插件。
    • 尝试在第二个插件的目标“替换”中添加单独的执行ID,这对此插件无效。
    • 我的当前项目文件夹有一个父项目,我尝试在父POM中添加另一个替换插件,并使阶段成为“包”,“生成资源”,“编译”等任何一个。不行。注意:“MySchema.json”(在我当前的项目POM中)中的替换阶段是generate-sources。
    • 给出Java的绝对路径,它一直抱怨路径不存在。但是我在生成Windows资源管理器地址栏后复制并粘贴了Java的路径,并且可以从Windows资源管理器中读取它。请注意,生成的Java文件“MyPojo.java”位于“target / generated-sources”下,该文件由父POM上的父POM使用父POM中的Maven Helper插件提供,因此该文件夹应作为源文件显示为进一步编译。 Maven Helper插件具有阶段生成源。
    • 使用与上述相同的结果

    在我当前的项目(非父项目)中,这是POM代码:

        <build>
            <!—execute a plugin grab schemas jar and unpack schemas-->
    
     ...
    
                <plugin>
                    <groupId>com.google.code.maven-replacer-plugin</groupId>
                    <artifactId>replacer</artifactId>
                    <version>1.5.3</version>
                    <executions>
                        <execution>
                            <phase>generate-sources</phase>
                            <goals>
                                <goal>replace</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <includes>
                            <include>${project.basedir}/target/schemas/MySchema.json</include>
                        </includes>
                        <replacements>
                            <replacement>
                                <token>"Hello":</token>
                                <value>"Hello World":</value>
                            </replacement>
                        </replacements>
                    </configuration>
                </plugin>
    
            <!-- execute a Plugin for converting shcemas to POJO -->
    
            . . .
    
            </plugins>
        </build>
    </project>
    

1 个答案:

答案 0 :(得分:2)

您应该只能声明一次插件,并在不同的Maven Build Lifecycle phases处运行两次替换execution

  • Json -> POJO转化之前
  • Json -> POJO转化后

因此,将其翻译成可能会导致类似:

<plugin>
    <!-- (unique) plugin declaration -->
    <groupId>com.google.code.maven-replacer-plugin</groupId>
    <artifactId>maven-replacer-plugin</artifactId>
    <version>1.3.5</version>
    <executions>
        <!-- first execution: replace on json file -->
        <execution>
            <id>replace-for-json</id>
            <phase>some-phase-before-conversion</phase>
            <goals>
                <goal>replace</goal>
            </goals>
            <configuration>
                <filesToInclude>${project.basedir}/target/schemas/MySchema.json</filesToInclude>
                <preserveDir>true</preserveDir>
                <outputDir>target</outputDir>
                <replacements>
                    <replacement>
                        <token>"Hello":</token>
                        <value>"Hello World (Json)":</value>
                    </replacement>
                </replacements>
            </configuration>
        </execution>
        <!-- second execution: replace on java file -->
        <execution>
            <id>replace-for-pojo</id>
            <phase>some-phase-after-conversion</phase>
            <goals>
                <goal>replace</goal>
            </goals>
            <configuration>
                <filesToInclude>${project.basedir}/target/generated-sources/MyPojo.java</filesToInclude>
                <preserveDir>true</preserveDir>
                <outputDir>target</outputDir>
                <replacements>
                    <replacement>
                        <token>"Hello":</token>
                        <value>"Hello World (Java)":</value>
                    </replacement>
                </replacements>
            </configuration>
        </execution>
    </executions>
</plugin>

来源:Configuration for the maven-replacer-plugin on two separate executions