java - 使用 Maven 创建 META-INF/services 文件

标签 java maven maven-3

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

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

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

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

总结:有没有办法用 Maven 创建服务文件(META-INF/services)的内容?

谢谢!

最佳答案

如果您可以创建相当少量的此类服务文件,您可以将它们存储在项目中的单独路径中。例如:

Example directory structure

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

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

要包含这两个文件,您将运行:

mvn clean package -P foo -P bar

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

mvn clean package -P foo

关于java - 使用 Maven 创建 META-INF/services 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42056142/

相关文章:

web-services - spring boot部署外部tomcat

maven - 您如何管理项目的依赖项(库)的许可证?

java - 如何管理模块化 Maven 项目的版本号?

linux - 使用 maven3 拉取依赖项 cloudstack 4 失败

Java vector 数学可读性

java - 带有 get() 和 requestBody 的 WebTestClient 不可用

java - Maven安装命令报错

hadoop - Oozie 3.3.0构建错误

java - 总是出现不可纠正的错误,无法解决,请帮忙吗?

java - 我应该如何覆盖 API 类的 equals 方法?