Maven: excluding java files in compilation

Maven

Maven Problem Overview


I have a folder of java sources which I wish to exclude from the compilation.

My folder is under qa/apitests/src/main/java/api/test/omi.

I added the following entry in the pom.xml under qa/bamtests but it didn't help. Is there an entry in addition I need to make?

   <build>
     <resources>
       <resource>
         <directory>src/main/java</directory>
         <includes>
           <include>**/*.properties</include>
           <include>**/*.xml</include>
           <include>**/*.xsd</include>
           <include>**/*.csv</include>
         </includes>
         <excludes>
<exclude>src/main/java/api/test/omi</exclude>
         </excludes>
       </resource>
</build>

Maven Solutions


Solution 1 - Maven

Use the Maven Compiler Plugin.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <configuration>
    <excludes>
      <exclude>**/api/test/omi/*.java</exclude>
    </excludes>
  </configuration>
</plugin>

Solution 2 - Maven

Adding an exclude as the other answers suggested worked for me, except the path shouldn't include "src/main/java":

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <configuration>
        <excludes>
          <exclude>com/filosync/store/StoreMain.java</exclude>
        </excludes>
      </configuration>
    </plugin>
  </plugins>
</build>

Solution 3 - Maven

For anyone needing to exclude test sources <exclude> tag will not work. You need to use <testExclude> instead:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.3</version>
    <configuration>
      <testExcludes>
        <testExclude>**/PrefixToExclude*</testExclude>
      </testExcludes>
    </configuration>
  </plugin>

Solution 4 - Maven

If you want to exclude the java sources from compiling, then mention them in the Maven Compiler Plugin definition

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.0.2</version>
        <configuration>
            <excludes>
              <exclude>src/main/java/api/test/omi/*.java</exclude>
            </excludes>
        </configuration>
    </plugin>
</plugins>

The resources plugin only defines what all resources to bundle in your final artifact.

Solution 5 - Maven

The top voted answer works fine but it doesn't allow forcing the exclusion when the excluded class/classes is/are being used by not-excluded ones.

Workaround using maven-antrun-plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.8</version>
    <executions>
        <execution>
            <phase>process-classes</phase>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <tasks>
            <delete dir="target/classes/folder/to/exclude"/>
        </tasks>
    </configuration>
</plugin>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionconstantlearnerView Question on Stackoverflow
Solution 1 - Mavenuser1907906View Answer on Stackoverflow
Solution 2 - MavenStefanView Answer on Stackoverflow
Solution 3 - MavenMichal KordasView Answer on Stackoverflow
Solution 4 - MavenArpitView Answer on Stackoverflow
Solution 5 - MavensatoshiView Answer on Stackoverflow