有关使用gradle创建java web项目

来源:互联网 发布:素描照片软件 编辑:程序博客网 时间:2024/05/17 21:28

最近一直在用gradle构建web项目,因此为了留下印象同时分享构建经验,写下这篇博文。

首先新建一个java项目,将源文件夹设置成src/main/java和src/main/resources的格式,即gradle通用格式,添加WebContent文件夹,在文件夹下新建META-INF和WEB-INF按照web项目格式生成。(或者直接新建dynamic web项目就行)然后在项目中添加build.gradle文件,文件内容如下:



import org.gradle.plugins.ide.eclipse.model.Facet


apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'eclipse-wtp'




repositories{
mavenLocal()
mavenCentral()
}


sourceCompatibility = 1.7   // 设置 JDK 版本
webAppDirName = 'WebContent'    // 设置 WebApp 根目录


sourceSets{
main{
java{srcDir 'src/main/java'}//设置源文件夹路径
resources{srcDir 'src/main/resources'}
}
}


ext{
hibernateVersion ='5.0.6.Final'
springVersion = "4.2.6.RELEASE"
slf4jVersion = "1.7.21"
log4jVersion = "1.2.17"
jacksonVersion="2.7.4"
}


dependencies{
//spring
compile "org.springframework:spring-core:$springVersion"
compile "org.springframework:spring-oxm:$springVersion"
compile "org.springframework:spring-web:$springVersion"
compile "org.springframework:spring-webmvc:$springVersion"
compile "org.springframework:spring-tx:$springVersion"
compile "org.springframework:spring-aop:$springVersion"
compile "org.springframework:spring-context-support:$springVersion"
compile "org.springframework:spring-test:$springVersion"
compile "org.springframework:spring-orm:$springVersion"


//hibernate
compile "org.hibernate:hibernate-core:$hibernateVersion"
compile "org.hibernate:hibernate-ehcache:$hibernateVersion"
compile 'org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final'
compile 'org.hibernate:hibernate-validator:5.2.4.Final'
//compile 'dom4j:dom4j:1.6.1'
compile 'com.cloudhopper.proxool:proxool:0.9.1'
//compile 'org.apache.geronimo.specs:geronimo-jta_1.1_spec:1.1.1'
//compile 'org.jboss:jandex:2.0.0.Final'


//sql server connector
compile 'net.sourceforge.jtds:jtds:1.3.1'
//log
compile "log4j:log4j:$log4jVersion"
compile "org.slf4j:slf4j-log4j12:$slf4jVersion"
compile 'org.jboss.logging:jboss-logging:3.3.0.Final'

//utils
//compile 'commons-codec:commons-codec:1.10'
//compile 'ognl:ognl:3.1.4'
//json
compile "com.fasterxml.jackson.core:jackson-core:$jacksonVersion"
compile "com.fasterxml.jackson.core:jackson-annotations:$jacksonVersion"
compile "com.fasterxml.jackson.core:jackson-databind:$jacksonVersion"
}


//  Project Facets
eclipse {
    wtp {
        facet {
            facet name: 'jst.web', type: Facet.FacetType.fixed
            facet name: 'wst.jsdt.web', type: Facet.FacetType.fixed
            facet name: 'jst.java', type: Facet.FacetType.fixed
            facet name: 'jst.web', version: '3.0'
            facet name: 'jst.java', version: '1.7'
            facet name: 'wst.jsdt.web', version: '1.0'
        }
    }
}


//定义任务将下载的jar包拷贝到WebContent/WEB-INF的lib文件夹下
task copyJars(type: Copy) {
from configurations.compile
into "WebContent/WEB-INF/lib"
}


如果出现编译版本不对,可到项目目录下.setting文件夹下org.eclipse.wst.common.project.facet.core.xml文件更改java版本,或者在eclipse中更改编译的jdk版本。

然后,进入项目目录,输入gradle eclipse进行项目构建,下载依赖的库。

再在tomcat中运行,应该就可以运行了。

1 0
原创粉丝点击