Developing Kotlin Web applications using the Gradle build tool

Source: Internet
Author: User
Tags jcenter

1. Installing Gradle

Https://gradle.org/install. The author uses the Gradle Link: http://pan.baidu.com/s/1gf9qyC7 password: u3k6. Note Gradle requires jdk1.7 or higher

The zip package after download is as follows

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M01/98/CB/wKioL1lArpmgfBbDAAAFj4Vu10A378.png "title=" Picture 1.png "alt=" Wkiol1larpmgfbbdaaafj4vu10a378.png "/>

Unzip the ZIP package, create a new Gradle directory on the F-disk (Customize the location of the new directory) to place the extracted folder, and then copy the gradle-3.5 folder to F:\Gradle

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/98/CC/wKioL1lAt-iyqpubAAAMz0FUt64458.png "title=" Picture 2.png "alt=" Wkiol1lat-iyqpubaaamz0fut64458.png "/>

Configure the Gradle environment variable, append F:\Gradle\gradle-3.5\bin to the System environment variable path , point to Gradle's Bin directory, and click OK

CMD run gradle-version, as shown in the following content description installation succeeded


650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/98/CC/wKiom1lAuDmhKhuoAAASBfEDTjg928.png "title=" Picture 3.png "alt=" Wkiom1laudmhkhuoaaasbfedtjg928.png "/>

2. Installing the Gradle plugin


Select Help, Eclipse Marketplace... in eclipse, enter Buildship Click Go, then select Install Gradle plugin


650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/98/CC/wKioL1lAuGKBYmxiAADe6oUVkVk322.png "title=" Picture 4.png "alt=" Wkiol1laugkbymxiaade6ouvkvk322.png "/>


Restart Eclipse when the installation is complete. Select Window ---Preferences Select Gradle. Set Gradle cache directory, point to Gradle's installation directory (customizable cache directory)


650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M00/98/CC/wKioL1lAuLHzRtLSAACyH4M57AQ807.png "title=" Picture 5.png "alt=" Wkiol1laulhzrtlsaacyh4m57aq807.png "/>


3. developing Web applications

Create a new Gradle Project in Eclipse. Select Java EE view


650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/98/CC/wKiom1lAuTry3d4bAAAwlYheUPM763.png "title=" Picture 6.png "alt=" Wkiom1lautry3d4baaawlyheupm763.png "/>



New --Other , select Gradle Project


650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M02/98/CC/wKioL1lAuVzSrnImAACZAeYxcmE452.png "title=" Picture 7.png "alt=" Wkiol1lauvzsrnimaaczaeyxcme452.png "/>



Select Next


650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/98/CC/wKioL1lAuZWyN0-KAAC7dAEtVeI362.png "title=" Picture 8.png "alt=" Wkiol1lauzwyn0-kaac7daetvei362.png "/>


Enter a project name and then select Next


650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/98/CC/wKiom1lAubuC2-jKAACdYxbziM4709.png "title=" Picture 9.png "alt=" Wkiom1laubuc2-jkaacdyxbzim4709.png "/>


Select the Gradle installation directory and click Finish


650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/98/CC/wKioL1lAu8eBbkN6AACij5x50C4583.png "title=" Picture 10.png "alt=" Wkiol1lau8ebbkn6aacij5x50c4583.png "/>


The new Gradle project structure is as follows. Where Build.gradle is the master profile of the Gradle project, we only need to modify this configuration file to


650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M00/98/CC/wKioL1lAu_fidB5UAAAz2ZNfQh0091.png "title=" Picture 11.png "alt=" Wkiol1lau_fidb5uaaaz2znfqh0091.png "/>


Convert Gradle project to a Web project first. Right-click the project, select Properties, and then select Project Facets


650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M01/98/CC/wKioL1lAvHnSxgzRAADx72-f Q_Q 782.png "title=" Picture 12.png "alt=" wkiol1lavhnsxgzraadx72-f Q_Q 782.png "/>



The WebContent directory is added automatically


650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/98/CC/wKiom1lAvXeS77m6AAAz1f6p0Sg674.png "title=" Picture 13.png "alt=" Wkiom1lavxes77m6aaaz1f6p0sg674.png "/>


Under SRC, under Main, create a new WebApp directory, then copy the folder under WebContent and create a new index.jsp, enter the content index in index.jsp, and then delete the WebContent directory. The structure is as follows


650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M02/98/CC/wKioL1lAvcSxzOOPAAATukjP75o415.png "title=" Picture 14.png "alt=" Wkiol1lavcsxzoopaaatukjp75o415.png "/>



Open the Build.gradle file with some basic configuration as well as give us the build good


650) this.width=650; "Src=" Https://s2.51cto.com/wyfs02/M00/98/CC/wKiom1lAvfDC7mo0AACVbeCdpNM138.png " title= "Picture 15.png" width= "height=" 370 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:600px;height:370px; "alt=" Wkiom1lavfdc7mo0aacvbecdpnm138.png "/>



In order to compile Kotlin we need the Kotlin plugin to copy the following configuration to 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 plugin and War package plug-in required at the same time

Apply plugin: ' java ' Apply plugin: ' War '

Configuring a dependent library for Kotlin

dependencies {compile "org.jetbrains.kotlin:kotlin-stdlib: $kotlin _version"}

In order to develop a Web application we also need the Java EE Class Library

Providedcompile group: ' Javax ', Name: ' Javaee-api ', Version: ' 7.0 '

Here we use inline tomcat to run the application, so we need the Tomcat plugin to add the following configuration

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:to Mcat-embed-jasper:${tomcatversion} "}

Using the Tomcat plugin

Apply plugin: ' Com.bmuschko.tomcat '

The complete configuration is as follows

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 libraryapply 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 projectrepositories {     //&nBsp 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} "}


Select Project Right-click Select Gradle, Refresh Gradle project, and then wait a moment


650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M01/98/CC/wKioL1lAwIixgiEsAAA9cOKP7bU735.png "title=" Picture 16.png "alt=" Wkiol1lawiixgiesaaa9cokp7bu735.png "/>


At this time kotlin-stadlib, Javaee-api have been introduced into the CLASSPATH


The Kotlin plugin uses Src/main/kotlin as the source code directory by default and should be configured if you do not use the default Convention Sourcesets

sourcesets {main.kotlin.srcDirs + = ' Src/main/mykotlin ' main.java.srcDirs + = ' Src/main/myjava '}

Select Project Create a new source directory named Src/main/kotlin to hold the Kotlin source code


650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/98/CC/wKioL1lAwYezHxzRAAAn9mtRXFA589.png "title=" Picture 17.png "alt=" Wkiol1lawyezhxzraaan9mtrxfa589.png "/>


Select Kotin view to quickly create a new Kotlin Class


650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M00/98/CD/wKioL1lAxL3S0BfkAABukWpO9yo975.png "title=" Picture 18.png "alt=" Wkiol1laxl3s0bfkaabukwpo9yo975.png "/>


Kotlin dependencies are added automatically when new is completed


650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/98/CD/wKiom1lAxOSxIz8oAAA2VgVShG0694.png "title=" Picture 19.png "alt=" Wkiom1laxosxiz8oaaa2vgvshg0694.png "/>


Write the following code in HELLOWORLD.KT

Package Com.test.servlet Import Javax.servlet.http.HttpServletimport Javax.servlet.http.HttpServletRequestimport Javax.servlet.http.HttpServletResponseimport javax.servlet.annotation.WebServlet @WebServlet ("/helloworld") class Helloworld:httpservlet () {Override fun Doget (Req:httpservletrequest, Resp:httpservletresponse) {println ("en        ter the Doget method "); Resp.writer.write ("Hello, World")}}

4. running the application



Right-click the project in the Java EE view select Run as, run configurations, select Gradle Project, then select New


650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/98/CD/wKioL1lAxVPxbDDkAAEk5VFDwVM941.png "title=" Picture 20.png "Width=" "height=" 480 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:600px;height:480px; "alt=" Wkiol1laxvpxbddkaaek5vfdwvm941.png "/>

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M00/98/CD/wKioL1lAxXOj7UC6AAElH8T9_aY836.png "title=" Picture 21.png "Width=" "height=" 480 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:600px;height:480px; "alt=" Wkiol1laxxoj7uc6aaelh8t9_ay836.png "/>


Click Apply and then click Run to wait a few moments after the following words appear on the console console to start the success


650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/98/CD/wKiom1lAxaDg5aR2AAAX-0qo4H8038.png "title=" Picture 22.png "alt=" Wkiom1laxadg5ar2aaax-0qo4h8038.png "/>


Copy Http://localhost:8080/Gradle to the browser address bar


650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M02/98/CD/wKioL1lAxdqRsSBmAAAOYueXeI8900.png "title=" Picture 23.png "alt=" Wkiol1laxdqrssbmaaaoyuexei8900.png "/>


Enter the access path for the servlet Http://localhost:8080/Gradle/helloworld


650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/98/CD/wKiom1lAxf7yAaZkAAARXekKbbw281.png "title=" Picture 24.png "alt=" Wkiom1laxf7yaazkaaarxekkbbw281.png "/>



This article is from the "Love_programme-love_life" blog, make sure to keep this source http://codemcx.blog.51cto.com/9638142/1936567

Developing Kotlin Web applications using the Gradle build tool

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.