
Spring 공식 문서에서 gradle의 dependencies에 대해 찾아보았다.
Declare dependencies
The simple Hello World sample is completely self-contained and does not depend on any additional libraries. Most applications, however, depend on external libraries to handle common and/or complex functionality.
간단한 Hello World 샘플은 완전히 독립적이며 추가 라이브러리에 의존하지 않습니다. 그러나 대부분의 애플리케이션은 일반 기능 및/또는 복잡한 기능을 처리하기 위해 외부 라이브러리에 의존합니다.
For example, suppose that in addition to saying "Hello World!", you want the application to print the current date and time. You could use the date and time facilities in the native Java libraries, but you can make things more interesting by using the Joda Time libraries.
예를 들어, "Hello World!"라고 말하는 것 외에도 응용 프로그램이 현재 날짜와 시간을 인쇄하기를 원한다고 가정합니다. 기본 Java 라이브러리의 날짜 및 시간 기능을 사용할 수 있지만 Joda Time 라이브러리를 사용하면 작업을 더욱 흥미롭게 만들 수 있습니다.
First, change HelloWorld.java to look like this:
먼저 HelloWorld.java를 다음과 같이 변경합니다.
package hello;
import org.joda.time.LocalTime;
public class HelloWorld {
public static void main(String[] args) {
LocalTime currentTime = new LocalTime();
System.out.println("The current local time is: " + currentTime);
Greeter greeter = new Greeter();
System.out.println(greeter.sayHello());
}
}
Here HelloWorld uses Joda Time’s LocalTime class to get and print the current time.
여기서 HelloWorld는 Joda Time의 LocalTime 클래스를 사용하여 현재 시간을 가져오고 인쇄합니다.
If you ran gradle build to build the project now, the build would fail because you have not declared Joda Time as a compile dependency in the build.
지금 프로젝트를 빌드하기 위해 gradle build를 실행했다면 빌드에서 Joda Time을 컴파일 종속성으로 선언하지 않았기 때문에 빌드가 실패할 것입니다.
For starters, you need to add a source for 3rd party libraries.
우선, 타사 라이브러리용 소스를 추가해야 합니다.
repositories {
mavenCentral()
}
The repositories block indicates that the build should resolve its dependencies from the Maven Central repository. Gradle leans heavily on many conventions and facilities established by the Maven build tool, including the option of using Maven Central as a source of library dependencies.
repositories 블록은 빌드가 Maven Central 저장소의 종속성을 해결해야 함을 나타냅니다. Gradle은 Maven Central을 라이브러리 종속성의 소스로 사용하는 옵션을 포함하여 Maven 빌드 도구에서 설정한 많은 규칙과 기능에 크게 의존합니다.
Now that we’re ready for 3rd party libraries, let’s declare some.
이제 타사 라이브러리를 사용할 준비가 되었으므로 일부 라이브러리를 선언해 보겠습니다.
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
implementation "joda-time:joda-time:2.2"
testImplementation "junit:junit:4.12"
}
With the dependencies block, you declare a single dependency for Joda Time. Specifically, you’re asking for (reading right to left) version 2.2 of the joda-time library, in the joda-time group.
종속성 블록을 사용하여 Joda Time에 대한 단일 종속성을 선언합니다. 특히 joda-time 그룹에서 joda-time 라이브러리 버전 2.2(오른쪽에서 왼쪽으로 읽기)를 요청하고 계십니다.
Another thing to note about this dependency is that it is a compile dependency, indicating that it should be available during compile-time (and if you were building a WAR file, included in the /WEB-INF/libs folder of the WAR). Other notable types of dependencies include:
이 종속성에 대해 주목해야 할 또 다른 점은 이것이 컴파일 종속성이라는 것입니다. 즉, 컴파일 시간 동안 사용할 수 있어야 함을 나타냅니다(WAR 파일을 빌드하는 경우 WAR의 /WEB-INF/libs 폴더에 포함됨). 다른 주목할만한 종속성 유형은 다음과 같습니다.
implementation. Required dependencies for compiling the project code, but that will be provided at runtime by a container running the code (for example, the Java Servlet API).
구현. 프로젝트 코드를 컴파일하는 데 필요한 종속성은 코드를 실행하는 컨테이너(예: Java Servlet API)에 의해 런타임 시 제공됩니다.
testImplementation. Dependencies used for compiling and running tests, but not required for building or running the project’s runtime code.
테스트 구현. 테스트를 컴파일하고 실행하는 데 사용되지만 프로젝트의 런타임 코드를 빌드하거나 실행하는 데는 필요하지 않은 종속성입니다.
Finally, let’s specify the name for our JAR artifact.
마지막으로 JAR 아티팩트의 이름을 지정해 보겠습니다.
jar {
archiveBaseName = 'gs-gradle'
archiveVersion = '0.1.0'
}
The jar block specifies how the JAR file will be named. In this case, it will render gs-gradle-0.1.0.jar.
jar 블록은 JAR 파일 이름 지정 방법을 지정합니다. 이 경우 gs-gradle-0.1.0.jar을 렌더링합니다.
Now if you run gradle build, Gradle should resolve the Joda Time dependency from the Maven Central repository and the build will succeed.
이제 Gradle 빌드를 실행하면 Gradle은 Maven Central 저장소에서 Joda Time 종속성을 해결해야 하며 빌드가 성공합니다.
implementation
: 컴파일 타임과 런타임 모두에 걸쳐서 사용자에게 의존성을 부여하는 지시어이다.
내부적으로만 사용되고 사용자에게는 의존성을 노출시키지 않게 선언한다.
의존 라이브러리를 수정해도 직접적으로 사용하는 모듈까지만 재빌드한다.
해당 모듈까지만 재빌드
compile
: 모든 모듈을 재빌드(D)
deprecated 되었다.
compileOnly
: lombok과 같이 컴파일시에만 필요한 의존성을 명시할 수 있다.
컴파일 할 때만 사용한다. 런타임에는 필요 없다.
runtimeOnly
: 런타임에만 필요하다.
DB 연동할 때 필요하다.
annotationProcessor
: 어노테이션으로 일하는 라이브러리.
testImplementation
: Test 할 때만 적용되며, 기본 작동 방식은 위와 동일하다.
테스트 코드 수행할 때만 작동한다.
api
: 해당 모듈이랑 관계있는 모듈 재빌드
곧 deprecated 될 것이다.
참고자료
https://spring.io/guides/gs/gradle
https://bnzn2426.tistory.com/136