buildscript 블록에서 "classpath"를 설정해서 해당 클래스 경로에 있는 클래스를 사용 할 수 있다. 내가 만들려고 하는 "spring boot"는 gradle에 기본으로 import 된 클래스가 아니기 때문에 여기에 등록해줘야 한다. plugins을 이용해서 Gradle의 모델을 확장 할 수 있다. ext는 전역변수를 설정하기 위해서 사용한다. 2019년 12월 현재 srping boot 의 최신버전은 2.2.2다.
Gradle은 Plugin을 이용해서 기능을 확장한다. 자바코드를 컴파일하는 등의 일은 모두 플러그인으로 수행한다.
테스트 코드
디렉토리를 만들고 Application.java와 HelloController.java 코드를 개발한다.
$ mkdir src/main/java/hello
$ cd src/main/java/hello
Application.java
package hello;
import java.util.Arrays;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Application.class, args);
System.out.println("Let's inspect the beans provided by Spring Boot:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}
}
}
HelloController.java
package hello;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
public class HelloController {
@RequestMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
}
Build
gradle
gradle로 빌드하고 애플리케이션을 실행해보자.
# ./gradlew build
Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.0.1/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 996ms
2 actionable tasks: 2 up-to-date
# java -jar build/libs/gs-spring-boot-0.1.0.jar
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.2.RELEASE)
2019-12-25 01:53:13.009 INFO 28447 --- [ main] hello.Application : Starting Application on yundream with PID 28447 (/home/yundream/workspace/java/myProject/build/libs/gs-spring-boot-0.1.0.jar started by yundream in /home/yundream/workspace/java/myProject)
2019-12-25 01:53:13.012 INFO 28447 --- [ main] hello.Application : No active profile set, falling back to default profiles: default
......
package hello;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
public class HelloController {
@RequestMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
@RequestMapping("/hello")
public String hello() {
return "Hello World";
}
}
용어들
pom.xml
Maven은 자바 프로젝트의 빌드를 도와주는 빌드 툴이다. Maven을 사용해서 C#, Ruby, Scala 기타 언어로 작성된 프로젝트를 빌드하고 관리 할 수 있다. Maven은 빌드를 위해서 두 개의 파일을 사용한다.
settings.xml
pom.xml
pom.xml은 프로젝트의 최상위 디렉토리에 만들어진다. pom은 Project Object Model로 프로젝트의 빌드 정보를 설정하는 파일이다. 다른 이름으로 설정 할 수 있기는 한데 관습적으로 pom.xml을 사용한다.
pom.xml의 형식은 아래와 같다.
<project>
<!-- model version is always 4.0.0 for Maven 2.x POMs -->
<modelVersion>4.0.0</modelVersion>
<!-- project coordinates, i.e. a group of values which uniquely identify this project -->
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0</version>
<!-- library dependencies -->
<dependencies>
<dependency>
<!-- coordinates of the required library -->
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<!-- this dependency is only used for running and compiling tests -->
<scope>test</scope>
</dependency>
</dependencies>
</project>
정리
Java는 해 본적이 없다.
어쩌다가 spring boot를 하게 됐는데, "Java가 아닌 spring boot를 사용한다"라는 느낌이 될 거 같다. "python은 모르지만 Django는 사용한다. Ruby는 잘 모르겠는데 Rails는 합니다" 이런 느낌.
Spring boot는 왜 하느냐 하면, 많이들 사용하니까. 그리고 간단? 하다고 하니까.
GoLang 사용하다가 Spring boot로 개발 하면서 느끼는 점. 아 GoLang이 간단하긴 하구나. Java는 개발 환경 세팅 부터 왜 이렇게 복잡한가 ? 나중에 더 구조화된(거대한) 프로그램을 만들게 되면 Java와 Spring boot의 장점을 찾을 수 있겠지 ?
Contents
Spring Boot 개발환경 구성
개발 환경
gradle 설치
maven 설치
Spring Boot 프로젝트
build.gradle 설정
buildscript { repositories { ¦ mavenCentral() } dependencies { ¦ classpath("org.springframework.boot:spring-boot-gradle-plugin:2.2.2.RELEASE") } } apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'idea' apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' bootJar { baseName = 'gs-spring-boot' version = '0.1.0' } repositories { mavenCentral() } sourceCompatibility = 1.8 targetCompatibility = 1.8 dependencies { compile("org.springframework.boot:spring-boot-starter-web") testCompile("junit:junit") }테스트 코드
package hello; import java.util.Arrays; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; @SpringBootApplication public class Application { public static void main(String[] args) { ApplicationContext ctx = SpringApplication.run(Application.class, args); System.out.println("Let's inspect the beans provided by Spring Boot:"); String[] beanNames = ctx.getBeanDefinitionNames(); Arrays.sort(beanNames); for (String beanName : beanNames) { System.out.println(beanName); } } }package hello; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RequestMapping; @RestController public class HelloController { @RequestMapping("/") public String index() { return "Greetings from Spring Boot!"; } }Build
gradle
marven
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.springframework</groupId> <artifactId>gs-spring-boot</artifactId> <version>0.1.0</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <properties> <java.version>1.8</java.version> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>코드 수정
package hello; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RequestMapping; @RestController public class HelloController { @RequestMapping("/") public String index() { return "Greetings from Spring Boot!"; } @RequestMapping("/hello") public String hello() { return "Hello World"; } }용어들
pom.xml
- settings.xml
- pom.xml
pom.xml은 프로젝트의 최상위 디렉토리에 만들어진다. pom은 Project Object Model로 프로젝트의 빌드 정보를 설정하는 파일이다. 다른 이름으로 설정 할 수 있기는 한데 관습적으로 pom.xml을 사용한다. pom.xml의 형식은 아래와 같다.<project> <!-- model version is always 4.0.0 for Maven 2.x POMs --> <modelVersion>4.0.0</modelVersion> <!-- project coordinates, i.e. a group of values which uniquely identify this project --> <groupId>com.mycompany.app</groupId> <artifactId>my-app</artifactId> <version>1.0</version> <!-- library dependencies --> <dependencies> <dependency> <!-- coordinates of the required library --> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <!-- this dependency is only used for running and compiling tests --> <scope>test</scope> </dependency> </dependencies> </project>정리
Recent Posts
Archive Posts
Tags