Maven覆盖依赖项中的资源文件 [英] Maven overwrite resource file in dependency

查看:59
本文介绍了Maven覆盖依赖项中的资源文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个Maven模块,AB. AB的依赖项.这两个模块在src/main/resources中都有一个名为default.properties的资源文件.在两个项目中,我需要保持文件名相同且文件的位置相同,因为AB均使用代码期望文件被命名并位于文件所在的位置.构建B时,A的默认属性位于最终的jar中.我希望在构建B时具有B的属性.我该怎么办?

I have two Maven modules, A and B. A is a dependency of B. Both modules have a resource file named default.properties located in src/main/resources. I need to keep the filenames the same and the location of the file the same in both projects because both A and B are using code which expects the file to be named and located where it is. When building B, A's default properties is in the final jar. I wish to have B's properties when I build B. How can I do this?

推荐答案

好吧,Maven资源插件和Assembly插件没有将其剪切掉,所以我进一步挖掘了一些内容.

Ok, Maven Resources Plugin and Assembly plugin did not cut it, so I dug some more.

这似乎可以通过 Maven Shade插件来实现.

It seems this is doable with Maven Shade plugin.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>1.4</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <!-- Main class -->
                                <mainClass> <!-- fully qualified package and class name --> </mainClass>
                                <manifestEntries>
                                    <Class-Path>.</Class-Path>
                                </manifestEntries>
                            </transformer>
                        </transformers>

                        <filters>
                            <filter>
                                <artifact>org.something:SomeDependency</artifact>
                                <excludes>
                                    <exclude>*.properties</exclude>
                                </excludes>
                            </filter>
                        </filters>

                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

因此,在<configuration> ... </configuration> -tags中,我定义了两件事:一个转换器实现,负责将jar清单修改为可运行的并使用当前目录作为类路径根,并排除所有以结尾的文件从org.something:SomeDependency依赖内部使用.properties.

So, inside the <configuration> ... </configuration> -tags I've defined two things: a transformer-implementation that takes care of modifying the jar-manifest to be runnable and use the current directory as classpath root, and excluding all the files ending with .properties from inside of dependency org.something:SomeDependency.

实际的过滤部分是您可以在阴暗建立的最终jar中排除您不想结束的文件.您可以在定义的<filter>中使用<artifact>*:*</artifact>从所有依赖项和当前项目中排除文件,或者可以使用<artifact>dependcyGroupId:dependencyArtifact</artifact>仅选择某些依赖项,例如<artifact>junit:junit</artifact>,甚至使用通配符为一个或其他(<artifact>*:junit</artifact>).然后,在<excludes>...</excludes> -tags内定义排除的文件.同样,您可以使用确切的文件名或通配符.尽管我建议您从插件站点阅读文档,但这应该可以帮助您解决当前的问题,因为shade可以做的更多.

The actual filtering part is where you can exclude the files you don't want to end up in the final jar built by shade. You can exclude the files from all the dependencies and the current project using <artifact>*:*</artifact> inside the defined <filter>, or you can select only certain dependency using <artifact>dependcyGroupId:dependencyArtifact</artifact>, for example <artifact>junit:junit</artifact>, or even using wildcards for one or the other (<artifact>*:junit</artifact>). The excluded files are then defined inside the <excludes>...</excludes> -tags. Again, you can use exact filenames or wildcards. This should get you going with your current problem, although I'd suggest reading the documentation from the plugin-site, because shade can do a lot more than this.

这篇关于Maven覆盖依赖项中的资源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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