Skip to content

Commit

Permalink
chore: make it optional
Browse files Browse the repository at this point in the history
  • Loading branch information
jmformenti committed May 20, 2024
1 parent 5aedb47 commit a9bcd80
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 15 deletions.
30 changes: 27 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<!-- Spring Boot Actuator displays build-related information
<!-- Spring Boot Actuator displays build-related information
if a META-INF/build-info.properties file is present -->
<goals>
<goal>build-info</goal>
Expand Down Expand Up @@ -378,7 +378,7 @@
<build>
<pluginManagement>
<plugins>
<!-- This plugin's configuration is used to store Eclipse m2e settings
<!-- This plugin's configuration is used to store Eclipse m2e settings
only. It has no influence on the Maven build itself. -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
Expand Down Expand Up @@ -434,6 +434,30 @@
</pluginManagement>
</build>
</profile>
<profile>
<id>mysql</id>
<properties>
<spring-boot.run.profiles>mysql,docker-compose</spring-boot.run.profiles>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-docker-compose</artifactId>
</dependency>
</dependencies>
</profile>
<profile>
<id>postgres</id>
<properties>
<spring-boot.run.profiles>postgres,docker-compose</spring-boot.run.profiles>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-docker-compose</artifactId>
</dependency>
</dependencies>
</profile>
</profiles>

</project>
</project>
41 changes: 37 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,51 @@ In its default configuration, Petclinic uses an in-memory database (H2) which
gets populated at startup with data. The h2 console is exposed at `http://localhost:8080/h2-console`,
and it is possible to inspect the content of the database using the `jdbc:h2:mem:<uuid>` URL. The UUID is printed at startup to the console.

A similar setup is provided for MySQL and PostgreSQL if a persistent database configuration is needed using docker compose.
A similar setup is provided for MySQL and PostgreSQL if a persistent database configuration is needed. Two options are available:

You only need to pass the Spring Boot profile at the time of running the application:
### Enable database via maven profile

Pass the profile for the corresponding database as an argument:

```bash
./mvnw spring-boot:run -Pmysql
```

or

```bash
./mvnw spring-boot:run -Ppostgres
```

### Manual configuration

Note that whenever the database type changes, the app needs to run with a different profile: `spring.profiles.active=mysql` for MySQL or `spring.profiles.active=postgres` for PostgreSQL.

You can start MySQL or PostgreSQL locally with whatever installer works for your OS or use docker:

```bash
docker run -e MYSQL_USER=petclinic -e MYSQL_PASSWORD=petclinic -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=petclinic -p 3306:3306 mysql:8.2
```

or

```bash
docker run -e POSTGRES_USER=petclinic -e POSTGRES_PASSWORD=petclinic -e POSTGRES_DB=petclinic -p 5432:5432 postgres:16.1
```

Further documentation is provided for [MySQL](https://github.com/spring-projects/spring-petclinic/blob/main/src/main/resources/db/mysql/petclinic_db_setup_mysql.txt)
and [PostgreSQL](https://github.com/spring-projects/spring-petclinic/blob/main/src/main/resources/db/postgres/petclinic_db_setup_postgres.txt).

Instead of vanilla `docker` you can also use the provided `docker-compose.yml` file to start the database containers. Each one has a profile just like the Spring profile:

```bash
./mvnw spring-boot:test-run -Dstart-class=org.springframework.samples.petclinic.MysqlTestApplication
docker-compose --profile mysql up
```

or

```bash
./mvnw spring-boot:test-run -Dstart-class=org.springframework.samples.petclinic.PostgresIntegrationTests
docker-compose --profile postgres up
```

## Test Applications
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application-docker-compose.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spring.docker.compose.profiles.active=${database}
2 changes: 0 additions & 2 deletions src/main/resources/application-mysql.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@ spring.datasource.username=${MYSQL_USER:petclinic}
spring.datasource.password=${MYSQL_PASS:petclinic}
# SQL is written to be idempotent so this is safe
spring.sql.init.mode=always
# docker compose
spring.docker.compose.profiles.active=mysql
2 changes: 0 additions & 2 deletions src/main/resources/application-postgres.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,3 @@ spring.datasource.username=${POSTGRES_USER:petclinic}
spring.datasource.password=${POSTGRES_PASS:petclinic}
# SQL is written to be idempotent so this is safe
spring.sql.init.mode=always
# docker compose
spring.docker.compose.profiles.active=postgres
36 changes: 36 additions & 0 deletions src/main/resources/db/mysql/petclinic_db_setup_mysql.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
================================================================================
=== Spring PetClinic sample application - MySQL Configuration ===
================================================================================

@author Sam Brannen
@author Costin Leau
@author Dave Syer

--------------------------------------------------------------------------------

1) Download and install the MySQL database (e.g., MySQL Community Server 5.1.x),
which can be found here: https://dev.mysql.com/downloads/. Or run the
"docker-compose.yml" from the root of the project (if you have docker installed
locally):

$ docker-compose up
...
mysql_1_eedb4818d817 | MySQL init process done. Ready for start up.
...

2) (Once only) create the PetClinic database and user by executing the "db/mysql/user.sql"
scripts. You can connect to the database running in the docker container using
`mysql -u root -h localhost --protocol tcp`, but you don't need to run the script there
because the petclinic user is already set up if you use the provided `docker-compose.yml`.

3) Run the app with `spring.profiles.active=mysql` (e.g. as a System property via the command
line, but any way that sets that property in a Spring Boot app should work). For example use

mvn spring-boot:run -Dspring-boot.run.profiles=mysql

To activate the profile on the command line.

N.B. the "petclinic" database has to exist for the app to work with the JDBC URL value
as it is configured by default. This condition is taken care of automatically by the
docker-compose configuration provided, or by the `user.sql` script if you run that as
root.
19 changes: 19 additions & 0 deletions src/main/resources/db/postgres/petclinic_db_setup_postgres.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
===============================================================================
=== Spring PetClinic sample application - PostgreSQL Configuration ===
===============================================================================

--------------------------------------------------------------------------------

1) Run the "docker-compose.yml" from the root of the project:

$ docker-compose up
...
spring-petclinic-postgres-1 | The files belonging to this database system will be owned by user "postgres".
...

2) Run the app with `spring.profiles.active=postgres` (e.g. as a System property via the command
line, but any way that sets that property in a Spring Boot app should work). For example use

mvn spring-boot:run -Dspring-boot.run.profiles=postgres

To activate the profile on the command line.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@

package org.springframework.samples.petclinic;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.testcontainers.containers.MySQLContainer;

/**
* PetClinic Spring Boot Application.
Expand All @@ -28,10 +32,15 @@
@Configuration
public class MysqlTestApplication {

@ServiceConnection
@Profile("mysql")
@Bean
static MySQLContainer<?> container() {
return new MySQLContainer<>("mysql:8.2");
}

public static void main(String[] args) {
new SpringApplicationBuilder(PetClinicApplication.class) //
.profiles("mysql") //
.run(args);
SpringApplication.run(PetClinicApplication.class, "--spring.profiles.active=mysql");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ static void available() {
public static void main(String[] args) {
new SpringApplicationBuilder(PetClinicApplication.class) //
.profiles("postgres") //
.properties( //
"spring.docker.compose.profiles.active=postgres" //
) //
.listeners(new PropertiesLogger()) //
.run(args);
}
Expand Down

0 comments on commit a9bcd80

Please sign in to comment.