Replace a Source file in maven compile

放肆的年华 提交于 2019-12-04 12:57:26

I would use the Maven Antrun Plugin to rename the "original" source file and copy the "special" source file from src/main/java2 to src/main/java before the compile phase. After compile, restore the original source file. Something like that (put that in the profile):

<plugin>
  <artifactId>maven-antrun-plugin</artifactId>
  <version>1.4</version>
  <executions>
    <execution>
      <id>replace-source-file</id>
      <phase>process-sources</phase>
      <configuration>
        <tasks>
          <move file="src/main/java/com/stackoverflow/App.java" tofile="src/main/java/com/stackoverflow/App.java.moved"/>
          <copy file="src/main/java2/com/stackoverflow/App.java" todir="src/main/java/com/stackoverflow/"/>
        </tasks>
      </configuration>
      <goals>
        <goal>run</goal>
      </goals>
    </execution>
    <execution>
      <id>restore-source-file</id>
      <phase>compile</phase>
      <configuration>
        <tasks>
          <move file="src/main/java/com/stackoverflow/App.java.moved" tofile="src/main/java/com/stackoverflow/App.java"/>
        </tasks>
      </configuration>
      <goals>
        <goal>run</goal>
      </goals>
    </execution>
  </executions>
</plugin>

Update: As mentioned by the OP in a comment, there is a major drawback with this approach. If there is a compile error the wrong source file (and the *.java.moved file) stays in the src/main/java directory. This is a problem.

A cleaner alternative would be to move both versions of the source in dedicated modules and to declare one or the other module as dependency depending on the profile (the normal artifact would be included in a profile active by default). I wouldn't even mess with compiler exclusions. This would work and is clean.

xor_eq

One possible solution would be to copy the source files you actually need into some temporary directory like /target/temp/src before executing the compile phase. Include only that directory as your source directory.

Use Maven filter option for this. Find the usage in the following link Maven Filter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!