使用程序集排除 *.war 文件中的 属性 个文件

Excluding property files in *.war file using assemblies

我有一个 WAR 文件,它作为依赖项包含在我的汇编模块中。 WAR 文件包含一些属性,我想在新生成的 WAR 文件中排除这些属性。

由于未知原因,排除无效。即使我使用 **/*.properties 排除所有 属性 文件,也不会排除这些文件。知道这里出了什么问题吗?

Maven Pom

<?xml version="1.0" encoding="UTF-8"?>
<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>de.mycompanyname.myprojectname</groupId>
<version>1.0-SNAPSHOT</version>

<artifactId>demo-dashboard-assembly</artifactId>
<packaging>pom</packaging>
<name>demo-dashboard-assembly</name>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.5.3</version>
            <goals></goals>
            <executions>
                <execution>
                    <id>delivery-prod</id>
                    <phase>prepare-package</phase>
                    <goals><goal>single</goal></goals>
                    <configuration>
                        <appendAssemblyId>false</appendAssemblyId>
                        <finalName>demo#prod#dashboard</finalName>
                        <descriptors>
                            <descriptor>src/main/assembly/delivery.prod.xml</descriptor>
                        </descriptors>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>de.mycompanyname</groupId>
        <artifactId>myprojectname.dashboard</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <type>war</type>
    </dependency>
</dependencies>

</project>

delivery.prod.xml

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>delivery</id>
<formats>
    <format>dir</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>

<dependencySets>
    <dependencySet>
        <outputDirectory>/</outputDirectory>
        <unpack>true</unpack>
        <excludes>
            <exclude>WEB-INF/classes/commonDashboard.dev.properties</exclude>
            <exclude>WEB-INF/classes/commonDashboard.test.properties</exclude>
            <exclude>WEB-INF/classes/db.dev.properties</exclude>
            <exclude>WEB-INF/classes/db.test.properties</exclude>
        </excludes>
    </dependencySet>
</dependencySets>
</assebly>

问题是 .

通过在 <dependencySet> 级别指定 <exclude>

<dependencySet>
  <outputDirectory>/</outputDirectory>
  <unpack>true</unpack>
  <excludes>
    <exclude>WEB-INF/classes/commonDashboard.dev.properties</exclude>
    <!-- more excludes -->
  </excludes>
</dependencySet>

您正在指定应排除在此集中的依赖项。也就是说这里是指定不要解压到根目录下的依赖。显然,由于没有 WEB-INF/classes/... 依赖项,因此它不匹配并且不执行任何操作。事实上,它会发出警告,您可以在日志中看到:

[WARNING] The following patterns were never triggered in this artifact exclusion filter:
o  'WEB-INF/classes/commonDashboard.dev.properties'
o  'WEB-INF/classes/commonDashboard.test.properties'
o  'WEB-INF/classes/db.dev.properties'
o  'WEB-INF/classes/db.test.properties'

您想要的是排除解压缩的依赖项中的文件。为此,您需要使用 <unpackOptions> 元素,其中包含一个排除配置:

Specifies options for including/excluding/filtering items extracted from an archive. (Since 2.2-beta-1)

因此,您应该改为:

<dependencySets>
  <dependencySet>
    <outputDirectory>/</outputDirectory>
    <unpack>true</unpack>
    <unpackOptions>
      <excludes>
        <exclude>WEB-INF/classes/commonDashboard.dev.properties</exclude>
        <exclude>WEB-INF/classes/commonDashboard.test.properties</exclude>
        <exclude>WEB-INF/classes/db.dev.properties</exclude>
        <exclude>WEB-INF/classes/db.test.properties</exclude>
      </excludes>
    </unpackOptions>
  </dependencySet>
</dependencySets>

这将在每个依赖项的解包过程中正确排除所选文件,并且生成的程序集将不包含它们。请注意,您可以 use a regular expression 而不是指定每个文件;例如:

<exclude>%regex[WEB-INF\/classes\/(commonDashboard|db)\.(dev|test)\.properties]</exclude>