Skip to content

Add a Java Spring Boot Example #1261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/development/python/pip/devbox.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
"lockfile_version": "1",
"packages": {
"python310": {
"plugin_version": "0.0.1",
"resolved": "github:NixOS/nixpkgs/f80ac848e3d6f0c12c52758c0f25c10c97ca3b62#python310"
},
"[email protected]": {
"last_modified": "2023-05-01T16:53:22Z",
"plugin_version": "0.0.1",
"resolved": "github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#python310Packages.pip",
"version": "23.0.1"
}
Expand Down
63 changes: 63 additions & 0 deletions examples/stacks/laravel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Laravel

Laravel is a powerful web application framework built with PHP. It's a great choice for building web applications and APIs.

This example shows how to build a simple Laravel application backed by MariaDB and Redis. It uses Devbox Plugins for all 3 Nix packages to simplify configuration

[![Open In Devbox.sh](https://jetpack.io/img/devbox/open-in-devbox.svg)](https://devbox.sh/github.com/jetpack-io/devbox/?folder=examples/stacks/laravel)

## How to Run

1. Install [Devbox](https://www.jetpack.io/devbox/docs/installing_devbox/)

1. Create a new Laravel App by running `devbox create --template laravel`. This will create a new Laravel project in your current directory.

1. Start your MariaDB and Redis services by running `devbox services up`.
1. This step will also create an empty MariaDB Data Directory and initialize your database with the default settings
2. This will also start the php-fpm service for serving your PHP project over fcgi. Learn more about [PHP-FPM](https://www.php.net/manual/en/install.fpm.php)

1. Create the laravel database by running `devbox run db:create`, and then run Laravel's initial migrations using `devbox run db:migrate`

1. You can now start the artisan server by running `devbox run serve:dev`. This will start the server on port 8000, which you can access at `localhost:8000`

1. If you're using Laravel on Devbox Cloud, you can test the app by appending `/port/8000` to your Devbox Cloud URL

1. For more details on building and developing your Laravel project, visit the [Laravel Docs](https://laravel.com/docs/10.x)


## How to Recreate this Example

### Creating the Laravel Project

1. Create a new project with `devbox init`

2. Add the packages using the command below. Installing the packages with `devbox add` will ensure that the plugins are activated:

```bash
devbox add mariadb@latest, [email protected], nodejs@18, redis@latest, php81Packages.composer@latest
```

3. Run `devbox shell` to start your shell. This will also initialize your database by running `initdb` in the init hook.

4. Create your laravel project by running:

```bash
composer create-project laravel/laravel tmp

mv tmp/* tmp/.* .
```

### Setting up MariaDB

To use MariaDB, you need to create the default Laravel database. You can do this by running the following commands in your `devbox shell`:

```bash
# Start the MariaDB service
devbox services up mariadb -b

# Create the database
mysql -u root -e "CREATE DATABASE laravel;"

# Once you're done, stop the MariaDB service
devbox services stop mariadb
```
37 changes: 37 additions & 0 deletions examples/stacks/spring/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
HELP.md
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/
11 changes: 11 additions & 0 deletions examples/stacks/spring/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Spring Boot Example

This example combines Java, Spring Boot, and MySQL to expose a simple REST API. This example is based on the official [Spring Boot Documentation](https://spring.io/guides/gs/accessing-data-mysql/).

## How to Run

1. Install [Devbox](https://www.jetpack.io/devbox/docs/installing_devbox/)

1. Prepare the database by running `devbox run setup_db`. This will create the user and database that Spring expects in `stacks/spring/src/main/resources/application.properties`
1. You can now start the Spring Boot service by running `devbox run bootRun`. This will start your MySQL service and run the application
1. You can test the service using `GET localhost:8080/demo/all` or `POST localhost:8080/demo/add`. See the Spring Documentation for more details.
28 changes: 28 additions & 0 deletions examples/stacks/spring/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
plugins {
id 'java'
id 'org.springframework.boot' version '3.1.1'
id 'io.spring.dependency-management' version '1.1.0'
}

group = 'com.devbox.example.spring'
version = '0.0.1-SNAPSHOT'

java {
sourceCompatibility = '17'
}

repositories {
mavenCentral()
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
implementation 'org.springframework.boot:spring-boot-starter-web'
runtimeOnly 'com.mysql:mysql-connector-j'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
useJUnitPlatform()
}
32 changes: 32 additions & 0 deletions examples/stacks/spring/devbox.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"packages": [
"jdk@17",
"gradle@latest",
"mysql80@latest"
],
"shell": {
"init_hook": [
"echo 'Welcome to devbox!'\n",
"echo 'Setup MySQL by running `devbox run setup_db`",
"echo 'Run the example using `devbox run bootRun"
],
"scripts": {
"bootRun": [
"devbox services up -b",
"./gradlew bootRun",
"devbox services stop"
],
"build": [
"./gradlew build"
],
"setup_db": [
"devbox services up mysql -b",
"mysql -u root < setup_db.sql",
"devbox services stop"
],
"test": [
"./gradlew test"
]
}
}
}
22 changes: 22 additions & 0 deletions examples/stacks/spring/devbox.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"lockfile_version": "1",
"packages": {
"gradle@latest": {
"last_modified": "2023-06-29T16:20:38Z",
"plugin_version": "0.0.1",
"resolved": "github:NixOS/nixpkgs/3c614fbc76fc152f3e1bc4b2263da6d90adf80fb#gradle",
"version": "8.0.1"
},
"jdk@17": {
"last_modified": "2023-06-29T16:20:38Z",
"resolved": "github:NixOS/nixpkgs/3c614fbc76fc152f3e1bc4b2263da6d90adf80fb#jdk17",
"version": "17.0.7+7"
},
"mysql80@latest": {
"last_modified": "2023-06-29T16:20:38Z",
"plugin_version": "0.0.1",
"resolved": "github:NixOS/nixpkgs/3c614fbc76fc152f3e1bc4b2263da6d90adf80fb#mysql80",
"version": "8.0.33"
}
}
}
1 change: 1 addition & 0 deletions examples/stacks/spring/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.gradle.java.home=/nix/store/csfdcflywb9gj20b1mvsp1ixy6f398bg-zulu17.34.19-ca-jdk-17.0.3
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this jar file isn't needed and can be deleted

Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading