Reputation: 11890
I have some system properties I specify from the command line e.g.
mvn -Djdbc.url=... -Djdbc.user=... -Djdbc.password=... clean install
Instead of explicitly specifying each one from the command line I'd like to have a hardcoded .properties
file that defaults these values but I can still override them with the command line as needed.
I know the properties-maven-plugin
is one of several ways to read properties from a file, but how do I read properties and have them applied system wide? The properties seem to only work when used inside my pom.xml
. If I have some other confg file (example a Hibernate config file), it doesn't seem to work.
Example:
In my pom.xml
I have
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<goals>
<goal>set-system-properties</goal>
</goals>
<configuration>
<properties>
<!--
Alternatively this can read properties from a config file too
Hardcoded inline here for example purposes
-->
<property>
<jdbc.url>...</jdbc.url>
<jdbc.user>...</jdbc.user>
<jdbc.property>...</jdbc.property>
</property>
</properties>
</configuration>
</execution>
</executions>
</plugin>
Now I can use the jdbc.*
properties anywhere in pom.xml
and even override them from the command line as needed.
But in my Hibernate config file I have
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="dialect">org.hibernate.dialect.PostgreSQL95Dialect</property>
<property name="connection.url">${jdbc.url}</property>
...
...
</session-factory>
</hibernate-configuration>
The jdbc.url
and other properties show up as blank/null strings here because the properties I read from file only apply in my pom.xml
. However if I specify it from the command line it works here since that applies application-wide.
Is there a good way to read properties and have them applied as they would be from the command line, application-wide?
Thanks!
Upvotes: 0
Views: 521
Reputation: 2805
You are mixing up things;
the property you are referring to is inside the properties-maven-plugin
, and you are configuring it so it can read the property for file.
But this is not the way to use "shared" properties inside maven, it's just a way to read some value from a file.
For being visible outside pom, you have to specify a property as a "global", in example:
<?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>*********</groupId>
<artifactId>******</artifactId>
<packaging>war</packaging>
<version>*****</version>
<name>****</name>
<properties>
<!-- Hibernate Configuration-->
<jdbc.url>...</jdbc.url>
<jdbc.user>...</jdbc.user>
<jdbc.property>...</jdbc.property>
</properties>
Then, you can use these values into your hibernate conf:
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="dialect">org.hibernate.dialect.PostgreSQL95Dialect</property>
<property name="connection.url">${jdbc.url}</property>
...
...
</session-factory>
</hibernate-configuration>
That way of setting global properties into the pom is the only way to let them visible outside of it.
I suggest you to move your hibernate connection info inside your pom, and eventually create more profiles, so you can edit them when building the corrisponding package.
Upvotes: 1