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

2019-08-12 07:43发布

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>

2条回答
我命由我不由天
2楼-- · 2019-08-12 07:51

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

查看更多
聊天终结者
3楼-- · 2019-08-12 08:16

As specified by the maven-assembly-plugin documentation:

If your project wants to package your artifact in an uber-jar, the assembly plugin provides only basic support. For more control, use the Maven Shade Plugin.


Using the maven-shade-plugin you can have a fat jar (like using the assembly plugin) and solve similar issues of excluding files via configuration (no need of external assembly descriptor file).

In your case, to exclude resources from assembled jars, you can use shade filters.

A simple configuration would look like:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.4.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <filters>
                            <filter>
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>**/*file_pattern*</exclude>
                                </excludes>
                            </filter>
                        </filters>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

In the example above, you can customize file_pattern or narrow down your filter in the artifact element using your my.groupId:my.artifactId.


Note: the approach above is recommended when excluding files from external libraries, however you can still use the maven-assembly-plugin for excluding files from your own project via a custom assembly descriptor file.

查看更多
登录 后发表回答