I have to replace a Java source file during maven compile if a special profile is active.
I thought about just excluding the file from the standard src/main/java/ and including it from another source directory like src/main/java2/.
But since the files have to have the same name and package the exclude always wins and the file from the other directory gets never included…
Any known working way to do that?
I would use the Maven Antrun Plugin to rename the “original” source file and copy the “special” source file from
src/main/java2tosrc/main/javabefore thecompilephase. Aftercompile, restore the original source file. Something like that (put that in the profile):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.movedfile) stays in thesrc/main/javadirectory. 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.