Populate environment properties file using pom.xml proeprties - java

I want to fetch the artifactId from the pom.xml and use this artifactId to populate the properties file.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>xxxxx</groupId>
<artifactId>fm-service</artifactId>
<packaging>war</packaging>
<version>x.x.x-SNAPSHOT</version>
</project>
application.preperties
email.subject = "ALERT - [artifactId from pom.xml]"
Is is possible to do so? If yes how, if no, then please suggest alternatives.

Maven provides a way to filter resources.
First, the file application.properties has to be put into the src/main/resources directory. Then its content has to be:
email.subject = "ALERT - ${project.artifactId}"
Last (but not least) simply add this configuration to your POM:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
Now all files in the resources directory will be filtered, which means that all variables inside are resolved.

Related

.getResource("/filename") returns null when maven packaging is pom

I have a Maven project with a submodule, developed in IntelliJ, using Java 11.
Unless the pom.xml file contains <packaging>pom</packaging>, there is a warning that
'packaging' with value 'jar' is invalid. Aggregator projects require 'pom' as packaging.
But when packaging is set to "pom", the resource file that I need can't be loaded; a null value is returned, and an exception is thrown. From the main() method:
URL resource = getClass().getResource("/fx/gui.fxml");
Objects.requireNonNull(resource);
On the other hand, sometimes the submodule is not found, unless I ask for pom packaging. What I do then is: Request pom packaging, start the program and watch it fail, remove the pom packaging statement from pom.xml, start again and the program works.
My resource file is in the standard location src/main/resources/fx/gui.fxml. This location is also given in the pom file:
<build>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
</resources>
</build>
Please help me understand what's going on. Do I need pom packaging, and how can resources be loaded with it?
Looks like you have your source code in your parent pom.
A parent pom (with sub-modules) must have packaging as pom and cannot have java source code. See this question
You should move your code in a new sub-module.
The project or module which contains the source code, has to have the packaging as jar/war as per your requirement. It can not be packaging as pom. Generally pom packaging is used with parent module when you have multimodule project structure and the submodules would have packaging as jar/war. So in your case, if you have multimodule project structure, your parent packaging would be "pom" and all submodules(contain source code) must have jar/war. Note : Your parent module should not have source code, if it is then move your source code to submodule. Multimodule project structure is basically used where there are common dependencies and artifacts can be used in multiple submodules so that duplication can be removed.
Like below.
parent pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.abc.test</groupId>
<artifactId>testartifact</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<modules>
<module>rest-services</module>
</modules>
</project>
Submodule pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.abc.test</groupId>
<artifactId>testartifact</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>rest-services</artifactId>
<name>rest-services</name>
<dependencies>
<dependency>
</dependency>
</dependencies>
</project>

How to override profile property from command line [duplicate]

I have the following plain pom running by Maven 3.0.4.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>test</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
</project>
I am trying to override default settings in command line like this:
mvn -Dproject.build.finalName=build clean package
But this this is ignored, and I get test-1.0.jar. I've tried to change another properties, like outputDirectory, directory, artifactId, but also failed.
What is the proper way to do this thing?
See Introduction to the POM
finalName is created as:
<build>
<finalName>${project.artifactId}-${project.version}</finalName>
</build>
One of the solutions is to add own property:
<properties>
<finalName>${project.artifactId}-${project.version}</finalName>
</properties>
<build>
<finalName>${finalName}</finalName>
</build>
And now try:
mvn -DfinalName=build clean package

Enabling CDI Decorator based on environmental properties

I know that it is possible to configure Decorators in the "beans.xml" file that is embedded in the EAR to be deployed.
The problem is that I use the same EAR for all the environments, and the set of properties or specific configurations are stored in some folder outside the package.
I need to determine if a Decorator will be used or not "external beans.xml" or some similar mechanism (something that is outside the EAR).
Any ideas?
Thank you very much.
Normally, this won't work - standard means of enablement are beans.xml for per-archive approach and #Priority for global enablement. There is nothing like "external beans.xml".
Although there is a way to enable it with extension. You need to set up an extension and observe AfterTypeDiscovery event. From there you can
make use of public List<Class<?>> getDecorators(); which returns MUTABLE list of decorators - so you can add your own into the list (in a form of a Class). That should enable it.
Another scenario you can use, is to utilize build-time inclusion and processing.
If you know before hand, what properties activate specific decorators at build-time, then you can use maven resources, together with system properties, to define additional resources to be filtered, thus:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompant</groupId>
<artifactId>my-project-id</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<some.kind.of.selector.properties>$basedir}/src/main/resources/development</some.kind.of.selector.properties>
</properties>
<build>
<resources>
<resource>
<directory>${some.kind.of.selector.properties}</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
<profiles>
<profile>
<id>test</id>
<properties>
<some.kind.of.selector.properties>$basedir}/src/main/resources/test</some.kind.of.selector.properties>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<some.kind.of.selector.properties>$basedir}/src/main/resources/prod</some.kind.of.selector.properties>
</properties>
</profile>
</profiles>
</project>
The at build time, you can specify different beans.xml for every environment:
mvn clean install -Pprod
or even specify the property directly
mvn clean install -Dsome.kind.of.selector.properties=/path/to/additional/resources

Validating Maven pom.xml when boolean is a property

If I have a pom.xml like
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>test-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<resource.filtering>true</resource.filtering>
</properties>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>${resource.filtering}</filtering>
<includes>
<include>environment.properties</include>
</includes>
</resource>
</resources>
</build>
</project>
Eclipse marks the <filtering>${resource.filtering}</filtering> line with a validation error, because the schema tells it this should be a boolean, and that doesn't look like a boolean. I would rather not shut off the pom.xml validation, and it's annoying to see the error on the project. My goal is to be able to control whether resource filtering occurs based on Maven profiles. Is there a way of doing this that passes Eclipse's validation? I'm using Eclipse Luna (4.4).
I asked a similar question, and even tried filing a bug report with the Eclipse team. The answer I got was, "This is a result of an inherent conflict between the xsd that describes the pom format, and the aims of the pom file." So other than turning off validation, there isn't much one can do about these error messages.

How make relative link work in maven site generation?

I have one parent project with submodule, I have already read explanation about links, I try to run
mvn clean site
And open Child link from index.html. I also try to run site:run goal from maven-site-plugin and visit:
http://localhost:8080/
So relative link "about" apge from maven site do not work. What is wrong? May be I misunderstood with site working?
See parent and child pom.xml below
Parent pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>MavenSiteGeneration</groupId>
<artifactId>MavenSiteGeneration</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>Child</module>
</modules>
<description>Main project</description>
<!--<url>main-url</url>-->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.3</version>
<!--<configuration>
<port>9000</port>
<tempWebappDirectory>${basedir}/target/site/tempdir</tempWebappDirectory>
</configuration>-->
</plugin>
</plugins>
</build>
<distributionManagement>
<site>
<id>someid</id>
<url>http://bash.org</url>
</site>
</distributionManagement>
<reporting>
<plugins>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<configuration>
<dependencyLocationsEnabled>false</dependencyLocationsEnabled>
</configuration>
</plugin>
</plugins>
</reporting>
</project>
Child pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>MavenSiteGeneration</artifactId>
<groupId>MavenSiteGeneration</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<description>Child project</description>
<artifactId>Child</artifactId>
<!--<url>/child</url>-->
<!--<distributionManagement>
<site>
<id>asd</id>
<name>name</name>
<url>/child1112</url>
</site>
</distributionManagement>-->
</project>
Could anybody tell me what is wrong?
P.S. You can see some tag in comments, I have already tried to comment/uncmment. Relative link do not work.
You need to run mvn site:stage first to get a directory that includes the sites of the module and its submodule (by default in target/staging/). This is also explained in the FAQ of the maven-site-plugin.
To run this goal, you need to provide the information in <distributionManagement><site>, as you have in the files posted above.
Running mvn site:run will however still not work, that goal can only be used for single modules.

Categories