爲Gradle添加tomcat插件,調試WEB應用

Gradle提供了不輸於maven的依賴管理java

提供了強大的test功能,輸出優美的測試報告mysql

而且提供war插件,使用內置的jetty調試WEB應用web

由於博主恰恰鍾情於tomcat,因此但願使用tomcat來調試WEB應用spring

下面咱們來經過些許的代碼,將tomcat插件配置到項目中sql

實現一鍵調試部署WEB應用apache

 

build.gradle文件:api

buildscript { repositories { jcenter() } dependencies { classpath "com.bmuschko:gradle-tomcat-plugin:2.2.3" } } 
apply plugin: "java"
apply plugin: "war"
apply plugin: "eclipse" apply plugin: "com.bmuschko.tomcat"

sourceCompatibility = 1.8
version = "0.0.1-SNAPSHOT"
war.baseName = "jblog"
project.webAppDirName = "src/main/webapp"


//擴展屬性放在ext的內部類中
ext{
    hibernate_version="4.3.9.Final"
    spring_version="4.2.3.RELEASE"
}

configurations {
    provided
}

sourceSets {
    main{
        resources.srcDirs = ["src/main/java"]    //引入資源文件,打包時纔會將配置文件植入war文件
    }
    main.compileClasspath += configurations.provided
    test.compileClasspath += configurations.provided
    test.runtimeClasspath += configurations.provided
}

repositories {
    mavenLocal()
    maven {url "http://maven.oschina.net/content/groups/public/"}    
    mavenCentral()
    jcenter()
}

dependencies {
    compile(
        "org.hibernate:hibernate-core:${hibernate_version}",
        "org.hibernate:hibernate-ehcache:${hibernate_version}",
        "org.springframework:spring-core:${spring_version}",
        "org.springframework:spring-beans:${spring_version}",
        "org.springframework:spring-context:${spring_version}",
        "org.springframework:spring-tx:${spring_version}",
        "org.springframework:spring-web:${spring_version}",
        "net.sf.ehcache:ehcache:2.9.0",
        "mysql:mysql-connector-java:5.1.37",
        "log4j:log4j:1.2.17"
    )
        
    testCompile "junit:junit:4.7"
    provided "javax.servlet:javax.servlet-api:4.+" def tomcatVersion = '8.0.27' tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}", "org.apache.tomcat.embed:tomcat-embed-logging-juli:${tomcatVersion}", "org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}"
}

//注意下面這個配置,新版本gradle若是不使用數組添加的話會致使eclipse頻繁報錯
eclipse.classpath.plusConfigurations += [configurations.provided]
tasks.withType(JavaCompile) {  
    options.encoding = "UTF-8"  
}

tomcatRun.contextPath = '/jblog' tomcatRunWar.contextPath = '/jblog'

上面全部藍色字體就是全部須要配置的代碼數組

默認端口8080,執行tomcatRun命令後會提示:tomcat

The Server is running at http://localhost:8080/jblogapp

這時候訪問http://localhost:8080/jblog就能夠訪問你的應用進行調試了

由於tomcat的gradle插件是超輕量級的,不帶有任何附加的功能,因此訪問http://localhost:8080是看不到任何tomcat歡迎界面的

這樣看來也就是僅僅調試WEB應用了,與使用內置jetty調試貌似也沒多大區別,呵呵

相關截圖發一下,我很討厭那種只發文字不發圖片的blog,每每搞得人一頭霧水,忙中添亂

項目結構

 

執行gradle命令

 

輸入build tomcatRun命令(注意大小寫),點擊Run執行

終端輸出如上圖所示,這時候就能夠訪問URL測試咱們的應用了。

相關文章
相關標籤/搜索