我花了好几个小时研究这个问题,搜索了几个Google和SO条目,我有一些想法,但没有得到结果。
我有一个像这样的maven文件:
抓住一个包含JSON模式的jar,然后解压缩它们。
使用Maven Replacer插件(v 1.5.3),替换名为“MySchema.json”的模式文件中的一行:
“你好”: “HelloWorld”:
然后Maven将使用另一个插件来编译一个名为“converter.java”的类,并运行此类以输出基于“MySchema.json”的Java文件。让我们调用生成的Java文件“MyPojo.java”。
现在,我希望Maven替换“MyPojo.java”中的一行,但无论我做什么,我都无法做到这一点。
我试过了:
在我当前的项目(非父项目)中,这是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>
答案 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