Create META-INF/services file with Maven

耗尽温柔 提交于 2019-12-14 03:22:55

问题


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

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.

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.

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.

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

Thanks!


回答1:


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:

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

To only include the foo file, you will run:

mvn clean package -P foo


来源:https://stackoverflow.com/questions/42056142/create-meta-inf-services-file-with-maven

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!