My project has the following package structure:
src/
com.my.app.school.course
-Course.java
...
com.my.app.school.course.free
-CourseFree.java
I use Maven to build the project, in my pom.xml, I defined maven-compiler-plugin to test excluding a package with all its java classes.
I first tried following way to exclude package com.my.app.school.course.free
:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<excludes>
<exclude>**/com/my/app/school/course/free/*</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
It works! I mean after run mvn clean install
, the final build under target/classes/ doesn't have the package com/my/app/school/course/free
.
Then, I tried to exclude package com.my.app.school.course
. I simply replace the above <exclude>
tag with value <exclude>**/com/my/app/school/course/*</exclude>
. I thought it should work too , but it doesnt! Under target/classes/ I see all packages, no package is excluded. Why?
What I want to achieve is to exclude package com.my.app.school.course
but keep pacakge com.my.app.school.course.free
, how to achieve this?
======== update ========
I feel it might be because the package I tried to exclude contain java classes that have been used in other packages. I will verify my guess.
Ok, I found the reason why the exclusion doesn't work.
Because some java classes under the package which I tried to exclude have been used in other packages. Seems maven-compiler-plugin is smart to detect that.
As you note, the problem is that your other sources depend on some of the sources you've excluded: Because Maven passes -sourcepath
to javac, javac can find and compile those "missing" sources.
If you want the build to fail in that case, you can explicitly specify a dummy value for -sourcepath
:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<compilerArgs>
<arg>-sourcepath</arg>
<arg>doesnotexist</arg>
</compilerArgs>
</configuration>
</plugin>
See MCOMPILER-174 and this longer explanation of how javac handles -sourcepath
and other arguments .
configuring the exclusion in the jar or war plugins seems to be a good technique to not disturb JUnit testing and compilation:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<excludes>
<exclude>**/service/*</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.1</version>
<configuration>
<packagingExcludes>
WEB-INF/lib/commons-logging-*.jar,
%regex[WEB-INF/lib/log4j-(?!over-slf4j).*.jar]
</packagingExcludes>
</configuration>
</plugin>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.