You can use SDKMAN to manage your Java and Maven runtimes, and there is a very good tutorial about how to use SDKMAN here.
- Java 17
sdk install java 17.0.3.6.1-amzn
- Maven 3.8.5
sdk install maven 3.8.5
mvn clean install
mvn test
Test environment will use H2SQL in memory mode to persist your test data
mvn spring-boot:run
or using just Java
mvn clean install
java -jar target/api-crud-1.0-SNAPSHOT.jar
Local environment will use H2SQL in file to persist your changes
mvn spring-boot:run -Dspring-boot.run.profiles=local
or using just Java
mvn clean install
java -jar -Dspring.profiles.active=local target/api-crud-1.0-SNAPSHOT.jar
Running Docker MariaDB to provide Database support and run the application using your own Machine (for test and debug)
- To create the MariaDB container, if it is not created
docker run --name db -d -e MARIADB_ROOT_PASSWORD=password -e MARIADB_DATABASE=mydb mariadb:latest
- To stop the MariaDB container, if the container is already running
docker stop db
- To restart the MariaDB container, if the container is already created do
docker start db
- To remove the MariaDB container, stop the container first and then
docker rm db
Then, start the application using maven
mvn spring-boot:run -Dspring-boot.run.profiles=deploy
... or execute directly from the JAR file, after you build it usinbg maven
mvn clean install
java -jar -Dspring.profiles.active=deploy target/api-crud-1.0-SNAPSHOT.jar
To customize the deploy to connect in another MYSQL
database, please set these environment variables:
- MYSQL_HOST: connects to the defined host (default localhost)
- MYSQL_PORT: connects to the defined port (default 3306)
- MYSQL_DATABASE: select the database as default for opened connection (default mydb)
- MYSQL_USERNAME: connects to database using the username (default root)
- MYSQL_PASSWORD: connects to database using the username (default password)
- SQL_QUERIES_SHOW: show all SQL statements generated by JPA (default false)
- SQL_QUERIES_FORMAT: pretty format all SQL statements generated by JPA (default false)
Example:
export MYSQL_HOST=mydb.internal.net
export MYSQL_DATABASE=customer_data
export MYSQL_USERNAME=customers
export MYSQL_PASSWORD=str0ngp@ssw0rd
export SQL_QUERIES_SHOW=true
export SQL_QUERIES_FORMAT=true
java -jar -Dspring.profiles.active=deploy target/api-crud-1.0-SNAPSHOT.jar
It works because the application-deploy.yml
profile has placeholders to find the value from the environment variables or use a default value. Please realize when you create your local MariaDB container you set the all parameters to match default profile values.
I used IntelliJ Idea from JetBrains to build this application but you also can use Eclipse, or Netbeans to run this application.
For further reference, please consider the following sections:
- Official Apache Maven documentation
- Spring Boot Maven Plugin Reference Guide
- Create an OCI image
- Spring Boot DevTools
- Spring Web
The following guides illustrate how to use some features concretely: