使用Maven创建META-INF/services文件 [英] Create META-INF/services file with Maven

查看:328
本文介绍了使用Maven创建META-INF/services文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用Maven在META-INF/services中创建自定义服务文件?这就是使用Ant的方式: https://ant.apache.org/manual/Tasks/jar.html

Is there a way to create a custom services file within META-INF/services with Maven? This is how you would do it with Ant: https://ant.apache.org/manual/Tasks/jar.html

我知道可以在源代码中简单地创建一个resource/META-INF/并将任何我想要的服务文件放在其中.然后,Maven将自动将那些文件拉入我的JAR.这不能解决我的问题.

I understand that it's possible to simply create a resources/META-INF/ in my source code and place whatever services file I want in there. Maven will then automatically pull those files into my JAR. This does not solve my issue.

我的服务文件的内容根据我正在构建的JAR类型而变化,因此我不能简单地在源代码文件夹中创建它.

The contents of my service file changes depending on the type of JAR I'm building, so I can't simply create it in my source code folders.

我的源代码中有多个版本的服务文件,使Maven排除了我不需要的版本,这也不能解决我的问题.这是因为服务文件需要是一个特定的名称.具有该文件的多个版本可以防止这种情况.

Having multiple versions of the service file in my source code, to have Maven exclude the ones I don't need, also doesn't solve my issue. This is because the service file needs to be a specific name; having multiple versions of the file will prevent this.

摘要:是否可以使用Maven创建服务文件(META-INF/services)的内容?

Summary: Is there a way to create the contents of a service file (META-INF/services) with Maven?

谢谢!

推荐答案

如果可以创建数量很少的此类服务文件,则可以将其存储在项目的单独路径中.例如:

If you can create a reasonably low number of such service files you could store them in a separate path in your project. For example:

然后,您可以有选择地包含带有pom.xml的文件,如下所示(pom.xml功能强大但冗长的另一个示例):

Then you can selectively include the files with an pom.xml like this (one more example that pom.xml is powerful, but verbose):

<properties>
    <service.declaration.dir>src/serviceManifests</service.declaration.dir>
    <service.files.path>META-INF/services</service.files.path>
</properties>

<profiles>
    <profile>
        <id>foo</id>
        <build>
            <resources>
                <resource>
                    <directory>${service.declaration.dir}</directory>
                    <includes>
                        <include>${service.files.path}/foo</include>
                    </includes>
                </resource>
            </resources>
        </build>
    </profile>
    <profile>
        <id>bar</id>
        <build>
            <resources>
                <resource>
                    <directory>${service.declaration.dir}</directory>
                    <includes>
                        <include>${service.files.path}/bar</include>
                    </includes>
                </resource>
            </resources>
        </build>
    </profile>
</profiles>

要同时包含这两个文件,请运行:

To include both files you will then run:

mvn clean package -P foo -P bar

仅包含foo文件,您将运行:

To only include the foo file, you will run:

mvn clean package -P foo

这篇关于使用Maven创建META-INF/services文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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