|
| 1 | +title: Jenkins X - Java Native Image Prod |
| 2 | +description: Creating a Java Native Image application and run it as Production with Jenkins X - Quarkus - 3/8 |
| 3 | +hero: Quarkus - 3/8 |
| 4 | + |
| 5 | +# Create Quarkus Application |
| 6 | + |
| 7 | +There are several ways you can create a Quarkus application. |
| 8 | + |
| 9 | +You can create one by going to [code.quarkus.io](https://code.quarkus.io/), fill in your details and select your dependencies - this is an API call, so automatable. You can start with an maven archetype and add Quarkus details, or you can start from one of the [Quarkus Quickstarts](https://github.com/quarkusio/quarkus-quickstarts). |
| 10 | + |
| 11 | +In this guide, we start with a Quarkus Quickstart (Spring Data JPA to be exact) and modify this to suit our needs. |
| 12 | + |
| 13 | +## Fork, Clone, or Copy |
| 14 | + |
| 15 | +We're going to start from Quarkus' `spring-data-jpa-quickstart`, I leave it up to you how you get the code in your own repository. You can fork it, clone it and copy it or whatever floats your boat. |
| 16 | + |
| 17 | +You can find the quickstart [here](https://github.com/quarkusio/quarkus-quickstarts/tree/master/spring-data-jpa-quickstart), and for reference, the Quarkus Guide that comes with it, [here](https://quarkus.io/guides/spring-data-jpa). |
| 18 | + |
| 19 | +## Update Dependencies |
| 20 | + |
| 21 | +We're going to modify the application, so lets dive into the `pom.xml` and make our changes. |
| 22 | + |
| 23 | +First, we will use MySQL as our RDBMS so we drop the `quarkus-jdbc-postgresql` dependency. |
| 24 | + |
| 25 | +Next, we add our other spring dependencies and the `quarkus-jdbc-mysql` for MySQL. |
| 26 | + |
| 27 | +```xml |
| 28 | +<dependency> |
| 29 | + <groupId>io.quarkus</groupId> |
| 30 | + <artifactId>quarkus-spring-web</artifactId> |
| 31 | +</dependency> |
| 32 | +<dependency> |
| 33 | + <groupId>io.quarkus</groupId> |
| 34 | + <artifactId>quarkus-spring-di</artifactId> |
| 35 | +</dependency> |
| 36 | +<dependency> |
| 37 | + <groupId>io.quarkus</groupId> |
| 38 | + <artifactId>quarkus-jdbc-mysql</artifactId> |
| 39 | +</dependency> |
| 40 | +``` |
| 41 | + |
| 42 | +## Transform Resource to Controller |
| 43 | + |
| 44 | +Now that we're using Spring Web, we are going to change our Resource - FruitResource - to a Spring Web Controller. |
| 45 | + |
| 46 | +Replace this annotation: |
| 47 | + |
| 48 | +```java |
| 49 | +@Path("/fruits") |
| 50 | +``` |
| 51 | + |
| 52 | +With this. |
| 53 | + |
| 54 | +```java |
| 55 | +@RestController |
| 56 | +@RequestMapping(value = "/fruits") |
| 57 | +``` |
| 58 | + |
| 59 | +Replace all `@PathParams` with Spring's `@PathVariable`'s. |
| 60 | +Mind you, these require the name of the variable as a parameter. |
| 61 | + |
| 62 | +For example: |
| 63 | + |
| 64 | +```java |
| 65 | +@POST |
| 66 | +@Path("/name/{name}/color/{color}") |
| 67 | +@Produces("application/json") |
| 68 | +public Fruit create(@PathParam String name, @PathParam String color) {} |
| 69 | +``` |
| 70 | + |
| 71 | +Becomes: |
| 72 | + |
| 73 | +```java |
| 74 | +@PostMapping("/name/{name}/color/{color}") |
| 75 | +public Fruit create(@PathVariable(value = "name") String name, @PathVariable(value = "color") String color) { |
| 76 | +``` |
| 77 | + |
| 78 | +Then, replace the Http method annotations for the methods: |
| 79 | + |
| 80 | +* `@GET` with `@GetMapping` |
| 81 | +* `@DELETE` with `@DeleteMapping` |
| 82 | +* `@POST` with `@PostMapping` |
| 83 | +* `@PUT` with `@PutMapping` |
| 84 | + |
| 85 | +!!! note |
| 86 | + Spring's annotation includes the path, so you can collapse the `@PATH` into the Http method annotation. |
| 87 | +
|
| 88 | + For example: |
| 89 | + |
| 90 | + ```java |
| 91 | + @GET |
| 92 | + @Path("/color/{color}") |
| 93 | + @Produces("application/json") |
| 94 | + ``` |
| 95 | +
|
| 96 | + Becomes: |
| 97 | +
|
| 98 | + ```java |
| 99 | + @GetMapping("/color/{color}") |
| 100 | + ``` |
| 101 | +
|
| 102 | +??? example "FruitResource.java" |
| 103 | +
|
| 104 | + ```java |
| 105 | + @RestController |
| 106 | + @RequestMapping(value = "/fruits") |
| 107 | + public class FruitResource { |
| 108 | +
|
| 109 | + private final FruitRepository fruitRepository; |
| 110 | +
|
| 111 | + public FruitResource(FruitRepository fruitRepository) { |
| 112 | + this.fruitRepository = fruitRepository; |
| 113 | + } |
| 114 | +
|
| 115 | + @GetMapping("/") |
| 116 | + public List<Fruit> findAll() { |
| 117 | + ... |
| 118 | + } |
| 119 | +
|
| 120 | + @DeleteMapping("{id}") |
| 121 | + public ResponseEntity<Long> delete(@PathVariable(value = "id") long id) { |
| 122 | + ... |
| 123 | + } |
| 124 | +
|
| 125 | + @PostMapping("/name/{name}/color/{color}") |
| 126 | + public Fruit create(@PathVariable(value = "name") String name, @PathVariable(value = "color") String color) { |
| 127 | + ... |
| 128 | + } |
| 129 | +
|
| 130 | + @PutMapping("/id/{id}/color/{color}") |
| 131 | + public Fruit changeColor(@PathVariable(value = "id") Long id, @PathVariable(value = "color") String color) { |
| 132 | + ... |
| 133 | + } |
| 134 | +
|
| 135 | + @GetMapping("/color/{color}") |
| 136 | + public List<Fruit> findByColor(@PathVariable(value = "color") String color) { |
| 137 | + ... |
| 138 | + } |
| 139 | + } |
| 140 | + ``` |
| 141 | +
|
| 142 | +## Update Application Properties |
| 143 | +
|
| 144 | +Let's update the applications properties, an initial configuration for MySQL. |
| 145 | + |
| 146 | +For the username and password, we use environment variables wich we will address later - when we import the aplication into Jenkins X. |
| 147 | + |
| 148 | +The JDBC URL looks a bit weird, but this has to do with [how Google Cloud SQL can be accessed via Kubernetes](https://cloud.google.com/sql/docs/mysql/connect-kubernetes-engine?hl=en_US). This is enough for now, we'll come back for more, don't worry. |
| 149 | + |
| 150 | +```properties |
| 151 | +quarkus.datasource.db-kind=mysql |
| 152 | +quarkus.datasource.jdbc.url=jdbc:mysql://127.0.0.1:3306/fruits |
| 153 | +quarkus.datasource.jdbc.max-size=8 |
| 154 | +quarkus.datasource.jdbc.min-size=2 |
| 155 | + |
| 156 | +quarkus.datasource.username=${GOOGLE_SQL_USER} |
| 157 | +quarkus.datasource.password=${GOOGLE_SQL_PASS} |
| 158 | +``` |
| 159 | + |
| 160 | +## Unit Testing |
| 161 | + |
| 162 | +In order to build our application, we now need a MySQL database as we have unit tests, testing our FruitResource - as we should! Locally, we can address this by running MySQL as a Docker container. Unfortunately, when building applications in Jenkins X, we don't have access to Docker - you could, but in Kubernetes this is a big no-no. So, for now, we'll spin up an H2 database in MySQL mode to avoid the issue, [but we should probably come back to that later](https://phauer.com/2017/dont-use-in-memory-databases-tests-h2/). |
| 163 | + |
| 164 | +This was in part inspired by [@hantsy's post on creating your first Quarkus application](https://medium.com/@hantsy/kickstart-your-first-quarkus-application-cde54f469973) on Medium, definitely worth a read in general. |
| 165 | +
|
| 166 | +In order to use the H2 database for our unit tests, we have to make three changes: |
| 167 | +
|
| 168 | +1. add the H2 test dependency |
| 169 | +1. create a `application.properties` file for tests, in `src/test/resources` |
| 170 | +1. annotate our test class with `@QuarkusTestResource(H2DatabaseTestResource.class)`, so Quarkus spins up the H2 database |
| 171 | +
|
| 172 | +
|
| 173 | +```xml |
| 174 | +<dependency> |
| 175 | + <groupId>io.quarkus</groupId> |
| 176 | + <artifactId>io.quarkus:quarkus-test-h2</artifactId> |
| 177 | + <scope>test</scope> |
| 178 | +</dependency> |
| 179 | +``` |
| 180 | +
|
| 181 | +!!! example "src/test/java/../FruitResourceTest.java" |
| 182 | + |
| 183 | + ```java |
| 184 | + @QuarkusTestResource(H2DatabaseTestResource.class) |
| 185 | + @QuarkusTest |
| 186 | + class FruitResourceTest { |
| 187 | + ... |
| 188 | + } |
| 189 | + ``` |
| 190 | +
|
| 191 | +!!! example "src/test/resoureces/application.properties" |
| 192 | +
|
| 193 | + ```properties |
| 194 | + quarkus.datasource.url=jdbc:h2:tcp://localhost/mem:test |
| 195 | + quarkus.datasource.driver=org.h2.Driver |
| 196 | + quarkus.hibernate-orm.database.generation = drop-and-create |
| 197 | + quarkus.hibernate-orm.log.sql=true |
| 198 | + ``` |
| 199 | +
|
| 200 | +## Replace jsonb with jackson |
| 201 | +
|
| 202 | +Spring depends on `Jackson` for marshalling JSON to and from Java Objects. |
| 203 | +It makes sense to make our application depend on the same libary to reduce potential conflicts. |
| 204 | +
|
| 205 | +Remove the `quarkus-resteasy-jsonb` dependency: |
| 206 | +
|
| 207 | +```xml |
| 208 | +<dependency> |
| 209 | + <groupId>io.quarkus</groupId> |
| 210 | + <artifactId>quarkus-resteasy-jsonb</artifactId> |
| 211 | +</dependency> |
| 212 | +``` |
| 213 | +
|
| 214 | +And add `quarkus-resteasy-jackson`. |
| 215 | +
|
| 216 | +```xml |
| 217 | +<dependency> |
| 218 | + <groupId>io.quarkus</groupId> |
| 219 | + <artifactId>quarkus-resteasy-jackson</artifactId> |
| 220 | +</dependency> |
| 221 | +``` |
| 222 | +
|
| 223 | +## Next Steps |
| 224 | +
|
| 225 | +Running `mvn clean test` should result in a succesful build, with two tests testing most of our application. |
| 226 | +
|
| 227 | +This means we're ready to go to the next step, importing the application into Jenkins X! |
0 commit comments