Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude a resource file in a Maven assembly?

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>
like image 526
Ian Avatar asked Mar 14 '23 11:03

Ian


1 Answers

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.

like image 108
Daniel Avatar answered Mar 23 '23 09:03

Daniel