Create META-INF/services file with Maven - java

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!

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

Related

Include different logback.xml in production build

I use Maven for building and would like to use logback-debugging.xml for developing but package another , logback-info.xml, to the final product.
I came up with the following Maven configuration:
<profile>
<id>development</id>
<activation>
<property>
<name>dev</name>
</property>
</activation>
<build>
<resources>
<resource>
<filtering>true</filtering><!-- if it is neccessary -->
<directory>src/main/logging/develop</directory><!-- from -->
<targetPath>${project.build.outputDirectory}</targetPath><!-- to -->
<includes><!-- what -->
<include>logback.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
</build>
</profile>
</profiles>
In Maven settings file, we can activate one profile per default:
<settings>
[..]
<activeProfiles>
<activeProfile>development</activeProfile>
</activeProfiles>
[..]
</settings>
Another way is to move the default profile configuration to the "normal" pom build section.
To activate the development profile, pass the debug flag via command line, e.g. mvn package -Ddev
Logback searches for configuration files in a specific order (see the docs here). The first place it looks is for a file called logback-test.xml in your classpath. Since you're using maven, include that file in your test/resources directory. That way when you're running any of your tests, the logback-test.xml file is used. For your production code, include logback.xml in your main/resources directory.

how to access main/resources of a maven dependency lib?

got a maven submodule project including set of dlls in main\resoursces. How can I access from my other module project ?
The maven profile detects guest os and download dlls submodule project similar to Resolving a Maven dependency differently if the JVM in use is x86 or x64?
You can introduce a resource in your pom.xml file
<project>
<build>
<resources>
<resource>
<directory>${Submodule_path}</directory>
<includes>
<include>*.dll</include>
</includes>
<targetPath/>
</resource>
</resources>
</build>
</project>
Other option is to use "build-helper-maven-plugin" and introduce there the source that you want.

How to add an extra source directory for maven to use only when running tests or when debugging?

I have a set of configurable plugins that are being debugged.
They would not exist in a build of the main program but are part of the debugging regimen.
How to add them as resources that can be used in tests and debugging but not included in the final build?
The tests and debugging depends on the program locating the proper plugins and configuration directory/folders, compiling sources on the fly and then testing them with also stepping through them.
As a result, the source files used during the test must reside in the normal folder used during run-time.
I have this:
<build>
<resources>
<resource>
<directory>conf</directory>
</resource>
</resources>
</build>
The problem is that the resources are now being added to the final compile, which is not what is desired.
You'll have to keep the ressources in a different folder. as mentioned in the comments, you could use the standard archiecture (src/main/resources and src/test/resources) or as you tried you can define a test resources folder:
<build>
<sourceDirectory>src</sourceDirectory>
<testSourceDirectory>test</testSourceDirectory>
<resources>
<resource>
<directory>resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>testresources</directory>
</testResource>
</testResources>
Helpfull links you'll find here: access the ressources in junit and the maven guide

MAVEN , How can remove JSP page based on environment

I am building one war file using mvn clean install -Dlifecycle=dev . so i have variable lifecycle.
Now my requirement is , when i create build file for UAT/PROD it must exclude one jsp(index.jsp) from package .My jsp is in webApps directory parallel to resources.
Using profile only for one page filtering is not good idea i think.
Appreciate any help .
It starts with one JSP. Next is a customized CSS. Then different DB properties...
A profile is the way to go. Just create one, set its activation to the value of the variable, create another source folder with the JSP and add it to the resources in the profile.
So:
Create a folder src/dev/webapp in your project folder (so it is parallel to src/main/webapp)
Add a profile to your pom.xml that configures the war plugin
<profiles>
<profile>
<activation>
<property>
<name>lifecycle</name>
<value>dev</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<configuration>
<webResources>
<resource>
<directory>src/dev/webapp</directory>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
This copies the resources from src/dev/webapps into the merged target folder when the variable lifecycle is set to dev.
Even if those are quite some lines of XML for copying a single file I think it is not a good idea to do it different (e.g. with a plugin that deletes files) when using Maven. While you can customize Maven builds so they aren't recognizable any more, the whole idea is to use the conventions so others can easily read the process.

Copy a file during maven build phase

My situation is:
I have a Maven project, I have my java classes in /app/src/main/java, my resources in /app/src/main/resources and my webapp files in /app/src/main/webapp
I have a javascript file in /common/script.js
Now what I want is to include (copy) the javascript file to the war file during the build phase of maven. To be precise, I want the script.js to land in /js/ directory of the war archive, just as it was placed in /app/src/main/webapp/js before starting the build.
I need this to share one version of resource files among many web-apps.
Kind regards,
Q.
You could do something like this, as documented here.
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<webResources>
<resource>
<!-- this is relative to the pom.xml directory -->
<directory>../common</directory>
<targetPath>/js</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
...
</project>
You can use the mojo copy-resources to copy resources which are not in the default maven layout or not declared in the build/resources element.
Check
"maven-resources-plugin"
You can use maven-resources plugin to copy a file to the desired location. Before or after a war has been built

Categories

Resources