How to exclude a resource file in a Maven assembly?

自闭症网瘾萝莉.ら 提交于 2019-12-12 14:14:06

问题


I'm trying to exclude a resource file from my Maven build. I've copied the my-jar-with-dependencies.xml and added a section. This correctly excludes the named file from being copied to the jar's /src/main/resources but the file still gets copied to the jar's top level directory along with the other resources.

<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">
  <!-- TODO: a jarjar format would be better -->
  <id>my-jar-with-dependencies</id>
  <formats>
    <format>jar</format>
  </formats>

  <fileSets>
    <fileSet>
      <directory>${project.basedir}</directory>
      <outputDirectory></outputDirectory>
      <useDefaultExcludes>true</useDefaultExcludes>
      <excludes>
        <exclude>**/lanchecker.properties</exclude>  <!-- this removes it from jar/src/main/resources but *NOT* from jar/ -->
      </excludes>
    </fileSet>
  </fileSets>

  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <dependencySet>
      <outputDirectory>/</outputDirectory>
      <useProjectArtifact>true</useProjectArtifact>
      <unpack>true</unpack>
      <scope>runtime</scope>
    </dependencySet>
  </dependencySets>
</assembly>

Can I prevent the maven-assembly-plugin from doing that copy but still leave that file accessible in development?

Update:

Dependencies, as requested:

  <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.2.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>xml-apis</groupId>
        <artifactId>xml-apis</artifactId>
        <version>1.4.01</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.8.7</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-server</artifactId>
        <version>2.47.1</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.1.3</version>
    </dependency>

    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>javax.mail-api</artifactId>
        <version>1.5.4</version>
    </dependency>

    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.4.7</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>4.2.1.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.freemarker</groupId>
        <artifactId>freemarker</artifactId>
        <version>2.3.23</version>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.4</version>
    </dependency>       

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.6.3</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.6.3</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.6.3</version>
    </dependency>

  </dependencies>

回答1:


Try this:

...
<dependencySets>
    <dependencySet>
      <outputDirectory>/</outputDirectory>
      <useProjectArtifact>true</useProjectArtifact>
      <unpack>true</unpack>
      <scope>runtime</scope>
      <unpackOptions>
        <excludes>
          <exclude>**/lanchecker.properties</exclude>
        </excludes> 
      </unpackOptions>
    </dependencySet>
  </dependencySets>
...

The reason this works is that the different assembly selectors (<dependencySets>, <moduleSets>, <fileSets> and <files> target different things and they can overlap.

For instance, <fileSets> only targets files in this module (the one the assembly is tied to). Since you specified <useProjectArtifact>true</useProjectArtifact>, the dependency set will include this module as a dependency, so that's why lanchecker.properties found its way into your assembly, and that's why you have to exclude it from the dependency set as well.

Without knowing what your POM looks like or what you are trying to achieve, it might be sufficient to specify <useProjectArtifact>false</useProjectArtifact> and just use <fileSets> to control the contents of this module.

On the other hand, since this module is already included in the dependency set (by way of <useProjectArtifact>true</useProjectArtifact>, you might get away with just removing the <fileSets> section altogether.

Assemblies are usually the last thing a Maven user learns to master. It's powerful, flexible and can be complex, especially if you throw in multi-modules in the mix, but it can also be a bit quirky.



来源:https://stackoverflow.com/questions/33136415/how-to-exclude-a-resource-file-in-a-maven-assembly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!