Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 910061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:58:59+00:00 2026-05-15T16:58:59+00:00

I have to replace a Java source file during maven compile if a special

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T16:59:00+00:00Added an answer on May 15, 2026 at 4:59 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following Java stored procedure: CREATE OR REPLACE AND RESOLVE JAVA SOURCE
I am attempting to do some find and replace on a java source file.
I have to replace something in file with :something . My editor is kate
I have html-file. I have to replace all text between this: [%anytext%]. As I
I have been trying to replace a word in a text file with a
I have many Java source files which I need to change their package line
I would like to update an open-source war file in Maven with some additional
I have some java source files that use a package prefix (they are emulating
I have a problem regarding java.lang.NoSuchMethodError. This program is about Compiler API (JSR 199).
I have to replace a the backslash with the slash with a query in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.