65

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> 
5
  • Location for properties is src/main/resources for test properties src/test/resources. Commented Jul 29, 2013 at 9:59
  • How does your project in qa/bamtests even find the sources in qa/apitests? Commented Jul 29, 2013 at 10:04
  • what about using the same pattern for excludes: <exclude>**/test/omi/**</exclude> Commented Jul 29, 2013 at 10:09
  • 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 resources Commented Aug 12, 2013 at 3:32
  • Read Stefan's answer about the need to remove src/main/java/ from the path! Commented Jan 23, 2015 at 15:31

6 Answers 6

81

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> 
Sign up to request clarification or add additional context in comments.

8 Comments

What does the two leading * stand for ?
The ** mean 'any directories'.
This answer didn't work for me. Look at this stackoverflow.com/a/19713000/358013
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?
To exclude tests, see @michal-kordas 's answer on stackoverflow.com/a/32531306/517134 it worked for me
|
50

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> 

1 Comment

remove src/main/ is so true!
40

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> 

1 Comment

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> 

Comments

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.

Comments

0

If you're interested in compiling just a folder, you can just include it and maven in ignore the rest but you include dependencies

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <release>${maven.compiler.release}</release> <includes> <include>org/clusterj/accountmanager/api/servlet/account/register/createaccount/**</include> </includes> </configuration> </plugin> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.