2

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!

BlueBucket7
  • 185
  • 3
  • 13
  • This is rather unclear. Can you show what you currently have, in terms of structure / content? And what you expect as result? – Tunaki Feb 05 '17 at 19:23
  • If you take a look at the link to the Ant page, in the "service" section, you'll see that it allows you to create a services file by taking two parameters: 1. "type" - the name of the service file. 2. "provider" - the class name of the provider. So If I specified 1. "java.my.serviceFile" 2. "com.test.mytest", I would end up with a file called "java.my.serviceFile" which will contain the text "com.test.mytest". I would like to do the same with Maven. – BlueBucket7 Feb 05 '17 at 19:50
  • Ok, so you want to create dynamically the service file, given an intername classified name, and an implementation classified name. I don't think there's a built-in way for this; you might have to call the Ant task through the Maven Antrun Plugin. – Tunaki Feb 05 '17 at 19:54

1 Answers1

3

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:

Example directory structure

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
Apokralipsa
  • 2,674
  • 17
  • 28