初試Gradle

     這maven大行其道的當下,咱們已經不用再使用MyEclipse 或是Intellij IDEA給咱們提供的新建項目那麼繁瑣的流程了,按照須要和規則咱們能夠快速的完成一個項目的新建。java

    有別於Maven使用骨架的方式能夠一步到位的新建好項目,Gradle還須要咱們手動的添加一些文件夾,幸虧Gradle的文件夾規則和Maven差很少,順手新建幾個文件夾就行了。git

    忽略代碼咱們直接上build.gradle. 下面是一個普通java項目github

//插件 java插件 和 application插件 若是想看都有什麼task 能夠執行gradle tasks
apply plugin: 'java'
apply plugin: 'application'

sourceCompatibility = 1.5
version = '1.0'

//運行的項目入口,裏面一般有main方法。
mainClassName = 'com.lee.study.HelloGradle'

repositories {
    mavenCentral()
    /*maven{
        credentials{
            username 'admin'
            password 'admin123'
        }
        url "http://localhost:8081/nexus/content/groups/public/"
    }*/
}
//jar依賴
dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

經過上面的設置咱們能夠編譯 打包 運行 生成javadoc等各類操做,web

下面咱們在看一個web 項目的build.gradlespring

//插件 war 插件 jetty 插件 tomcat插件
apply plugin: 'war'
apply plugin: 'jetty'
//apply plugin: 'tomcat'

//sourceCompatibility = 1.5 //這是IDEA新建項目帶的 好像是過期了 so 我註釋起來了,不是tomcat插件的配置
version = '1.0'

repositories {
    mavenCentral()
    maven{
        credentials{
            username 'admin'
            password 'admin123'
        }
        url "http://localhost:8081/nexus/content/groups/public/"
    }
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
    compile 'org.springframework:spring-webmvc:3.2.0.RELEASE'
    runtime 'javax.servlet:jstl:1.1.2'
    
    //tomcat 插件的配置
   /* def tomcatVersion = '7.0.11'
    tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
            "org.apache.tomcat.embed:tomcat-embed-logging-juli:${tomcatVersion}"
    tomcat("org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}") {
        exclude group: 'org.eclipse.jdt.core.compiler', module: 'ecj'
    }*/
}
tomcat 插件的配置
/*
tomcat {
    httpPort = 8090
    stopPort = 8091
    //httpsPort = 8091
    //enableSSL = true
}

tomcatRun{
    contextPath= 'server'
    URIEncoding= 'UTF-8'
    reloadable = 'true'

}
*/

//jetty 插件的的配置
httpPort = 8090
stopPort  = 8091
stopKey = "kelljetty"

jettyRun{
    contextPath = 'server'
    reload = 'automatic'
    scanIntervalSeconds = 10
}

//tomcat 插件的配置
/*
buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath 'org.gradle.api.plugins:gradle-tomcat-plugin:1.2.3'
    }
}*/

上面的web 項目我把tomcat plugin的都註釋起來了,我本人在項目開發中比較喜歡使用tomcat的插件,無論是Maven仍是Gradle中,可是大多數人都喜歡jetty來進行嵌入式的服務器用於開發。apache

給喜歡Tomcat的同窗提供一下Gradle tomcat plugin的 GitHub地址api

https://github.com/bmuschko/gradle-tomcat-plugin tomcat

我就是照着配得 很簡單,要是添加什麼按照readme中得介紹配就好了服務器

http://git.oschina.net/lixuwei/hello-gradlemvc

這是我寫得一個小例子。

相關文章
相關標籤/搜索