2016-11-04 465 views
1

我在这个问题上花了几个小时,搜索了几个Google和SO条目,我有一些想法但没有得到结果。使用Maven替换插件来替换生成的Java文件中的短语Target/generated-sources

我有一个Maven的文件,做这样的事情:

  1. 抢包含JSON模式一个罐子,并解压缩。

  2. 使用Maven的代用品插件(V 1.5.3),取代所谓的“MySchema.json”这样一个模式文件中的一行:

    “你好”: 的“HelloWorld”:

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

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

我尝试:

  • 包括用于步骤4,其将模式到Java插件后的一个单独更换插件条目,但ofcourse这引起Maven来抱怨现有用同一工件/组更换插件从步骤2的编号。
  • 试图添加一个单独的执行id到第二个插件的目标“替换”,这是插件无效。
  • 有一个父项目到我目前的项目文件夹,我试着在父POM中放置另一个替代插件,并使阶段成为任何“包”,“生成资源”,“编译”等不行。注意:“MySchema.json”(在我当前的项目POM中)内的替换阶段是generate-sources。
  • 给绝对路径的Java,它一直抱怨路径不存在。但是,我在Windows资源管理器地址栏中复制并粘贴了Java内部路径,并可以从Windows资源管理器读取它。请注意,生成的Java文件“MyPojo.java”在父项POM中使用Maven Helper插件在本项目上方由父POM提供的“target/generated-sources”下,因此该文件夹应作为源进一步汇编。 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

你应该能够声明插件只有一次和运行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

  • +1

    这个答案非常完美!非常感谢 :) –