헤르메스 LIFE

[Gradle] 개발환경 설정 본문

Build

[Gradle] 개발환경 설정

헤르메스의날개 2014. 3. 8. 12:42
728x90

* build.gradle 생성

/**
 * plugin 설정 : JAVA, WAR(JAVA-WEB)
 * 웹프로젝트는 eclipse-wtp 플러그인으로 분리되었다.
 * 따라서, WTP 프로젝트를 생성하려면 eclipse-wtp 플러그인도 적용해야 한다.
 * eclipse-wtp를 적용하면 eclipse 플러그인은 명시하지 않아도 자동 적용된다.
 */
apply plugin: "java"
apply plugin: "war"
apply plugin: "eclipse-wtp"

// JAVA Version 1.6
sourceCompatibility = 1.6
// 개발한 애플리케이션 버전
version = "1.0"

// 의존성 설정에 사용할 프로퍼티
ext {
    encoding      = "UTF-8"
    javaVersion   = "1.6"
    springVersion = "3.1.1.RELEASE"
}

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

def lib = [
    // Servlet 버젼에 따라 Dependency 경로가 다름.
    //providedCompile "javax.servlet:javax.servlet-api:3.0.1"
    "servlet" : [
        "javax.servlet:servlet-api:2.5" ,
        "javax.servlet.jsp:jsp-api:2.0"
    ],
    "spring" : [
          "org.springframework:spring-context:${springVersion}" ,
          "org.springframework:spring-context-support:${springVersion}" ,
          "org.springframework:spring-webmvc:${springVersion}" ,
          "org.springframework:spring-orm:${springVersion}" ,
    ],
    "aop" : [
          "org.aspectj:aspectjrt:1.7.3" ,
          "org.aspectj:aspectjweaver:1.7.3" ,
          "cglib:cglib:2.2.2" ,
          "cglib:cglib-nodep:2.2.2"
    ],
    "hirbernate" : [
          "org.hibernate:hibernate-entitymanager:4.2.3.Final" ,
          "org.hibernate:hibernate-envers:4.2.3.Final" ,
          "org.hibernate:hibernate-validator:5.0.1.Final"
    ],
    "hirbernate-related" : [
          "joda-time:joda-time:2.2" ,
          "joda-time:joda-time-hibernate:1.3" ,
          "com.google.guava:guava:14.0.1"
    ],
    "etc" : [
          "commons-logging:commons-logging:1.1.1" ,
          "commons-codec:commons-codec:1.8" ,
          "commons-dbcp:commons-dbcp:1.4" ,
          "mysql:mysql-connector-java:5.1.26" ,
          "javax.servlet:jstl:1.2" ,
          "org.projectlombok:lombok:0.12.0"
    ],
    "test" : [
          "org.springframework:spring-test:${springVersion}" ,
          "junit:junit:4.+" ,
          "org.mockito:mockito-core:1.9.0"
    ]
  
]

/**
 * 의존성 설정
 * Maven을 이용하여 Jar 파일을 내려받는다.
 */
dependencies {
    providedCompile lib.servlet
    compile lib.spring, lib.aop, lib.etc
}


// logback(slf4j)를 사용하기 때문에 모든 의존성에서 commons-logging는 제외
[configurations.runtime, configurations.default]*.exclude( module: "commons-logging" )

// JAVA 컴파일시 인코딩 설정
[compileJava, compileTestJava]*.options*.encoding = encoding

// TEST 설정
test {
    jvmArgs = ["-ea" , "-Xmx256m" ]
    logging.captureStandardOutput(LogLevel.INFO)
    testReport = false
}

/**
 * Eclipse 환경설정.
 */
eclipse {
    classpath {
        downloadSources = true
    }
    // workspace/{project}/.settings 폴더를 설정한다.
    wtp {
        // .settings 폴더의 org.eclipse.wst.common.component 파일을 설정한다.
        component {
            //contextPath = project.name // 원하는 contextPath 지정. 단, 빈 컨텍스트패스는 "/" 로 지정
            contextPath = "" // 원하는 contextPath 지정. 단, 빈 컨텍스트패스는 "/" 로 지정
        }
        // .settings 폴더의 org.eclipse.wst.common.project.facet.core.xml 파일을 설정한다.
        facet {
            facet name: "jst.web" , version: "2.5" // Servlet Spec Version 지정
            facet name: "jst.java" , version: "1.6" // Java Version 지정, 1.7 ...
            facet name: "wst.jsdt.web" , version: "1.0"   // Javascript 지정, 1.0
        }
    }
}

war {
    exclude "WEB-INF/lib/**"
    exclude "WEB-INF/classes/**"

    // copy properties file in classes so that
    // they may be loaded from classpath
    from("resources")    {
        include "*.properties"
        into "WEB-INF/classes/"
    }

    //webInf { from "src/additionalWebInf" } // adds a file-set to the WEB-INF dir.
    //classpath fileTree("additionalLibs") // adds a file-set to the WEB-INF/lib dir.
    //webXml = file("src/main/webapp/WEB-INF/web.xml") // copies a file to WEB-INF/web.xml
}

// 프로젝트 초기화
// 1. java source directory 생성 : src/main/java, src/test/java
// 2. resource directory 생성    : src/main/resource, src/test/resource
// 3. web source directory 생성  : src/main/webapp, src/main/webapp/WEB-INF
// .settings 폴더의 org.eclipse.wst.common.component 파일을 설정한다.
task initProject(description: "initialize project") << {
    createDir = {
        println "create source directory: $it"
        if (!it.exists()) it.mkdirs()
    }
    sourceSets *.java. srcDirs*.each createDir
    sourceSets *.resources. srcDirs*.each createDir
    [webAppDir, new File(webAppDir, "/WEB-INF" ), new File(webAppDir, "/WEB-INF/spring" ), new File(webAppDir, "/WEB-INF/views" ), new File(webAppDir, "/META-INF" )].each createDir
}
//eclipseClasspath.dependsOn initProject

// 모든 의존성 *.jar들을 build/libs 에 복사한다.
task copyLibs(type: Sync) {
    from configurations.runtime
    into "WebContent/WEB-INF/lib"
    //into libDeployPath
}
//build.dependsOn(copyLibs)


task wrapper(type: Wrapper) {
    gradleVersion = "1.11"
}


* Eclipse 환경 생성
> gradle eclipse



// 프로젝트 기본폴더 생성
// 1. java source directory 생성 : src/main/java, src/test/java
// 2. resource directory 생성    : src/main/resource, src/test/resource
// 3. web source directory 생성  : src/main/webapp, src/main/webapp/WEB-INF
// .settings 폴더의 org.eclipse.wst.common.component 파일을 설정한다.
task initProject(description: "initialize project") << {
    createDir = {
        println "create source directory: $it"
        if (!it.exists()) it.mkdirs()
    }
    sourceSets *.java. srcDirs*.each createDir
    sourceSets *.resources. srcDirs*.each createDir
    [webAppDir, new File(webAppDir, "/WEB-INF" ), new File(webAppDir, "/WEB-INF/spring" ), new File(webAppDir, "/WEB-INF/views" ), new   File(webAppDir, "/META-INF" )].each createDir
}
eclipseClasspath.dependsOn initProject // <-- 막혀있던 걸 풀고 실행하면 Eclipse Import 시 Maven 개발환경과 같은
                                              환경을 바로 구성할 수 있다.


* Eclipse Existing Project into Workspace






728x90

'Build' 카테고리의 다른 글

[Maven] 수동으로 jar파일 다운받는 방법은?  (0) 2020.11.12
[Maven] 최신버젼 실행 시 오류  (0) 2020.11.12
[Gradle] Gradle task 실행  (0) 2014.03.08
[Gradle] build.gradle 설정방법  (0) 2014.03.08
[Gradle] Gradle의 소개  (0) 2014.03.08