从jar-with-dependencies构建的文件中排除文件 [英] Exclude file from jar as built by jar-with-dependencies

查看:398
本文介绍了从jar-with-dependencies构建的文件中排除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的pom.xml包含以下内容以创建具有所有依赖项的我的项目的jar.
现在,我在src/main/resources中有一个属性文件,该文件是运行应用程序所必需的(并且我想从IDE开始使用它),但是由于保留了设置,因此我不想将其发送到创建的jar文件中

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>

推荐答案

maven-assembly-plugin 文档:

如果您的项目想要将工件打包在uber-jar中,则Assembly插件仅提供基本支持.要进行更多控制,请使用 Maven Shade插件.


使用 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).

在您的情况下,要从组装的jar中排除资源,可以使用阴影滤镜.

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

简单的配置如下所示:

<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>

在上面的示例中,您可以使用my.groupId:my.artifactId自定义file_pattern或在artifact元素中缩小过滤器的范围.

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

注意:从外部库中排除文件时,建议采用上述方法,但是您仍然可以使用maven-assembly-plugin通过自定义

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.

这篇关于从jar-with-dependencies构建的文件中排除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆