์ด์ ๊ธ์์ ์ดํด๋ณธ ์๋ฐ ํ๋ฌ๊ทธ์ธ์ ์ด์ด, ์ด๋ฒ ๊ธ์์๋ ์๋ฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ํ๋ฌ๊ทธ์ธ์ ๋ํด ์์๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
๐ง java-library ํ๋ฌ๊ทธ์ธ
java-library ํ๋ฌ๊ทธ์ธ์ java ํ๋ฌ๊ทธ์ธ์ ํ์ฅํ์ฌ ์ถ๊ฐ์ ์ธ ๊ธฐ๋ฅ์ ์ ๊ณตํฉ๋๋ค.
์ฆ ์๋ฐ ํ๋ฌ๊ทธ์ธ์ ํ์ฅํ๊ธฐ ๋๋ฌธ์, Java ํ๋ฌ๊ทธ์ธ์ ์ํด ์ ๊ณต๋๋ ๋ชจ๋ source set, task, configuration๋ฑ ์ญ์๋ ์ฌ์ฉํ ์ ์์ต๋๋ค.
์๋ฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ํ๋ฌ๊ทธ์ธ์ ์๋ฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์์ฑํ ๋ ์ฌ์ฉํ๋๋ฐ, Java ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ์ด๋ฅผ ์ฌ์ฉํ๋ ๋ค๋ฅธ ๋ชจ๋ ํน์ ํ๋ก์ ํธ์์ ์ฌ์ฉํ ์ ์๋ API๋ฅผ ์ ๊ณตํฉ๋๋ค.
์๋ฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ํ๋ฌ๊ทธ์ธ์ ๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉํ ์ ์์ต๋๋ค.
plugins {
id 'java-library'
}
๐ง api configuration
java-library ํ๋ฌ๊ทธ์ธ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์์ฑ์ ์ํด api๋ผ๋ configuration์ ์ ๊ณตํฉ๋๋ค.
(์๋ฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ํ๋ฌ๊ทธ์ธ์ ์๋ฐ ํ๋ฌ๊ทธ์ธ์ ์์ํ์๊ธฐ ๋๋ฌธ์, ์๋ฐ ํ๋ฌ๊ทธ์ธ์์ ์ ๊ณตํ๋ implementation ๋ฑ์ configuration๋ค ์ญ์ ๋ชจ๋ ์ ๊ณตํฉ๋๋ค.)
api configuration์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์ ๊ณตํ๋ API์์ (์ฌ์ฉ์์๊ฒ) ๋ ธ์ถ๋์ด์ง๋ ์์กด์ฑ์ ์ ์ธํ๋๋ฐ ์ฌ์ฉ๋ฉ๋๋ค.
๋ฐ๋ผ์ api๋ก ์ ์ธ๋ ์์กด์ฑ์ ๊ฐ์ง ๋ชจ๋์ ์ฌ์ฉํ๋ ์๋น์(๋ค๋ฅธ ๋ชจ๋, ํน์ ๋ค๋ฅธ ํ๋ก์ ํธ ๋ฑ)์์๋ ํด๋น ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ํด๋์ค ๋ฐ ๋ฉ์๋์ ์ ๊ทผํ ์ ์์ต๋๋ค.
์ด๋ฅผ ์ํด์, api๋ก ์ ์ธ๋ ์์กด์ฑ์ ์๋น์์ compile classpath์ runtime classpath ๋ชจ๋์ ํฌํจ๋ฉ๋๋ค.
๐ง api์ implementaion์ ์ฐจ์ด์
api๋ก ์ ์ธ๋ ์์กด์ฑ์ ์๋น์์ compile classpath์ runtime classpath ๋ชจ๋์ ํฌํจ๋๋ ๋ฐ๋ฉด,
implementation configuration์ ๋ด๋ถ์์ ์ฌ์ฉ๋๋ ์์กด์ฑ์ ์ ์ธํ๋๋ฐ ์ฌ์ฉ๋ฉ๋๋ค.
์ฆ implementation์ ์ฌ์ฉํ์ฌ ์ ์ธ๋ ์์กด์ฑ์ ํด๋น ๋ชจ๋ ๋ด๋ถ์์๋ง ์ฌ์ฉ๋๊ณ , ๋ค๋ฅธ ๋ชจ๋์ ๋ ธ์ถ๋์ง ์์ต๋๋ค.
์ด๋ก ์ธํด ๋ชจ๋ ๋ด๋ถ์์ implementation์ผ๋ก ์ ์ธ๋์ด ์ฌ์ฉ๋๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๋ณ๊ฒฝ์ฌํญ์ ๋ค๋ฅธ ๋ชจ๋์ ์ํฅ์ ๋ฏธ์น์ง ์์ต๋๋ค.
์ด๋ฅผ ์ํด์, Implemetation์ ์ฌ์ฉํ์ฌ ์ ์ธ๋ ์์กด์ฑ์ ์๋น์์ compile classpath์๋ ํฌํจ๋์ง ์์ต๋๋ค.
api๋ ์์กด์ฑ์ ๋ค๋ฅธ ๋ชจ๋์ ๋ ธ์ถํ๊ณ , implementation์ ์์กด์ฑ์ ๋ ธ์ถํ์ง ์์ต๋๋ค.
๐ฑ ์์
๋ฉํฐ ๋ชจ๋ ํ๋ก์ ํธ๋ฅผ ์์๋ก ์ดํด๋ณด๊ฒ ์ต๋๋ค.
์ฐ์ ์ ์ฒด ๊ตฌ์กฐ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
ํด๋น ํ๋ก์ ํธ์๋ module-api์ module-consumer๋ผ๋ ๋ ๋ชจ๋์ด ์กด์ฌํ๋ฉฐ, module-consumer์์ module-api๋ฅผ ์์กดํ๋ ๊ตฌ์กฐ์ ๋๋ค.
๐ root [settings.gradle]
rootProject.name = 'gradle-study'
include('module-api')
include('module-consumer')
๐ root [build.gradle]
allprojects {
group 'org.example'
version 'unspecified'
repositories {
mavenCentral()
}
}
๐ module-api [build.gradle]
plugins {
id 'java-library'
}
dependencies {
api 'org.apache.httpcomponents:httpclient:4.5.7'
implementation 'org.apache.commons:commons-lang3:3.5'
}
๐ module-consumer [build.gradle]
plugins {
id 'java'
}
group 'org.example'
version 'unspecified'
repositories {
mavenCentral()
}
dependencies {
implementation project(':module-api')
}
๋น๋ ๊ฒฐ๊ณผ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
api๋ก ์ ์ธํ ์์กด์ฑ์ธ 'org.apache.httpcomponents:httpclient:4.5.7'์ ๊ฒฝ์ฐ,
ํด๋น ๋ชจ๋์ ์ฌ์ฉํ๋ ๋ค๋ฅธ ๋ชจ๋(consumer)์ compileClasspath์ runtimeClasspath์ ๋ชจ๋ ํฌํจ๋ฉ๋๋ค.
implementation์ผ๋ก ์ ์ธํ ์์กด์ฑ์ธ 'org.apache.commons:commons-lang3:3.5'์ ๊ฒฝ์ฐ,
consumer์ compileClasspath์๋ ํฌํจ๋์ง ์๋ ๊ฒ์ ์ ์ ์์ต๋๋ค.
๐ง ์ธ์ api๋ฅผ ์ฐ๊ณ , ์ธ์ implemetation์ ์ธ๊น?
์ฐ์ ์ ์ธ ๊ท์น์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
๊ฐ๋ฅํ๋ค๋ฉด implementation์ ์จ๋ผ.
์ด๋ ๊ฒ ํ๋ฉด ํด๋น ๋ชจ๋์์ ์ฌ์ฉํ๋ ์์กด์ฑ๋ค์ด, ํด๋น ๋ชจ๋์ ์ฌ์ฉํ๋ ๋ค๋ฅธ ๋ชจ๋์ compileClasspath์ ํฌํจ๋์ง ์์ผ๋ฏ๋ก, ์ฌ๋ฌ ์ฅ์ ์ ๋ง์ด ์ป์ ์ ์์ต๋๋ค..
๊ทธ๋ ๋ค๋ฉด ์ธ์ api๋ฅผ ์ฌ์ฉํด์ผ ํ ๊น์?
๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ๊ตฌํํ๋ ๊ณต๊ฐ API์ ๋ ธ์ถ๋๋ ์์กด์ฑ์ ๋ํด์ ์ฌ์ฉํ์ฌ์ผ ํ๋๋ฐ, ๊ทธ ์์๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
์์๋ฐ์ ์ฌ์ฉํ๊ฑฐ๋ ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ๋ ๊ฒฝ์ฐ:
์ด๋ ์์๋ฐ์ ํด๋์ค๋ ๊ตฌํํ ์ธํฐํ์ด์ค์ ๊ณต๊ฐ API๋ฅผ ์ฌ์ฉํ๋ ๋ค๋ฅธ ๋ชจ๋์์ ํด๋น ํ์ ์ ์ฌ์ฉํ ์ ์๋๋ก ํ๊ธฐ ์ํจ์ ๋๋ค.
์ ๋คํฑ ํ์ ํ๋ผ๋ฏธํฐ๋ฅผ ํฌํจํ์ฌ, public ๋ฉ์๋์ ํ๋ผ๋ฏธํฐ ํน์ ๋ฐํ๊ฐ์ผ๋ก ์ฌ์ฉ๋๋ ๊ฒฝ์ฐ:
์ด๋ ๋ค๋ฅธ ๋ชจ๋์์ ํด๋น ๋ฉ์๋๋ฅผ ํธ์ถํ๊ฑฐ๋ ์ค๋ฒ๋ผ์ด๋ํ ๋ ํด๋น ํ์ ์ ์ฌ์ฉํ ์ ์๋๋ก ํ๊ธฐ ์ํจ์ ๋๋ค. ๋ํ, ์ ๋ค๋ฆญ ํ๋ผ๋ฏธํฐ ํ์ ์ api ์ข ์์ฑ์ผ๋ก ์ ์ํด์ผ ํ๋ ์ด์ ๋ ํด๋น ์ ๋ค๋ฆญ ํ์ ์ ์ค์ ํ์ ์ธ์๊ฐ ๋ ธ์ถ๋๊ธฐ ๋๋ฌธ์ ๋๋ค.
public ํ๋์์ ์ฌ์ฉ๋๋ ๊ฒฝ์ฐ:
์ด๋ ๋ค๋ฅธ ๋ชจ๋์์ ํด๋น ํ๋๋ฅผ ์ฌ์ฉํ ์ ์๋๋ก ํ๊ธฐ ์ํจ์ ๋๋ค.
pulbic ์ ๋ ธํ ์ด์ ์ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ:
์ด๋ ๋ค๋ฅธ ๋ชจ๋์์ ํด๋น ์ ๋ ธํ ์ด์ ์ ์ฌ์ฉํ ์ ์๋๋ก ํ๊ธฐ ์ํจ์ ๋๋ค.
์ด์ ๋์กฐ์ ์ผ๋ก, api๋ฅผ ์ฌ์ฉํ์ง ์๊ณ , implementation์ผ๋ก ๊ฐ๋ฅํ ๊ฒฝ์ฐ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
๋ฉ์๋ ๋ด๋ถ์์๋ง ์ฌ์ฉ๋๋ ๊ฒฝ์ฐ
private ๋ฉค๋ฒ๋ณ์๋ก๋ง ์ฌ์ฉ๋๋ ๊ฒฝ์ฐ
๋ด๋ถ(inner) ํด๋์ค์์๋ง ๋ฐ๊ฒฌ๋๋ ์ ํ (๋ด๋ถ ํด๋์ค๋ static nested class๋ฅผ ์๋ฏธํ๋ ๊ฒ์ด ์๋)
๐ฑ ์์
๋ค์ ์์์์, ๋ ๊ฐ์ third-party ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํฉ๋๋ค.
ํ๋๋ ํด๋์ค์ public API์ ๋ ธ์ถ๋๊ณ , ๋ค๋ฅธ ํ๋๋ ๋ด๋ถ์์๋ง ์ฌ์ฉ๋ฉ๋๋ค.
import๋ก๋ ์ด๋ ๊ฒ์ด ์ด๋๊ฒ์ธ์ง ์ ์ ์์ผ๋ฏ๋ก ํ๋์ ์์ฑ์, ๋ฉ์๋๋ค์ ์ดํด๋ณด์์ผ ํฉ๋๋ค.
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import java.io.ByteArrayOutputStream;
public class HttpClientWrapper {
private final HttpClient client; // private member๋ก ์ฌ์ฉ๋ฉ๋๋ค.
// HttpClient ๋ public ๋ฉ์๋์ ํ๋ผ๋ฏธํฐ๋ก ์ฌ์ฉ๋ฉ๋๋ค.
public HttpClientWrapper(HttpClient client) {
this.client = client;
}
public byte[] doRawGet(String url) {
HttpGet request = new HttpGet(url);
try {
HttpEntity entity = doGet(request);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
entity.writeTo(baos);
return baos.toByteArray();
} catch (Exception e) {
ExceptionUtils.rethrow(e); // ExceptionUtils๋ ๋ฉ์๋ ๋ด๋ถ์์๋ง ์ฌ์ฉ๋ฉ๋๋ค.
} finally {
request.releaseConnection();
}
return null;
}
// HttpGet ์ HttpEntity ๋ private ๋ฉ์๋์์๋ง ์ฌ์ฉ๋๋ฏ๋ก, API์ ์ํ์ง ์์ต๋๋ค.
private HttpEntity doGet(HttpGet get) throws Exception {
HttpResponse response = client.execute(get);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
System.err.println("Method failed: " + response.getStatusLine());
}
return response.getEntity();
}
}
์ ํด๋์ค์ public ์์ฑ์๋ ํ๋ผ๋ฏธํฐ๋ก HttpClient๋ฅผ ์ฌ์ฉํฉ๋๋ค.
๋ฐ๋ผ์ HttpClient๋ ํด๋น API๋ฅผ ์ฌ์ฉํ๋ ๋ค๋ฅธ ๋ชจ๋์๊ฒ ๋ ธ์ถ๋์ด์ง๋๋ค.
HttpGet๊ณผ HttpEntity๋ private ๋ฉ์๋์์๋ง ์ฌ์ฉ๋๋ฏ๋ก ์ด๋ค์ HttpClient๋ฅผ ๋ ธ์ถํ์ง๋ ์์ต๋๋ค.
์ ์์์์๋ ์ค์ง ์์ฑ์์ ์ฌ์ฉ๋ HttpClient์ ์ํ์ฌ httpclient์ ๋ํ dependency๋ฅผ api์ผ๋ก ์ค์ ํด์ผ ํฉ๋๋ค.
๋ฐ๋ฉด์ ExceptionUtils์ ์ค์ง ๋ฉ์๋ ๋ฐ๋์์๋ง ์ฌ์ฉ๋๊ธฐ ๋๋ฌธ์ ์ด๋ implementation ์ ์ฌ์ฉํด์ผ ํฉ๋๋ค.
dependencies {
api 'org.apache.httpcomponents:httpclient:4.5.7'
implementation 'org.apache.commons:commons-lang3:3.5'
}
์ ๋ชจ๋์ ๋ค๋ฅธ ๋ชจ๋์์ ์ฌ์ฉํ๋ฉด, classpath๋ ๋ค์๊ณผ ๊ฐ์ด ์ค์ ๋ฉ๋๋ค.
api๋ก ์์กด์ฑ์ ์ค์ ํ๋ค๋ฉด, ์ปดํ์ผ๊ณผ ๋ฐํ์ classpath ๋ชจ๋์ ํฌํจ๋๋๊ฒ์ ํ์ธํ ์ ์์ต๋๋ค.
๊ทธ๋ฌ๋ implementation์ ์ฌ์ฉํ๋ค๋ฉด ๋ฐํ์ classpath์๋ง ํฌํจ๋๋ ๊ฒ์ ์ ์ ์์ต๋๋ค.
๋ง์ฝ ์์์ ์ค์ ๋ api๋ฅผ implementation์ผ๋ก ๋ฐ๊พผ ๋ค, ๋ค๋ฅธ ๋ชจ๋์์ ์ฌ์ฉํ๋ค๋ฉด, ๋ค์๊ณผ ๊ฐ์ด ์ปดํ์ผ ์๋ฌ๊ฐ ๋ฐ์ํฉ๋๋ค.
๐ง Java Library plugin configuration
์๋ ๊ทธ๋ฆผ์ java-library์์ ์ ๊ณตํ๋ configuration ๋ค์ ๊ด๊ณ๋ฅผ ๋ํ๋ธ ๊ฒ์ ๋๋ค.
์ ๊ทธ๋ฆผ์ ํตํด ์ ์ ์๋ฏ์ด, compileClasspath, runtimeElements, runtimeClasspath์ implementation์ ์์ํ๊ธฐ ๋๋ฌธ์, implementation์ผ๋ก ์ ์ธ๋ ์์กด์ฑ์ compileClasspath, runtimeElements, runtimeClasspath์์๋ ๋ชจ๋ ํฌํจ๋ฉ๋๋ค.
๋ง์ฐฌ๊ฐ์ง๋ก apiElements์ implementation๋ api๋ฅผ ์์๋ฐ์๊ธฐ ๋๋ฌธ์, api์ ์ ์๋ ์์กด์ฑ๋ค์ implementation๊ณผ apiElements ๋ชจ๋์์ ์ฌ์ฉ ๊ฐ๋ฅํฉ๋๋ค.
๋ค์ ๊ทธ๋ฆผ์ ํ ์คํธ ๊ตฌ์ฑ ์ค์ ์ ๋ํ๋ ๋๋ค.
๐ Reference
https://docs.gradle.org/current/userguide/java_plugin.html#java_plugin
https://docs.gradle.org/current/userguide/java_library_plugin.html
'๐ Gradle' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Gradle] ์๋ฐ ํ๋ฌ๊ทธ์ธ (0) | 2023.04.15 |
---|---|
[Gradle] Dependency Configuration์ด๋? (0) | 2023.04.15 |
[Gradle] ์์กด์ฑ ๊ด๋ฆฌ(Dependency management)๋? (0) | 2023.04.15 |
[Gradle] Gradle Wrapper(gradlew์ gradle.bat)์ settings.gradle (0) | 2023.04.14 |
[Gradle] Gradle์ ๊ตฌ์ฑ์์(Project, Task, Plugin)์ ์์ํด๋ณด๊ธฐ (0) | 2023.04.14 |