简体   繁体   中英

How to exclude classes from a packaged webapp with maven

How can I filter certain classes in /target/classes from going into /target/[webapp]/WEB-INF/classes? I want them compiled into /target/classes/ but not in the final war.

What are these classes for? If they are for testing, you can specify them in src/test/java, they will then be compiled into target/test-classes in the test-compile phase, but won't be included in the final war.

If they aren't for testing and aren't to be included in the war, perhaps they should be refactored into another project so you can specify it as a dependency (perhaps with "provided" scope so they won't be deployed.

For reference you can configure the war to include and exclude resources when packaging.

The following example will include all jpgs but exclude resources from the image2 sub folder:

    <configuration>
      <webResources>
        <resource>
          <!-- this is relative to the pom.xml directory -->
          <directory>resource2</directory>
          <!-- the list has a default value of ** -->
          <includes>
            <include>**/*.jpg</include>
          <includes>
          <excludes>
            <exclude>**/image2</exclude>
          </excludes>
        </resource>
      </webResources>
    </configuration>

See the war plugin documentation for more details.

You might have luck with this, assuming you them in a package that you can define with an ant pattern

  <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.0.2</version>
        <configuration>
                <excludes>**/dontneed/*.class</excludes>
        </configuration>
  </plugin> 

With current version of maven-war-plugin (3.0.0) this works for me -

<profile>
     <id>abc</id>
     ...
     <build>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
          <packagingExcludes>WEB-INF/classes/com/abc/pqr/ClassName.class</packagingExcludes>
        </configuration>
       </plugin>
     </build>
</profile> 

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM