Search code examples
javamavenmaven-assembly-plugin

Exclude file from jar as built by jar-with-dependencies


My pom.xml contains the following to create a jar of my project with all dependencies.
Now I have a properties file in src/main/resources which is necessary to run the application (and I want to use it from starting from the IDE), but I do not want to ship it in the created jar file, as the settings are maintained separately.

Question: How I can I get a file with all dependencies, but exclude those properties files?

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
        <execution>
            <id>make-jar</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>x.Main</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </execution>
    </executions>
</plugin>

Solution

  • If you want to stick with maven-assembly-plugin you can use an assembly descriptor file where you configure your filters, like in this section:

    <fileSets>
    <fileSet>
      <directory>${basedir}</directory>
      <includes>
        <include>*.txt</include>
      </includes>
      <excludes>
        <exclude>*.properties/exclude>
      </excludes>
    </fileSet>
    

    And in your maven configuration, pom.xml you specify the assemblu descriptor file, in the descriptors tag (distribution.xml is a file containing the section from above)

    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          <filters>
            <filter>src/assembly/filter.properties</filter>
          </filters>
          <descriptors>
            <descriptor>src/assembly/distribution.xml</descriptor>
          </descriptors>
        </configuration>
      </plugin>
    

    Also check this link maven-assembly