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

时间:2016-12-16 13:28:15

标签: java maven war maven-assembly-plugin

我有一个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>

1 个答案:

答案 0 :(得分:1)

问题在于you're not using the right <exclude> configuration

<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>元素:

  

指定包含/排除/过滤从存档中提取的项目的选项。 (自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>