8

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.

4

3 回答 3

14

好的,我找到了排除不起作用的原因。

因为我试图排除的包下的一些java类已经在其他包中使用过。似乎 maven-compiler-plugin 很聪明地检测到这一点。

于 2014-08-15T08:44:01.170 回答
0

正如您所注意到的,问题在于您的其他来源取决于您排除的一些来源:因为 Maven 传递-sourcepath给 javac,所以 javac 可以找到并编译那些“缺失”的来源。

如果您希望构建在这种情况下失败,您可以显式指定一个虚拟值-sourcepath

<plugin>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>3.8.1</version>
  <configuration>
    <compilerArgs>
      <arg>-sourcepath</arg>
      <arg>doesnotexist</arg>
    </compilerArgs>
  </configuration>
</plugin>

请参阅MCOMPILER-174以及有关 javac 如何处理和其他参数的更长解释-sourcepath

于 2020-12-08T20:30:24.713 回答
0

在jarwar 插件中配置排除似乎是一种不干扰 JUnit 测试和编译的好技术:

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

<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>
于 2021-04-27T22:02:17.613 回答