wildfly-jar-maven-plugin icon indicating copy to clipboard operation
wildfly-jar-maven-plugin copied to clipboard

Externalize configurations Example

Open hendratommy opened this issue 4 years ago • 9 comments

How to externalize configurations? Like for example, in persistence.xml, I need to set the jndi data source name and hibernate.hbm2ddl.auto differently in my local, staging and prod.

hendratommy avatar Dec 29 '20 16:12 hendratommy

@hendratommy , is it something that you are already achieving with WildFly (without using bootable JAR)?

It would be interesting to see how you are doing it. If that is based on custom JBoss modules (as we can see there: https://www.java2novice.com/jboss/external-property-in-classpath/), that is something that you could mimic with bootable JAR by defining different maven profiles and using the capability to copy extra content inside a bootable JAR.

Perhaps a simpler solution would be to have each maven profile to contain resources configured differently. So, for example, a different persistence.xml file per profile.

jfdenise avatar Jan 05 '21 15:01 jfdenise

@jfdenise No, I haven't done it yet. I was thinking to use environment variable since the datasource layer is also use environment variable. Looking at this link seems like this feasible in standard WildFly.

hendratommy avatar Jan 05 '21 16:01 hendratommy

That seems interesting, please keep us posted with your result. We could then create a new example to cover your use-case. Thank-you.

jfdenise avatar Jan 05 '21 18:01 jfdenise

Hi,

It's working, I made small POC using bootable JAR to configure persistence.xml to use environment variables by adding ee layer and use cli to configure it. Here is the repo hendratommy/wf-articles.

However it is not what I'm expecting when working bootable jar, I was expecting something similar to Spring Boot.

hendratommy avatar Jan 06 '21 15:01 hendratommy

@hendratommy , thank-you, could you point me to the workflow/approach you would have preferred? Thank-you.

jfdenise avatar Jan 06 '21 15:01 jfdenise

I think Spring-Boot configuration is great way to configure our application. Our configuration basically all reside in single file src/main/resources/application.yml which we can fill with default values. These configuration value is overridable at runtime either by supplying environment variables or by putting config/application.yml in the same folder with the jar file. And we can also have el expression inside the config explicitly to lookup the values.

hendratommy avatar Jan 08 '21 07:01 hendratommy

@hendratommy , thank-you. I added your use-case to the JIRA that tracks support for yaml config: https://issues.redhat.com/browse/WFLY-13978

jfdenise avatar Jan 08 '21 08:01 jfdenise

Currently I do this with maven profiles and then resource filtering

Maven Profile

<profile>
	<id>sandbox</id>
	<activation>
		<activeByDefault>false</activeByDefault>
	</activation>
	<properties>
		<applicationMode>SANDBOX</applicationMode>
		<applicationVersion>${project.version}</applicationVersion>
		<applicationRootPath>/var/sandbox/</applicationRootPath>
		<datasource_JNDI_Name>java:jboss/datasources/PORTAL_DS</datasource_JNDI_Name>
		<hibernate.dialect>MySQL57InnoDB</hibernate.dialect>
		<hibernate.ddl_mode>create</hibernate.ddl_mode>
	</properties>
</profile>

Persistence.xml

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd" version="2.1">
	<persistence-unit name="primary" transaction-type="JTA">
		<jta-data-source>${datasource_JNDI_Name}</jta-data-source>
		<properties>
			<property name="hibernate.dialect" value="${hibernate.dialect}"/>
			<property name="hibernate.hbm2ddl.auto" value="${hibernate.ddl_mode}"/>
		</properties>
	</persistence-unit>
</persistence>

codylerum avatar Feb 20 '21 04:02 codylerum

@codylerum @hendratommy , FYI, using WF23 you can excute CLI script to adjust your configuration at runtime. We have an example that has been evolved to showcase the feature: https://github.com/wildfly-extras/wildfly-jar-maven-plugin/tree/master/examples/authentication It could open new possibilities in your context.

jfdenise avatar Mar 12 '21 18:03 jfdenise