[英]Excluding package with maven-compiler-plugin works for one package but doesn't work for another
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.我使用 Maven 来构建项目,在我的 pom.xml 中,我定义了maven-compiler-plugin来测试排除一个包及其所有 java 类。
I first tried following way to exclude package com.my.app.school.course.free
:我首先尝试以下方法来排除包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
.我的意思是在运行mvn clean install
, target/classes/ 下的最终构建没有包com/my/app/school/course/free
。
Then, I tried to exclude package com.my.app.school.course
.然后,我尝试排除包com.my.app.school.course
。 I simply replace the above <exclude>
tag with value <exclude>**/com/my/app/school/course/*</exclude>
.我只是将上面的<exclude>
标记替换为值<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.在 target/classes/ 我看到所有包,没有包被排除在外。 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?我想要实现的是排除包com.my.app.school.course
但保留com.my.app.school.course.free
,如何实现?
======== update ======== ========更新========
I feel it might be because the package I tried to exclude contain java classes that have been used in other packages.我觉得可能是因为我试图排除的包包含在其他包中使用过的 java 类。 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.因为我试图排除的包下的一些java类已经在其他包中使用了。 Seems maven-compiler-plugin is smart to detect that.似乎 maven-compiler-plugin 很聪明地检测到这一点。
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.正如您所注意到的,问题在于您的其他来源依赖于您排除的一些来源:因为 Maven 将-sourcepath
传递给 javac,javac 可以找到并编译那些“缺失”的来源。
If you want the build to fail in that case, you can explicitly specify a dummy value for -sourcepath
:如果您希望在这种情况下构建失败,您可以为-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 .请参阅MCOMPILER-174以及有关 javac 如何处理-sourcepath
和其他参数的详细说明。
configuring the exclusion in the jar or war plugins seems to be a good technique to not disturb JUnit testing and compilation:在jar或war 插件中配置排除似乎是一种不干扰 JUnit 测试和编译的好技术:
maven-jar-plugin
/ exclude
maven-jar-plugin
/ exclude
<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>
maven-war-plugin
/ exclude
maven-war-plugin
/ exclude
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.