From 94f50c2175239b1db859274ed3832faae9daeb3b Mon Sep 17 00:00:00 2001 From: aiet Date: Wed, 23 May 2018 16:46:55 +0800 Subject: [PATCH 1/2] BAEL-1779 deploy springboot app to azure --- azure/.gitignore | 25 ++++ azure/README.md | 3 + azure/docker/Dockerfile | 6 + azure/pom.xml | 140 ++++++++++++++++++ .../springboot/azure/AzureApplication.java | 19 +++ .../springboot/azure/TestController.java | 34 +++++ .../com/baeldung/springboot/azure/User.java | 43 ++++++ .../springboot/azure/UserRepository.java | 9 ++ .../src/main/resources/application.properties | 16 ++ .../azure/AzureApplicationTests.java | 16 ++ 10 files changed, 311 insertions(+) create mode 100644 azure/.gitignore create mode 100644 azure/README.md create mode 100644 azure/docker/Dockerfile create mode 100644 azure/pom.xml create mode 100644 azure/src/main/java/com/baeldung/springboot/azure/AzureApplication.java create mode 100644 azure/src/main/java/com/baeldung/springboot/azure/TestController.java create mode 100644 azure/src/main/java/com/baeldung/springboot/azure/User.java create mode 100644 azure/src/main/java/com/baeldung/springboot/azure/UserRepository.java create mode 100644 azure/src/main/resources/application.properties create mode 100644 azure/src/test/java/com/baeldung/springboot/azure/AzureApplicationTests.java diff --git a/azure/.gitignore b/azure/.gitignore new file mode 100644 index 0000000000..82eca336e3 --- /dev/null +++ b/azure/.gitignore @@ -0,0 +1,25 @@ +/target/ +!.mvn/wrapper/maven-wrapper.jar + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/build/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ \ No newline at end of file diff --git a/azure/README.md b/azure/README.md new file mode 100644 index 0000000000..d51172c186 --- /dev/null +++ b/azure/README.md @@ -0,0 +1,3 @@ +### Relevant Articles: + +- [Deploy Spring Boot App to Azure]() diff --git a/azure/docker/Dockerfile b/azure/docker/Dockerfile new file mode 100644 index 0000000000..f865518330 --- /dev/null +++ b/azure/docker/Dockerfile @@ -0,0 +1,6 @@ +FROM frolvlad/alpine-oraclejdk8:slim +VOLUME /tmp +ADD azure-0.1.jar app.jar +RUN sh -c 'touch /app.jar' +EXPOSE 8080 +ENTRYPOINT [ "sh", "-c", "java -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ] diff --git a/azure/pom.xml b/azure/pom.xml new file mode 100644 index 0000000000..a5854a84f9 --- /dev/null +++ b/azure/pom.xml @@ -0,0 +1,140 @@ + + + 4.0.0 + + com.baeldung.springboot + azure + 0.1 + war + azure + Demo project for Spring Boot on Azure + + + org.springframework.boot + spring-boot-starter-parent + 2.0.2.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + + aietdocker + ${azure.containerRegistry}.azurecr.io + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-tomcat + provided + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + + com.h2database + h2 + + + + mysql + mysql-connector-java + 5.1.6 + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + com.spotify + docker-maven-plugin + 1.0.0 + + ${docker.image.prefix}/${project.artifactId} + https://${docker.image.prefix} + ${azure.containerRegistry} + docker + + + / + ${project.build.directory} + ${project.build.finalName}.jar + + + + + + com.microsoft.azure + azure-webapp-maven-plugin + 1.1.0 + + + azure-auth + + baeldung-group + baeldung-webapp + baeldung-plan + 1.8 + + + + + + + + + + spring.datasource.url + jdbc:h2:file:~/test + + + + spring.datasource.username + sa + + + + spring.datasource.password + + + + + + + + + + + + + + + + + + + + + diff --git a/azure/src/main/java/com/baeldung/springboot/azure/AzureApplication.java b/azure/src/main/java/com/baeldung/springboot/azure/AzureApplication.java new file mode 100644 index 0000000000..5233f6bdf4 --- /dev/null +++ b/azure/src/main/java/com/baeldung/springboot/azure/AzureApplication.java @@ -0,0 +1,19 @@ +package com.baeldung.springboot.azure; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; + +@SpringBootApplication +public class AzureApplication extends SpringBootServletInitializer { + + @Override + protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { + return application.sources(AzureApplication.class); + } + + public static void main(String[] args) { + SpringApplication.run(AzureApplication.class, args); + } +} diff --git a/azure/src/main/java/com/baeldung/springboot/azure/TestController.java b/azure/src/main/java/com/baeldung/springboot/azure/TestController.java new file mode 100644 index 0000000000..9a18bf4d65 --- /dev/null +++ b/azure/src/main/java/com/baeldung/springboot/azure/TestController.java @@ -0,0 +1,34 @@ +package com.baeldung.springboot.azure; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import static com.baeldung.springboot.azure.User.userNamed; + +/** + * @author aiet + */ +@RestController +public class TestController { + + @GetMapping("/hello") + public String hello() { + return "hello azure!"; + } + + @Autowired private UserRepository userRepository; + + @PostMapping("/user") + public String register(@RequestParam String name) { + userRepository.save(userNamed(name)); + return "registered"; + } + + @GetMapping("/user") + public Iterable userlist() { + return userRepository.findAll(); + } +} diff --git a/azure/src/main/java/com/baeldung/springboot/azure/User.java b/azure/src/main/java/com/baeldung/springboot/azure/User.java new file mode 100644 index 0000000000..d7a25aa246 --- /dev/null +++ b/azure/src/main/java/com/baeldung/springboot/azure/User.java @@ -0,0 +1,43 @@ +package com.baeldung.springboot.azure; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +/** + * @author aiet + */ +@Entity +public class User { + + public User() { + } + + public static User userNamed(String name) { + User u = new User(); + u.setName(name); + return u; + } + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Integer id; + private String name; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/azure/src/main/java/com/baeldung/springboot/azure/UserRepository.java b/azure/src/main/java/com/baeldung/springboot/azure/UserRepository.java new file mode 100644 index 0000000000..74a23f3180 --- /dev/null +++ b/azure/src/main/java/com/baeldung/springboot/azure/UserRepository.java @@ -0,0 +1,9 @@ +package com.baeldung.springboot.azure; + +import org.springframework.data.repository.CrudRepository; + +/** + * @author aiet + */ +public interface UserRepository extends CrudRepository { +} diff --git a/azure/src/main/resources/application.properties b/azure/src/main/resources/application.properties new file mode 100644 index 0000000000..32ee873194 --- /dev/null +++ b/azure/src/main/resources/application.properties @@ -0,0 +1,16 @@ +server.port=8080 +server.address=0.0.0.0 +spring.jpa.hibernate.ddl-auto=create + +logging.file=azure.log +logging.level.root=info + +spring.datasource.url=jdbc:h2:file:~/test +spring.datasource.username=sa +spring.datasource.password= + +#spring.datasource.url=jdbc:mysql://localhost:3306/localdb +#spring.datasource.username=your-db-username +#spring.datasource.password=your-db-password + + diff --git a/azure/src/test/java/com/baeldung/springboot/azure/AzureApplicationTests.java b/azure/src/test/java/com/baeldung/springboot/azure/AzureApplicationTests.java new file mode 100644 index 0000000000..91632be11a --- /dev/null +++ b/azure/src/test/java/com/baeldung/springboot/azure/AzureApplicationTests.java @@ -0,0 +1,16 @@ +package com.baeldung.springboot.azure; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class AzureApplicationTests { + + @Test + public void contextLoads() { + } + +} From b4c7c5f89e1c5b8bb12005553f3bcdb86c2e9df6 Mon Sep 17 00:00:00 2001 From: aiet Date: Wed, 23 May 2018 17:26:31 +0800 Subject: [PATCH 2/2] update spotify docker-maven-plugin to latest version --- azure/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azure/pom.xml b/azure/pom.xml index a5854a84f9..419ebacd4a 100644 --- a/azure/pom.xml +++ b/azure/pom.xml @@ -70,7 +70,7 @@ com.spotify docker-maven-plugin - 1.0.0 + 1.1.0 ${docker.image.prefix}/${project.artifactId} https://${docker.image.prefix}