下載地址https://gradle.org/install。筆者使用的Gradle下載地址連接: http://pan.baidu.com/s/1gf9qyC7 密碼: u3k6。注意Gradle要求jdk1.7或更高java
下載後的zip包以下web
將zip包解壓,在F盤新建一個Gradle目錄(新建目錄的位置自定義)用來放置解壓後的文件夾,而後將gradle-3.5文件夾複製到F:\Gradle下apache
配置Gradle環境變量,在系統環境變量path中追加F:\Gradle\gradle-3.5\bin;指向Gradle的bin目錄,而後點擊肯定api
CMD運行gradle -version,如出現如下截圖中的內容說明安裝成功瀏覽器
在Eclipse中選擇Help -> Eclipse Marketplace...,輸入buildship點擊Go,而後選擇Install安裝Gradle插件緩存
安裝完成後重啓Eclipse。選擇Window -> Preferences 選擇Gradle。設置Gradle的緩存目錄,指向Gradle的安裝目錄(可自定義緩存目錄)tomcat
在Eclipse中新建Gradle Project。選擇JavaEE視圖app
New -> Other ,選擇Gradle Projectwebapp
選擇Nextjsp
輸入項目名稱,而後選擇Next
選擇Gradle的安裝目錄而後點擊Finish
新建的Gradle項目結構以下。其中build.gradle是Gradle項目的主配置文件,咱們只須要修改這個配置文件便可
先將Gradle Project 轉換成Web項目。右擊項目選擇Properties,而後選擇Project Facets
會自動添加WebContent目錄
在src下的main下面新建webapp目錄,而後將WebContent下的文件夾複製過去並新建index.jsp,在index.jsp中輸入內容index,而後將WebContent目錄刪除。結構以下
打開build.gradle文件,其中一些基本的配置以及給咱們生成好了
爲了編譯Kotlin咱們須要Kotlin插件,將以下配置複製到build.gradle中
buildscript { ext.kotlin_version = '1.1.2' repositories { jcenter() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } apply plugin: 'kotlin'
同時須要java插件和war包插件
apply plugin: 'java' apply plugin: 'war'
配置Kotlin的依賴庫
dependencies { compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" }
爲了開發web應用程序咱們還須要JavaEE類庫
providedCompile group: 'javax', name: 'javaee-api', version: '7.0'
這裏咱們使用內嵌的tomcat來運行應用程序,因此須要tomcat插件,添加以下配置
dependencies { def tomcatVersion = '7.0.68' 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}" }
使用tomcat插件
apply plugin: 'com.bmuschko.tomcat'
完整配置以下
buildscript { ext.kotlin_version = '1.1.2' repositories { mavenCentral() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath 'com.bmuschko:gradle-tomcat-plugin:2.2.2' } } // Apply the java-library plugin to add support for Java Library apply plugin: 'java-library' apply plugin: 'kotlin' apply plugin: 'java' apply plugin: 'war' apply plugin: 'com.bmuschko.tomcat' // In this section you declare where to find the dependencies of your project repositories { // Use jcenter for resolving your dependencies. // You can declare any Maven/Ivy/file repository here. mavenCentral() } dependencies { // Use JUnit test framework testImplementation 'junit:junit:4.12' providedCompile group: 'javax', name: 'javaee-api', version: '7.0' compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" } dependencies { def tomcatVersion = '7.0.68' 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 -> Refresh Gradle Project,而後進行等待片刻
此時kotlin-stadlib、javaee-api都已經引入到classpath中
Kotlin插件默認使用src/main/kotlin做爲源代碼目錄,若是不使用默認約定則應配置sourceSets
sourceSets { main.kotlin.srcDirs += 'src/main/myKotlin' main.java.srcDirs += 'src/main/myJava' }
選擇項目新建一個source目錄命名爲src/main/kotlin用來存放Kotlin源代碼
選擇Kotin視圖可快速新建Kotlin Class
新建完成後會自動添加Kotlin依賴
在HelloWorld.kt中編寫以下代碼
package com.test.servlet import javax.servlet.http.HttpServlet import javax.servlet.http.HttpServletRequest import javax.servlet.http.HttpServletResponse import javax.servlet.annotation.WebServlet @WebServlet("/helloworld") class HelloWorld : HttpServlet(){ override fun doGet(req: HttpServletRequest, resp: HttpServletResponse) { println("enter the doGet method"); resp.writer.write("Hello, World") } }
在JavaEE視圖中右擊項目選擇Run as -> Run Configurations,選擇Gradle Project,而後選擇New
點擊Apply而後點擊Run,等到片刻後如Console控制檯上出現如下字樣說明啓動成功
將http://localhost:8080/Gradle複製到瀏覽器地址欄
輸入Servlet的訪問路徑http://localhost:8080/Gradle/helloworld