Skip to content

Commit b46a28d

Browse files
felipe-alves-moraesarjantijms
authored andcommitted
Example of a configuration based on environment using payara (#3)
* Example of a configuration based on environment using payara * Making the config simpler by passing the whole DB URL instead of peaces of it; * Making the config simpler by passing the whole DB URL instead of peaces of it; * Updating README to match the new config
1 parent 456cd7d commit b46a28d

20 files changed

+383
-0
lines changed
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#
2+
# Default excludes
3+
#
4+
5+
# Binaries
6+
*.7z
7+
*.dmg
8+
*.gz
9+
*.iso
10+
*.jar
11+
*.rar
12+
*.tar
13+
*.zip
14+
*.war
15+
*.ear
16+
*.sar
17+
*.class
18+
19+
# Maven
20+
target/
21+
22+
# IntelliJ project files
23+
*.iml
24+
*.iws
25+
*.ipr
26+
.idea/
27+
28+
# eclipse project file
29+
.settings/
30+
.classpath
31+
.project
32+
33+
# NetBeans specific
34+
nbproject/private/
35+
build/
36+
nbbuild/
37+
dist/
38+
nbdist/
39+
nbactions.xml
40+
nb-configuration.xml
41+
42+
43+
# OS
44+
.DS_Store
45+
46+
# Misc
47+
*.swp
48+
release.properties
49+
pom.xml.releaseBackup
50+
pom.xml.tag
51+
/backend/derby.log
52+
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Environment Aware
2+
3+
This project was made using this archetype that I created: [javaee8-archetype](https://github.com/felipe-alves-moraes/javaee8-archetype)
4+
5+
With this approach I set my datasource configuration based on the environment that I was building my project.
6+
7+
The idea here is that you application should only have references of the environment around it through known variables.
8+
9+
To do this I've used [payara variable substitution](https://docs.payara.fish/documentation/payara-server/server-configuration/var-substitution/types-of-variables.html) feature.
10+
11+
This way I was able to let the environment handle infrastructure configuration.
12+
13+
You can check the configuration inside the `web.xml` file and in each of the docker env-files inside the docker folder.
14+
15+
## Cons of this approach
16+
When in development process you will have to set the environment variables in your machine or in you IDE. (This is not hard to achieve but some people may find this annoying)
17+
18+
Vendor dependent configuration.
19+
20+
## Pros
21+
Simple configuration of resources.
22+
23+
Don't need to do any magical trick in you app to handle configuration.
24+
25+
CI/CD ready configuration.
26+
27+
# Build
28+
`mvn clean package && docker build -f ./docker/Dockerfile -t br.com.fmoraes/environment-aware-configuration .`
29+
30+
# RUN
31+
32+
`docker rm -f environment-aware-configuration || true && docker run --env-file=./docker/local_env_properties -d -p 8080:8080 -p 4848:4848 --name environment-aware-configuration br.com.fmoraes/environment-aware-configuration`
33+
34+
You can also run the application using the script `buildAndRun.sh` by default it will pick the `local_env_properties` file to run docker, you can change it passing one of the following envs as argument: `dev`, `stage` and `prod`.
35+
36+
To run it locally in you IDE you have to configure these Environment Variables:
37+
38+
```
39+
DATASOURCE_CLASS=org.apache.derby.jdbc.ClientDriver
40+
DB_ENGINE=derby
41+
DB_URL=//host.docker.internal:1527/environment-aware-configuration
42+
DB_USER=app
43+
DB_PASSWORD=app
44+
```
45+
46+
OBS. You need to have a local instance of Apache Derby(Java DB) running in you local machine to this project to work, or configure any DB that you want changing the configuration files.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/sh
2+
ENV=$1
3+
4+
if [ -z "$ENV" ]
5+
then
6+
ENV='local'
7+
fi
8+
ENV_FILE="$ENV"_"env_properties"
9+
10+
mvn clean package && docker build -f ./docker/Dockerfile -t br.com.fmoraes/environment-aware-configuration .
11+
docker rm -f environment-aware-configuration || true && docker run --env-file=./docker/$ENV_FILE -d -p 8080:8080 -p 4848:4848 --name environment-aware-configuration br.com.fmoraes/environment-aware-configuration
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM payara/micro:5.182
2+
3+
ADD ./target/libs /opt/libs
4+
5+
COPY ./target/environment-aware-configuration.war ${DEPLOY_DIR}
6+
7+
ENTRYPOINT ["java", "-jar", "/opt/payara/payara-micro.jar", \
8+
"--addLibs", "/opt/libs/", \
9+
"--deploy", "/opt/payara/deployments/environment-aware-configuration.war"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
DATASOURCE_CLASS=org.apache.derby.jdbc.ClientDriver
2+
DB_ENGINE=derby
3+
DB_URL=//host.docker.internal:1527/environment-aware-configuration
4+
DB_USER=app
5+
DB_PASSWORD=app
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
DATASOURCE_CLASS=org.apache.derby.jdbc.ClientDriver
2+
DB_ENGINE=derby
3+
DB_URL=//host.docker.internal:1527/environment-aware-configuration
4+
DB_USER=app
5+
DB_PASSWORD=app
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
DATASOURCE_CLASS=org.apache.derby.jdbc.ClientDriver
2+
DB_ENGINE=derby
3+
DB_URL=//host.docker.internal:1527/environment-aware-configuration
4+
DB_USER=app
5+
DB_PASSWORD=app
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
DATASOURCE_CLASS=org.apache.derby.jdbc.ClientDriver
2+
DB_ENGINE=derby
3+
DB_URL=//host.docker.internal:1527/environment-aware-configuration
4+
DB_USER=app
5+
DB_PASSWORD=app
+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>br.com.fmoraes</groupId>
5+
<artifactId>environment-aware-configuration</artifactId>
6+
<version>1.0-SNAPSHOT</version>
7+
<packaging>war</packaging>
8+
<dependencies>
9+
<dependency>
10+
<groupId>javax</groupId>
11+
<artifactId>javaee-api</artifactId>
12+
<version>8.0</version>
13+
<scope>provided</scope>
14+
</dependency>
15+
<dependency>
16+
<groupId>org.eclipse.microprofile</groupId>
17+
<artifactId>microprofile</artifactId>
18+
<version>1.3</version>
19+
<type>pom</type>
20+
<scope>provided</scope>
21+
</dependency>
22+
<dependency>
23+
<groupId>junit</groupId>
24+
<artifactId>junit</artifactId>
25+
<version>4.12</version>
26+
<scope>test</scope>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.apache.derby</groupId>
30+
<artifactId>derbyclient</artifactId>
31+
<version>10.14.2.0</version>
32+
</dependency>
33+
</dependencies>
34+
35+
<build>
36+
<finalName>environment-aware-configuration</finalName>
37+
<plugins>
38+
<plugin>
39+
<groupId>fish.payara.maven.plugins</groupId>
40+
<artifactId>payara-micro-maven-plugin</artifactId>
41+
<version>${payaramicro.maven.plugin.version}</version>
42+
<configuration>
43+
<deployWar>true</deployWar>
44+
<javaCommandLineOptions>
45+
<option>
46+
<value>-Xdebug</value>
47+
</option>
48+
<option>
49+
<key>-Xrunjdwp:transport</key>
50+
<value>dt_socket,server=y,suspend=n,address=5005</value>
51+
</option>
52+
</javaCommandLineOptions>
53+
<commandLineOptions>
54+
<option>
55+
<key>--addLibs</key>
56+
<value>${project.build.directory}/libs/</value>
57+
</option>
58+
</commandLineOptions>
59+
</configuration>
60+
</plugin>
61+
<plugin>
62+
<artifactId>maven-war-plugin</artifactId>
63+
<version>3.2.2</version>
64+
<configuration>
65+
<packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes>
66+
</configuration>
67+
</plugin>
68+
<plugin>
69+
<groupId>org.apache.maven.plugins</groupId>
70+
<artifactId>maven-dependency-plugin</artifactId>
71+
<version>3.1.1</version>
72+
<executions>
73+
<execution>
74+
<id>copy-dependencies</id>
75+
<phase>package</phase>
76+
<goals>
77+
<goal>copy-dependencies</goal>
78+
</goals>
79+
<configuration>
80+
<outputDirectory>${project.build.directory}/libs</outputDirectory>
81+
<includeScope>runtime</includeScope>
82+
<excludeScope>provided</excludeScope>
83+
</configuration>
84+
</execution>
85+
</executions>
86+
</plugin>
87+
<plugin>
88+
<groupId>org.liquibase</groupId>
89+
<artifactId>liquibase-maven-plugin</artifactId>
90+
<version>3.6.1</version>
91+
<configuration>
92+
<propertyFile>src/main/resources/liquibase/liquibase-${liquibase.env}.properties</propertyFile>
93+
</configuration>
94+
<dependencies>
95+
<dependency>
96+
<groupId>org.apache.derby</groupId>
97+
<artifactId>derbyclient</artifactId>
98+
<version>10.14.2.0</version>
99+
</dependency>
100+
</dependencies>
101+
</plugin>
102+
</plugins>
103+
</build>
104+
<properties>
105+
<maven.compiler.source>1.8</maven.compiler.source>
106+
<maven.compiler.target>1.8</maven.compiler.target>
107+
<failOnMissingWebXml>false</failOnMissingWebXml>
108+
<payaramicro.maven.plugin.version>1.0.1</payaramicro.maven.plugin.version>
109+
<liquibase.env>local</liquibase.env>
110+
</properties>
111+
<name>environment-aware-configuration</name>
112+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package br.com.fmoraes.environmentawareconfiguration;
2+
3+
import javax.ws.rs.ApplicationPath;
4+
import javax.ws.rs.core.Application;
5+
6+
@ApplicationPath("resources")
7+
public class JAXRSConfiguration extends Application {
8+
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package br.com.fmoraes.environmentawareconfiguration.model;
2+
3+
import java.io.Serializable;
4+
import javax.persistence.Entity;
5+
import javax.persistence.GeneratedValue;
6+
import javax.persistence.GenerationType;
7+
import javax.persistence.Id;
8+
9+
/**
10+
*
11+
* @author fmoraes
12+
*/
13+
@Entity
14+
public class Person implements Serializable {
15+
16+
@Id
17+
@GeneratedValue(strategy = GenerationType.AUTO)
18+
private Long id;
19+
private String name;
20+
21+
protected Person() {
22+
23+
}
24+
25+
public Person(String name) {
26+
this.name = name;
27+
}
28+
29+
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package br.com.fmoraes.environmentawareconfiguration.resources;
2+
3+
import br.com.fmoraes.environmentawareconfiguration.model.Person;
4+
import javax.ejb.Stateless;
5+
import javax.persistence.EntityManager;
6+
import javax.persistence.PersistenceContext;
7+
import javax.ws.rs.POST;
8+
import javax.ws.rs.Path;
9+
10+
/**
11+
*
12+
* @author fmoraes
13+
*/
14+
@Path("person")
15+
@Stateless
16+
public class PersonResource {
17+
18+
@PersistenceContext
19+
private EntityManager em;
20+
21+
@POST
22+
public void create() {
23+
em.persist(new Person("Felipe"));
24+
}
25+
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<persistence version="2.1" 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">
3+
<persistence-unit name="environment-aware-configurationPU" transaction-type="JTA">
4+
<jta-data-source>java:app/datasources/environment-aware-configurationDS</jta-data-source>
5+
<exclude-unlisted-classes>false</exclude-unlisted-classes>
6+
<properties>
7+
<property name="javax.persistence.schema-generation.database.action" value="create"/>
8+
</properties>
9+
</persistence-unit>
10+
</persistence>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<databaseChangeLog
2+
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
5+
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd
6+
http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
7+
8+
<!--Include all your db change files like the example below-->
9+
<!-- <include file="liquibase/changelog/StoryNumber-changelog.sql" />-->
10+
</databaseChangeLog>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
changeLogFile: liquibase/changelog/db.changelog-master.xml
2+
driver: org.apache.derby.jdbc.ClientDriver
3+
url: jdbc:derby://localhost:1527/environment-aware-configuration_dev
4+
username: app
5+
password: app
6+
verbose: true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
changeLogFile: liquibase/changelog/db.changelog-master.xml
2+
driver: org.apache.derby.jdbc.ClientDriver
3+
url: jdbc:derby://localhost:1527/environment-aware-configuration
4+
username: app
5+
password: app
6+
verbose: true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
changeLogFile: liquibase/changelog/db.changelog-master.xml
2+
driver: org.apache.derby.jdbc.ClientDriver
3+
url: jdbc:derby://localhost:1527/environment-aware-configuration_prod
4+
username: app
5+
password: app
6+
verbose: true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
changeLogFile: liquibase/changelog/db.changelog-master.xml
2+
driver: org.apache.derby.jdbc.ClientDriver
3+
url: jdbc:derby://localhost:1527/environment-aware-configuration_stage
4+
username: app
5+
password: app
6+
verbose: true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
5+
bean-discovery-mode="all">
6+
</beans>

0 commit comments

Comments
 (0)