Skip to content
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

{WIP} examples/spring-devtools #173

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
.DS_Store
old

# Eclipse
.project
.classpath
.settings/
54 changes: 54 additions & 0 deletions java/examples/spring-devtools/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# s2i-java-spring-devtools-example

This is an project illustrating [Spring Boot Automatic Restarts](../../images/centos/README.md#spring-boot-automatic-restarts).


## Local without container

Run using:

mvn spring-boot:run

Open [http://localhost:8080/hello](http://localhost:8080/hello) - see "hello, xorld" ? Wrong.

Edit `src/main/java/io/okd/s2i/java/spring/example/HelloServlet.java` to change "hello, xorld" to "hello, world".
Use an IDE like Eclipse, with incremental rebuild of the .class file (won't work with a raw text editor).

Spring Boot devtools will automatically restart and (on reloading /hello) show "hello, world" - good.


## Local in container by S2I

For local building, install s2i either from source https://github.com/openshift/source-to-image/releases/ or e.g. via:

sudo dnf install source-to-image

Now to locally build a container using OpenShift Source-To-Image (S2I) use:

s2i build --copy . fabric8/s2i-java s2i-java-spring-devtools-example

Now run it like this:

docker run -p 8080:8080 s2i-java-spring-devtools-example

and see "hello, xorld" when accessing [http://localhost:8080/hello](http://localhost:8080/hello) - it works, but oups, it's wrong.

Edit `src/main/java/io/okd/s2i/java/spring/example/HelloServlet.java` to change "hello, xorld" to "hello, world".
Use an IDE like Eclipse, with incremental rebuild of the .class file (won't work with a raw text editor).
Now copy the fixed class file into the running container:

docker cp src/main/java/io/okd/s2i/java/spring/example/HelloServlet.java $(docker ps --filter ancestor=s2i-java-spring-devtools-example --format "{{.ID}}"):/TODO

_TODO NOK; cp .class or .java? What is the right path to copy it into? /deployments/ has .jar not .class? /tmp/src(/target) isn't what we want?? Check:_

docker exec -it $(docker ps --filter ancestor=s2i-java-spring-devtools-example --format "{{.ID}}") bash

**Spring Boot devtools will automatically restart, in container, and (on reloading /hello) show "hello, world" - great!**



## In OpenShift via MiniShift

_[oc rsync](https://docs.okd.io/latest/dev_guide/copy_files_to_container.html)_

**TODO**
58 changes: 58 additions & 0 deletions java/examples/spring-devtools/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>

<groupId>io.okd.s2i.java.spring</groupId>
<artifactId>example</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>example</name>
<description>Example project illustrating Spring Boot Devtools on OpenShift with S2I</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath />
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- WARNING: Do not use devtools in production!!! This is an example, IRL you would only do this in Maven using a custom profile. -->
<excludeDevtools>false</excludeDevtools>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.okd.s2i.java.spring.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@SpringBootApplication
@ServletComponentScan
public class ExampleApplication {

public static void main(String[] args) {
SpringApplication.run(ExampleApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.okd.s2i.java.spring.example;

import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/hello")
@SuppressWarnings("serial")
public class HelloServlet extends HttpServlet {

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.getOutputStream().print("hello, xorld");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.okd.s2i.java.spring.example;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
public class ExampleApplicationTests {

@Autowired private TestRestTemplate restTemplate;

@Test
public void contextLoads() {
}

@Test
public void testHelloXorld() {
ResponseEntity<String> responseEntity = restTemplate.getForEntity("/hello", String.class);

assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
assertEquals("hello, xorld", responseEntity.getBody());
}
}
12 changes: 7 additions & 5 deletions java/templates/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ The following environment variables can be used to influence the behaviour of th

Application arguments can be provided by setting the variable **JAVA_ARGS** to the corresponding value.

## Spring Boot Automatic Restarts
## Spring Boot Automatic Restarts

This image also supports detecting jars with [Spring Boot devtools](https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-devtools) included, which allows automatic restarts when files on the classpath are updated. Files can be easily updated in OpenShift using command [`oc rsync`](https://docs.openshift.org/latest/dev_guide/copy_files_to_container.html).
This image also supports detecting jars with [Spring Boot devtools](https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-devtools) included, which allows automatic restarts when files on the classpath are updated. Files can be easily updated in OpenShift using command [`oc rsync`](https://docs.openshift.org/latest/dev_guide/copy_files_to_container.html).

To enable automatic restarts, three things are required:
A [ready made example show case project](../../examples/spring-devtools) is here.

To enable automatic restarts, three things are required:

1. Add Spring Boot devtools dependency:

Expand Down Expand Up @@ -57,7 +59,7 @@ To enable automatic restarts, three things are required:
</build>
```

3. Set environment variables `JAVA_DEBUG=true` or `DEBUG=true` and optionally `JAVA_DEBUG_PORT=<port-number>` or `DEBUG_PORT=<port-number>`, which defaults to 5005. Since the `DEBUG` variable clashes with Spring Boot's recognition of the same variable to enable Spring Boot debug logging, use `SPRINGBOOT_DEBUG` instead.
3. Set environment variables `JAVA_DEBUG=true` or `DEBUG=true` and optionally `JAVA_DEBUG_PORT=<port-number>` or `DEBUG_PORT=<port-number>`, which defaults to 5005. Since the `DEBUG` variable clashes with Spring Boot's recognition of the same variable to enable Spring Boot debug logging, use `SPRINGBOOT_DEBUG` instead.

WARNING: Do not use devtools in production!!! This can be accomplished in Maven using a custom profile.
WARNING: Do not use devtools in production!!! This can be accomplished in Maven using a custom profile.

2 changes: 1 addition & 1 deletion java/templates/s2i/assemble
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ else
fi

# check if this is a fat jar executable archive. SpringBoot fat jars can be exploded
echo "Checking for fat jar archive..."
echo "Checking for 'fat JAR' archive..."
old_dir=`pwd`
cd "${DEPLOYMENTS_DIR}"
nr_jars=`ls *.jar 2>/dev/null | grep -v '^original-' | wc -l | tr -d '[[:space:]]'`
Expand Down
12 changes: 11 additions & 1 deletion test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function test_container() {

# ----------------------------------------------------------------------------------
# Maven
# --------------------------------------------------------------------------------

s2i build --copy java/examples/maven fabric8/s2i-java fabric8/s2i-java-maven-example

Expand All @@ -45,7 +46,6 @@ test_container "s2i-java-maven-example"
# Gradle
# --------------------------------------------------------------------------------


s2i build --copy java/examples/gradle fabric8/s2i-java fabric8/s2i-java-gradle-example

s2i build --copy java/examples/gradle fabric8/s2i-java fabric8/s2i-java-gradle-example --incremental
Expand All @@ -65,6 +65,16 @@ rm java/examples/binary/deployments/*

test_container "s2i-java-binary-example"


# ----------------------------------------------------------------------------------
# Spring Boot Developer Tools
# ----------------------------------------------------------------------------------

s2i build --copy java/examples/spring-devtools fabric8/s2i-java fabric8/s2i-java-spring-devtools-example

test_container "s2i-java-spring-devtools-example"

Choose a reason for hiding this comment

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

You must now check that the build log contains a certain string that the fat JAR has been exploded



# ----------------------------------------------------------------------------------
# Maven Wrapper
# ----------------------------------------------------------------------------------
Expand Down