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

Create META-INF/services file with Maven

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

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

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

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

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

谢谢!

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

然后你可以像这样有选择地包含带有 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