Maven: excluding java files in compilation
Asked Answered
E

5

64

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>
Emyle answered 29/7, 2013 at 9:53 Comment(5)
Location for properties is src/main/resources for test properties src/test/resources.Junk
How does your project in qa/bamtests even find the sources in qa/apitests?Eusebiaeusebio
what about using the same pattern for excludes: <exclude>**/test/omi/**</exclude>Kacikacie
a bit off topic: your way to organize resources is just a mess. You should put it in src/main/resources (or create other directories under src/main/ ) to organize your different kind of resourcesCheka
Read Stefan's answer about the need to remove src/main/java/ from the path!Anserine
T
79

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>
Transit answered 29/7, 2013 at 9:53 Comment(8)
What does the two leading * stand for ?Skit
The ** mean 'any directories'.Eusebiaeusebio
This answer didn't work for me. Look at this https://mcmap.net/q/225997/-maven-excluding-java-files-in-compilationIntercede
For me it did not work either, I had to remove src/main/java part ... Looks like a relative path issue. Can someone please explain in detail?Tommyetommyrot
@SoumyaKanti, not 100% sure, but the maven compiler plugin handles java sources. It normally expects them to reside in src/main/java (according to the archetype structure), and thus is limited to operate within that directory sub-tree. The behavior might have been different for older versions of the plugin though.Boman
To exclude tests, see @michal-kordas 's answer on https://mcmap.net/q/225997/-maven-excluding-java-files-in-compilation it worked for meIllinois
what is the difference between this exclude and the exclude that can be used in the build tag?Maudemaudie
It worth saying, compiler will ignore these excludes if excluded java file referenced (for example, in imports) from another (non-excluded) fileKarrah
K
49

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>
Kalvn answered 31/10, 2013 at 17:26 Comment(1)
remove src/main/ is so true!Artel
H
39

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>
Hyperthyroidism answered 11/9, 2015 at 20:3 Comment(1)
Documented here: maven.apache.org/plugins/maven-compiler-plugin/…Quent
S
3

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>
Sashasashay answered 9/5, 2016 at 0:6 Comment(0)
E
2

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.

Emirate answered 29/7, 2013 at 10:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.