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> 55 Answers
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> 7Adding 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> 1For 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> 1If 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.
0The 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>