tags:

views:

22

answers:

3

Hi,

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?

Thanks, regarsd, Marko

A: 

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.

xor_eq
I allready thought of something like this myself, but it would break the eclipse project nature...
In that case I'm wondering if you're on the right track. Why don't you move the different source files in distinct maven projects, so that you can include them as different dependencies based on a user profile?
xor_eq
A: 

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

Script Runner
Sorry, but reading the doc (your link) I cannot see how filtering could help me here...
A: 

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.

Pascal Thivent
Thanks, that works but has a major pitfall: If there is a compile error the wrong source file (and the *.java.moved file) stays in the src/main/java dir.
@user385480 Arghhh, indeed, didn't think about that. I'll update my answer.
Pascal Thivent