maven-war-plugin:排除所有目录,但只有一个

Moh*_*sal 5 java build-process maven

我有一个目录结构:

截图

src
|__ main
    |__ java
    |__ resources
    |__ webapp
        |__ css_blue
        |__ css_green
        |__ css_red
        |__ WEB-INF
Run Code Online (Sandbox Code Playgroud)

那里有CSS三个单独的目录(如css_red,css_green,css_blue).在这里,我想根据-D交换机只包含其中一个:

mvn clean install -Dcss=green
Run Code Online (Sandbox Code Playgroud)

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.faisal.dwr</groupId>
  <artifactId>chatbox</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <dependencies>
    <dependency>
      <groupId>javax</groupId>
      <artifactId>javaee-api</artifactId>
      <version>6.0</version>
      <scope>provided</scope>
    </dependency>
    <!-- Spring - MVC -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.2.4.RELEASE</version>
    </dependency>
    <!-- Spring Web Security -->
    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-web</artifactId>
      <version>4.0.3.RELEASE</version>
    </dependency>
    <!-- Spring Security Config -->
    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-config</artifactId>
      <version>4.0.3.RELEASE</version>
    </dependency>
    <!-- DWR -->
    <dependency>
      <groupId>org.directwebremoting</groupId>
      <artifactId>dwr</artifactId>
      <version>3.0.0-RELEASE</version>
    </dependency>
  </dependencies>

    <build>
      <finalName>${project.artifactId}</finalName>
      <plugins>
       <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.6</version>
        <configuration>
            <packagingIncludes>css_${css}</packagingIncludes>
        </configuration>
       </plugin>
      </plugins>
    </build>
</project>
Run Code Online (Sandbox Code Playgroud)

但在这种情况下,文件和目录WEB-INF不在最终.war文件中.

Tun*_*aki 2

默认情况下,packagingIncludes的属性maven-war-plugin将包括 下的所有内容src/main/webapp。当您覆盖它来指定时

<packagingIncludes>css_${css}/**</packagingIncludes>
Run Code Online (Sandbox Code Playgroud)

那么插件将只包含该文件夹(及其下的所有内容),而不再包含WEB-INF。一个简单的解决方案是重新包含WEB-INF

<packagingIncludes>WEB-INF/**,css_${css}/**</packagingIncludes>
Run Code Online (Sandbox Code Playgroud)

这样的配置,底下的一切WEB-INF,都css_${css}将被纳入战争之中。


另一种不需要重新添加文件夹的解决方案是使用<packagingExcludes>。这样,src/main/webapp除了我们在此处指定的文件之外,所有文件都将被包含在内。在这种情况下,我们可以使用正则表达式:排除以css 和 开头css_${css}的所有内容。

<packagingExcludes>%regex[css_(?!${css}/).*]</packagingExcludes>
Run Code Online (Sandbox Code Playgroud)