最近項(xiàng)目中使用了Gradle來進(jìn)行構(gòu)建滥朱,項(xiàng)目選型用的是springboot,打包后是jar方式運(yùn)行位谋。但是在以前副签,公司使用的webproject遥椿,在不改變tomcat運(yùn)行環(huán)境的情況下,如何做到gradle進(jìn)行編譯和測(cè)試呢淆储?下面將用一個(gè)實(shí)例講解如何eclipse的web project和gradle進(jìn)行集成。
新建Dynamic Web Project家浇,命名為GradleWeb本砰。記得勾選生成web.xml復(fù)選框。
GIT:http://git.oschina.net/renchhao/gradle-test
在新建的項(xiàng)目下web項(xiàng)目根目錄新建build.gradle文件钢悲,同時(shí)編寫構(gòu)建腳本点额。如下:
import org.gradle.plugins.ide.eclipse.model.Facet
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'eclipse-wtp'
webAppDirName = 'WebContent'
sourceSets.main.java.srcDir 'src'
version = '1.0'
sourceCompatibility = 1.8
[compileJava]*.options*.encoding = 'UTF-8'
repositories {
maven { url 'http://maven.aliyun.com/nexus/content/groups/public'}
mavenLocal()
mavenCentral()
}
ext {
warName = 'gradle_test'
springVersion = '4.2.3.RELEASE'
hibernateVersion = '5.0.5.Final'
hibernateSearchVersion = '5.5.1.Final'
freemarkerVersion = '2.3.23'
httpclientVersion = '4.5'
jacksonVersion = '2.6.1'
}
war{
baseName ="${warName}"
from("$projectDir/src/") {
include "**/*.xml"
include "**/*.properties"
into('WEB-INF/classes')
}
}
dependencies {
compile (
"org.freemarker:freemarker:${freemarkerVersion}",
'commons-logging:commons-logging:1.2',
'commons-io:commons-io:2.4',
'commons-fileupload:commons-fileupload:1.3.1',
'commons-codec:commons-codec:1.10',
'org.apache.commons:commons-collections4:4.0',
'org.apache.commons:commons-lang3:3.4',
"org.springframework:spring-context-support:${springVersion}",
"org.springframework:spring-jdbc:${springVersion}",
"org.springframework:spring-orm:${springVersion}",
"org.springframework:spring-webmvc:${springVersion}",
"org.hibernate:hibernate-core:${hibernateVersion}",
"org.hibernate:hibernate-ehcache:${hibernateVersion}",
"org.hibernate:hibernate-search:${hibernateSearchVersion}",
'c3p0:c3p0:0.9.1.2',
'mysql:mysql-connector-java:5.1.35',
"com.fasterxml.jackson.core:jackson-core:${jacksonVersion}",
"com.fasterxml.jackson.core:jackson-databind:${jacksonVersion}",
"com.fasterxml.jackson.core:jackson-annotations:${jacksonVersion}",
"org.apache.httpcomponents:httpclient:${httpclientVersion}",
"org.apache.httpcomponents:httpmime:${httpclientVersion}",
"org.apache.httpcomponents:httpclient-cache:${httpclientVersion}",
'javax.activation:activation:1.1.1',
'javax.transaction:jta:1.1',
'javax.mail:mail:1.4.7',
'org.slf4j:slf4j-api:1.7.10',
'eu.bitwalker:UserAgentUtils:1.15',
'antlr:antlr:2.7.7',
'dom4j:dom4j:1.6.1',
'net.coobird:thumbnailator:0.4.8',
'org.apache.ant:ant:1.9.4',
'org.quartz-scheduler:quartz:2.2.1',
'cglib:cglib-nodep:3.1',
'dom4j:dom4j:1.6.1'
)
compile('javax.servlet:javax.servlet-api:3.1.0')
}
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.java', version: '1.8'
facet name: 'jst.web', version: '3.1'
facet name: 'wst.jsdt.web', version: '1.0'
}
}
}
task warApp(dependsOn: war){
doLast{
def tempWarDirOut = new File("${buildDir}/tmp/warApplication")
tempWarDirOut.mkdirs()
ant.unwar(src: "${buildDir}/libs/${warName}-${version}.war",
dest: "${buildDir}/tmp/warApplication")
}
}
在構(gòu)建腳本中,主要做了幾件事莺琳,引用war插件还棱,引用eclipse-wtp插件,用于識(shí)別eclipse項(xiàng)目和war包的打包惭等。
repositories中指定了阿里云的maven倉庫珍手,國內(nèi)比較快。ext中定義了幾個(gè)變量辞做,包括war包名稱和依賴項(xiàng)的版本號(hào)琳要。
其中的warApp這個(gè)自定義task是用于把war進(jìn)行解壓的操作,有些時(shí)候秤茅,可能使用war中的文件稚补,所以進(jìn)行一個(gè)war解壓,以便后續(xù)的操作框喳。
- 修改web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>gradle-test</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/app-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring配置 -->
<!-- ====================================== -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 指定Spring Bean的配置文件所在目錄课幕。默認(rèn)配置在WEB-INF目錄下 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:xin/lowang/config/app-context.xml</param-value>
</context-param>
</web-app>
為了做一個(gè)完成的mvc項(xiàng)目厦坛,我們繼續(xù)增加mvc配置和代碼。在WebContent/WEB-INF下新增app-servlet.xml
- /gradle-test/WebContent/WEB-INF/app-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 啟用spring mvc 注解 -->
<context:annotation-config />
<!-- 設(shè)置使用注解的類所在的jar包 -->
<context:component-scan base-package="xin.lowang"></context:component-scan>
<!-- 完成請(qǐng)求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<!-- 對(duì)轉(zhuǎn)向頁面的路徑解析乍惊。prefix:前綴粪般, suffix:后綴 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/jsp/" p:suffix=".jsp" />
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes" value = "text/html;charset=UTF-8" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
</beans>
- /gradle-test/src/xin/lowang/config/app-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 啟用spring mvc 注解 -->
<context:annotation-config />
<!-- 設(shè)置使用注解的類所在的jar包 -->
<context:component-scan base-package="xin.lowang" />
</beans>
- /gradle-test/src/xin/lowang/web/IndexController.java
package xin.lowang.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller("indexController")
public class IndexController {
@RequestMapping("/hello")
@ResponseBody
public String home() {
return "你好";
}
}
在eclipse中可以直接run on server,如果需要打包或者抽取文件污桦,則執(zhí)行g(shù)radle build/gradle warApp