如何在“META-INF / services”中包含配置文件使用Maven的JAR文件夹 [英] How to include a config file in the "META-INF/services" folder of a JAR using Maven

查看:288
本文介绍了如何在“META-INF / services”中包含配置文件使用Maven的JAR文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Eclipse中开发一个Java项目,它是使用Maven构建的。我正在使用旧项目中的一些回收代码,其中一个类在具有特定名称的JAR的META-INF / services文件夹中查找文件,然后解析该文件的文本。在这个特定的例子中,它查找一个名为Java接口的文件,然后从文件内部获取实现的类名。

I'm working on a Java project in Eclipse, which is built using Maven. I'm using some recycled code from an older project, and one of these classes looks for a file in the "META-INF/services" folder of the JAR with a particular name, then parses the text of this file. In this particular example, it looks for a file with the name of a Java interface, then grabs the class name of the implementation from inside the file.

所以基本上我是什么我想要做的是在JAR的META-INF / services文件夹中包含一个文件名(X)和一行文字(Y)。我猜这应该是使用Maven完成的,可能是通过在POM文件中指定一个属性,但我的研究没有发现任何东西。我知道你不应该硬编码或手动输入任何META文件,所以我不知道该怎么做。

So basically what I'm trying to do is include a file in the "META-INF/services" folder of the JAR with a file name (X), and one line of text (Y). I'm guessing this should be done using Maven, maybe by specifying an attribute in the POM file, but my research hasn't turned up anything. I know you're not supposed to hard-code or manually type out any META files, so I'm not sure what to do here.

提前致谢。

推荐答案

创建一个新的源文件夹,其位置为 src / main / resources ,然后在其中创建 META-INF / services 文件夹并输入您的FQCN文件。这应该自动将它们复制到jar文件中。所以你将拥有:

Create a new source folder with the location src/main/resources, then create your META-INF/services folder in there and drop in your FQCN file. This should copy them into the jar file automatically. So you'll have:

Project
| src
| | main
|   | java
|     | [your source code]
|   | resources
|     | META-INF
|       | services
|         | [your service files]

值得注意的是,这也适用于具有默认源集的Gradle项目。

It's worth noting that this applies to Gradle projects with the default source sets as well.

这篇关于如何在“META-INF / services”中包含配置文件使用Maven的JAR文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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