LANGUAGE/SPRING 2016. 2. 3. 22:00

///// Spring Boot (스프링 부트)

스프링 부트는 기존 스프링의 xml설정을 간단하게 설정할 수 있게 해주는 쉽고 간결한 툴이란다.

하지만, 그냥 스프링도 그랬지만 스프링부트 이녀석도 깐깐한 녀석이다.

잘 알고 쓰면 편리한 툴이겠지만, 뭐 하나 수틀리면 골칫덩이가 되어버린다.


여기 있는 방식은 완벽하고 간결하진 않겠지만, 여러 경험을 통해 얻어낸 실행이되는 것을 확인한 결과물이다.




///// 환경

IDE: IntelliJ

Build: Gradle 2.1

Java: 1.7

Groovy: 2.3.11




///// Executable JAR Build (톰캣이 내장된 실행가능한 자르 빌드하기)

1. Setting build.gradle (그래들 설정)

group 'com.hellosj'
version '1.0-SNAPSHOT'

buildscript {
repositories {
maven { url "http://repo.spring.io/libs-snapshot" }
maven { url "http://repo.spring.io/release" }
maven { url "http://repo.spring.io/snapshot" }
maven { url "http://repo.spring.io/milestone" }
mavenLocal()
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:1.3.1.RELEASE"
classpath 'org.springframework:springloaded:1.2.3.RELEASE'
}
}

apply plugin: 'groovy'
apply plugin: 'maven'
apply plugin: 'idea'
apply plugin: 'spring-boot'

sourceCompatibility = 1.7
version=''

jar {
manifest {
attributes 'Title': 'Reqeust And Header Tester', 'Version': 1.0, 'Main-Class': 'hello.Application'
}
baseName 'myapp'
// archiveName 'reqTester.jar'
dependsOn configurations.runtime
from {
configurations.compile.collect {it.isDirectory()? it: zipTree(it)}
}
}


repositories {
mavenCentral()
maven { url "http://repo.spring.io/libs-snapshot" }
maven { url "http://repo.spring.io/release" }
maven { url "http://repo.spring.io/snapshot" }
maven { url "http://repo.spring.io/milestone" }
}

dependencies {
compile 'org.codehaus.groovy:groovy-all:2.3.11'
compile("org.springframework:spring-web:4.2.4.RELEASE")
compile("org.springframework:spring-core:4.2.4.RELEASE")
compile("org.springframework:spring-beans:4.2.4.RELEASE")
compile("org.springframework:spring-tx:4.2.4.RELEASE")
compile("org.springframework:spring-context:4.2.4.RELEASE")
compile("org.springframework.boot:spring-boot-starter-web:1.3.1.RELEASE")

// JSP
// compile "javax.servlet:jstl:1.2"
// providedRuntime 'org.apache.tomcat.embed:tomcat-embed-jasper:7.0.42'

// providedRuntime "org.springframework.boot:spring-boot-starter-tomcat:1.2.1.RELEASE"
testCompile("org.springframework.boot:spring-boot-starter-test:1.3.1.RELEASE")
}


2. Build with gradle (그래들로 빌드하기)

gradle clean jar


3. Run JAR (자르 실행)

- Run JAR with 8080 port (8080 port로 실행)

java -jar build/libs/myapp.war


- Run JAR with other port (다른 포트로 변경해서 실행)

java -jar build/libs/myapp.war --server.port=8888






///// Executable WAR Build (톰캣이 내장된 실행가능한 와르 빌드하기)

1. Setting build.gradle (그래들 설정)

group 'com.hellosj'
version '1.0-SNAPSHOT'

buildscript {
repositories {
maven { url "http://repo.spring.io/libs-snapshot" }
maven { url "http://repo.spring.io/release" }
maven { url "http://repo.spring.io/snapshot" }
maven { url "http://repo.spring.io/milestone" }
mavenLocal()
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:1.3.1.RELEASE"
classpath 'org.springframework:springloaded:1.2.3.RELEASE'
}
}

apply plugin: 'groovy'
apply plugin: 'maven'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'war'



sourceCompatibility = 1.7
version=''

war{
baseName = 'myapp'
}
configurations {
providedRuntime
}



repositories {
mavenCentral()
maven{ url "http://repo.spring.io/libs-snapshot" }
maven { url "http://repo.spring.io/release" }
maven { url "http://repo.spring.io/snapshot" }
maven { url "http://repo.spring.io/milestone" }
}

dependencies {
compile 'org.codehaus.groovy:groovy-all:2.3.11'
compile("org.springframework:spring-web:4.2.4.RELEASE")
compile("org.springframework:spring-core:4.2.4.RELEASE")
compile("org.springframework:spring-beans:4.2.4.RELEASE")
compile("org.springframework:spring-tx:4.2.4.RELEASE")
compile("org.springframework:spring-context:4.2.4.RELEASE")
compile("org.springframework.boot:spring-boot-starter-web:1.3.1.RELEASE")
// JSP
// compile "javax.servlet:jstl:1.2"
// providedRuntime 'org.apache.tomcat.embed:tomcat-embed-jasper:7.0.42'

providedRuntime "org.springframework.boot:spring-boot-starter-tomcat:1.2.1.RELEASE"
testCompile("org.springframework.boot:spring-boot-starter-test:1.3.1.RELEASE")
}



2. Setting Spring Boot (스프링부트 설정)

@SpringBootApplication
class Application extends SpringBootServletInitializer{

// Used when launching as an executable jar or war
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
System.out.println("Spring Boot Started.");
}

// Used when deploying to a standalone servlet container
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}

}


@Configuration
@EnableWebMvc
class WebMvcConfig extends WebMvcConfigurerAdapter {

@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}

@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/");
resolver.setSuffix(".html");
return resolver;
}

}


@Controller
@RequestMapping("/")
class Controller {

@RequestMapping("/")
public String index() {
return "index";
}

}


2. Build with gradle (그래들로 빌드하기)

gradle clean -Pprod bootRepackage



3. Run WAR (와르 실행)

- Run WAR with 8080 port (8080 port로 실행)

java -jar build/libs/myapp.war


- Run WAR with other port (다른 포트로 변경해서 실행)

java -jar build/libs/myapp.war --server.port=8888






///// 참고

[springboot] bootrepackage 처리된 war 내에서 template file 을 찾지 못할 때:

http://java.ihoney.pe.kr/360


Gradle, War, tomcat and manifest:

http://stackoverflow.com/questions/28879370/gradle-war-tomcat-and-manifest


Spring Boot: Fast MVC start

http://www.javacodegeeks.com/2014/06/spring-boot-fast-mvc-start.html


spring-boot gradle plugin can't be found

http://stackoverflow.com/questions/26577805/spring-boot-gradle-plugin-cant-be-found