Gradle이란??

hwany·2020년 2월 25일
5

Gradle

Gradle은 프로젝트를 위한 범용 빌드 도구

gradle wrapper

  1. 이미 존재하는 프로젝트를 새로운 환경에 설치할때 별도의 설치나 설정과정없이 곧 바로 빌드할 수 있게 하기 위함
    (Java나 Gradle도 설치할 필요가 없음. 또한 로컬에 설치된 Gradle 또는 Java의 버전도 신경쓸 필요가 없음. 따라서 항상 wrapper를 사용할 것을 권장)
  2. wrapper는 사용자가 Gradle이 설치되어 있지 않아도 Gradle tasks를 실행할 수 있도록 해주는 작은 script, jar 및 등록 정보 파일
  3. wrapper를 생성하면 사용자가 프로젝트를 만든 사람과 동일한 버전의 Gradle을 사용할 수 있음
  • gradle wrapper 실행
$ gradle wrapper

BUILD SUCCESSFUL in 602ms
1 actionable task: 1 executed

아래와 같이 파일이 만들어짐

.
├── build.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
└── gradlew.bat

2 directories, 5 files
  $ ./gradlew build

Wrapper를 사용하면 위와 같이 실행가능

  $ gradle build

이 경우 Java나 Gradle이 설치되어 있어야 하고, 새로받은 프로젝트의 Gradle 버전과 로컬에 설치된 Gradle 버전이 호환되지 않으면 문제가 발생할 수 있음

또, gradle명령어로 컴파일이나 빌드 등을 할때, 위와 같이 하면 로컬에 설치된 gradle을 사용

  • Build 후에는 test, compile 후 jar파일 생성

  • gradlew라는 UNIX script와 gradlew.bat라는 Windows명령이 jar 파일 및 속성 파일과 함께 생성됨

task 설정

  • Gradle에는 자신의 프로젝트에서 구성할 수 있는 task 라이브러리가 함께 제공됨

  • build.gradle에 apply plugin:'java' 을 추가 하면 사용할 수 있는 task가 추가 됨

    $ ./gradlew build && ./gradlew tasks --all
    > Task :tasks
    
    ------------------------------------------------------------
    Tasks runnable from root project
    ------------------------------------------------------------
    
    Build tasks
    -----------
    assemble - Assembles the outputs of this project.
    build - Assembles and tests this project.
    buildDependents - Assembles and tests this project and all projects that depend on it.
    buildNeeded - Assembles and tests this project and all projects it depends on.
    classes - Assembles main classes.
    clean - Deletes the build directory.
    jar - Assembles a jar archive containing the main classes.
    testClasses - Assembles test classes.
    
    Build Setup tasks
    -----------------
    init - Initializes a new Gradle build.
    wrapper - Generates Gradle wrapper files.
    
    Documentation tasks
    -------------------
    javadoc - Generates Javadoc API documentation for the main source code.
    
    Help tasks
    ----------
    buildEnvironment - Displays all buildscript dependencies declared in root project 'test'.
    components - Displays the components produced by root project 'test'. [incubating]
    dependencies - Displays all dependencies declared in root project 'test'.
    dependencyInsight - Displays the insight into a specific dependency in root project 'test'.
    dependentComponents - Displays the dependent components of components in root project 'test'. [incubating]
    help - Displays a help message.
    model - Displays the configuration model of root project 'test'. [incubating]
    outgoingVariants - Displays the outgoing variants of root project 'test'.
    projects - Displays the sub-projects of root project 'test'.
    properties - Displays the properties of root project 'test'.
    tasks - Displays the tasks runnable from root project 'test'.
    
    Verification tasks
    ------------------
    check - Runs all checks.
    test - Runs the unit tests.
    
    Other tasks
    -----------
    compileJava - Compiles main Java source.
    compileTestJava - Compiles test Java source.
    prepareKotlinBuildScriptModel
    processResources - Processes main resources.
    processTestResources - Processes test resources.
    
    Rules
    -----
    Pattern: clean<TaskName>: Cleans the output files of a task.
    Pattern: build<ConfigurationName>: Assembles the artifacts of a configuration.
    Pattern: upload<ConfigurationName>: Assembles and uploads the artifacts belonging to a configuration.
    
    BUILD SUCCESSFUL in 538ms
    1 actionable task: 1 executed

gradle.bat

  • 윈도우용 실행 배치 스크립트
  • 윈도우에서 실행가능하다는 점만 제외하면 gradlew와 동일

gradle/wrapper/gradle-wrapper.jar

  • gradlew나 gradlew.bat 파일이 프로젝트 내에 설치하는 이 파일을 사용하여 gradle task를 실행하기 때문에 로컬 환경의 영향을 받지 않음
    (실제로는 Wrapper 버전에 맞는 구성들을 로컬 캐시에 다운로드 받음)

gradle/wrapper/gradle-wrapper.properties

  • gradle wrapper 설정 파일

  • 이 파일의 wrapper 버전 등을 변경하면 task 실행 시, 자동으로 wrapper 파일을 로컬 캐시에 다운로드 함

    distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip
    distributionBase=GRADLE_USER_HOME
    distributionPath=wrapper/dists
    zipStorePath=wrapper/dists
    zipStoreBase=GRADLE_USER_HOME

build.gradle

  • 기본적인 빌드 파일의 이름

  • 의존성이나 플러그인 설정 등을 위한 스크립트

    repositories {
      mavenCentral()
      jcenter()
    }

    필요한 dependencis도 추가(gradle프로젝트를 springboot프로젝트로 바꾼 예)

    dependencies { 
        compile('org.springframework.boot:spring-boot-starter-web')     
        compile('org.projectlombok:lombok') 
        compile('org.springframework.boot:spring-boot-starter-data-jpa')
        compile('org.springframework.boot:spring-boot-starter-mustache')
        compile('com.h2database:h2') 
    
        compile('org.springframework.boot:spring-boot-starter-oauth2-client')
        compile('org.springframework.session:spring-session-jdbc')  
        compile('org.mariadb.jdbc:mariadb-java-client')
    
        testCompile('org.springframework.boot:spring-boot-starter-test')
        testCompile('org.springframework.security:spring-security-test')
    }

settings.gradle

  • 프로젝트의 구성 정보를 기록하는 파일
  • 어떤 하위 프로젝트들이 어떤 관계로 구성되어 있는지를 기술
  • gradle은 이 파일에 기술된대로 프로젝트를 구성

0개의 댓글