zoukankan      html  css  js  c++  java
  • gradle入门(1-5)创建并运行Web应用

    一、使用Gretty运行Web应用

    Gretty支持Jetty和Tomcat,它不会被Gradle缺少SLF4J绑定所导致的问题所困扰。

    1、配置文件build.gradle

    buildscript {
        repositories {
            jcenter()
        }
        dependencies {
            classpath 'org.akhikhl.gretty:gretty:+'
        }
    }

    apply plugin: 'java'
    apply plugin: 'war'
    apply plugin: 'org.akhikhl.gretty'

    gretty {
        port = 8080
        contextPath = '/'
        servletContainer = 'jetty9'
    }

    2、开启或终止我们的Web应用:

    gradle appStart 命令能运行Web应用。
    gradle appStop 命令能终止Web应用。

    二、创建 Spring Boot Web 应用项目 

    使用Spring Boot,我们可以将web应用程序打包为一个可执行的jar文件,这个文件使用嵌入式的servlet容器。

    1、添加java插件后的build.gradle

    apply plugin: 'java'

    sourceCompatibility = 1.8

    targetCompatibility = 1.8

    Java插件会在我们的构建中添加新的约定(如默认的目录结构)、任务和属性集。

    2、添加测试插件后的build.gradle

    buildscript {
        repositories {
            jcenter()
        }
        dependencies {
            classpath(
                    'org.unbroken-dome.gradle-plugins:gradle-testsets-plugin:1.0.2'
            )
        }
    } 
     
    apply plugin: 'java'
    apply plugin: 'org.unbroken-dome.test-sets' 
     
    sourceCompatibility = 1.8
    targetCompatibility = 1.8 
     
    testSets {
        integrationTest { dirName = 'integration-test' }
    }
     
    project.integrationTest {
        outputs.upToDateWhen { false }
    }
     
    check.dependsOn integrationTest
    integrationTest.mustRunAfter test
     
    tasks.withType(Test) {
        reports.html.destination = file("${reporting.baseDir}/${name}")
    } 

    3、添加springboot支持后的build.gradle

    buildscript {

        repositories {

            jcenter()

        }

        dependencies {

            classpath(

                    'org.springframework.boot:spring-boot-gradle-plugin:1.2.5.RELEASE',

                    'org.unbroken-dome.gradle-plugins:gradle-testsets-plugin:1.0.2'

            )

        }

    }

     

    apply plugin: 'java'

    apply plugin: 'org.unbroken-dome.test-sets'

    apply plugin: 'spring-boot' 

     

    sourceCompatibility = 1.8

    targetCompatibility = 1.8

     

    testSets {

        integrationTest { dirName = 'integration-test' }

    } 

    project.integrationTest {

        outputs.upToDateWhen { false }

    } 

     

    check.dependsOn integrationTest

    integrationTest.mustRunAfter test 

     

    tasks.withType(Test) {

        reports.html.destination = file("${reporting.baseDir}/${name}")

    }

     

    我们无须使用Bintray的Jcenter Maven仓库,但是由于Gradle测试集插件依赖于该仓库,因此本文中的演示程序也将其加入。

    在应用Spring Boot Gradle插件后,我们可以:

    • 将应用程序打包为可执行的jar文件。
    • 使用bootrun任务运行程序。
    • 省略Spring Boot依赖的版本信息。
    • 将应用程序打包为war文件。

    当然,我们也可以对Spring Boot Gradle插件进行配置,并自定义执行和打包应用程序的任务。

    4、获取所需的依赖项

    我们可以通过所谓的starter POM来获取Spring Boot应用的依赖。Spring Boot的参考指南将starter POM描述如下:

    starter POM是一组可以被包含到项目中的便捷依赖描述符。你可以一站式的获取所有需要的Spring和相关技术,无需苦苦寻找演示代码,也无需复制粘贴大量的依赖描述符。

    换句话说,我们只需选择正确的starter POM,并将其加入到Gradle构建中即可。

    我们可以通过以下步骤获取所需的依赖:

    1. 确保所有的依赖都从Maven2的中央仓库获取。

    2. compile配置里添加spring-boot-starter-actuator依赖,我们之所以需要这个依赖,是因为它提供了一种监控应用运行状态的方法。

    3. compile配置里添加spring-boot-starter-thymeleaf依赖,我们之所以需要该依赖,是因为我们需要使用Thymeleaf作为创建Web应用的模版引擎。

    4. testCompile配置里添加spring-boot-starter-test依赖,我们之所以需要该依赖,是因为我们需要在Web应用中编写单元测试和集成测试。

    build.gradle的源代码如下:

    buildscript {

        repositories {

            jcenter()

        }

        dependencies {

            classpath(

                    'org.springframework.boot:spring-boot-gradle-plugin:1.2.5.RELEASE',

                    'org.unbroken-dome.gradle-plugins:gradle-testsets-plugin:1.0.2'

            )

        }

    }

     

    apply plugin: 'java'

    apply plugin: 'org.unbroken-dome.test-sets'

    apply plugin: 'spring-boot'

     

    sourceCompatibility = 1.8

    targetCompatibility = 1.8

     

    repositories {

        mavenCentral()

    }

     

    dependencies {

        compile(

                'org.springframework.boot:spring-boot-starter-actuator',

                'org.springframework.boot:spring-boot-starter-thymeleaf'

        )

        testCompile('org.springframework.boot:spring-boot-starter-test')

    }

     

    testSets {

        integrationTest { dirName = 'integration-test' }

    }

     

    project.integrationTest {

        outputs.upToDateWhen { false }

    }

     

    check.dependsOn integrationTest

    integrationTest.mustRunAfter test

     

    tasks.withType(Test) {

        reports.html.destination = file("${reporting.baseDir}/${name}")

    }

    我们不需要设置Spring Boot依赖的版本,因为Spring Boot Gradle插件能够决定这些依赖的版本。

    换句话说,我们可以通过设置Spring Boot Gradle插件的版本来选择偏好的Spring Boot的版本。

    5、运行spring boot程序

    5.1、方法一、在开发阶段可以使用Spring Boot Gradle插件中的bootRun任务运行应用程序,而无需创建jar文件。

    使用这个方法,因为它可以使我们静态的classpath资源(即:在src/main/resources下的文件)都成为可重载的资源。

    换句话说,如果我们使用这个方法,就可以在Spring Boot应用程序运行时对这些文件进行更改,而且可以在不重启应用的情况下观察到变化。

    通过在命令提示符中输入以下命令,就可以使用该方法了。

    gradle clean bootRun

    5.2、方法二、我们可以将应用程序打包为一个可执行的jar文件,继而执行所创建的文件。

    如果想要在一台远程服务器上运行Spring Boot应用,应当采用这种方法。通过在命令提示符中输入以下命令,就可以创建一个可执行的jar文件了。
    gradle clean build
    这条命令会在build/libs目录下创建spring-boot-web-application.jar文件。在将其复制到远程服务器上后,可以通过以下命令运行应用程序。
    java -jar spring-boot-web-application.jar

  • 相关阅读:
    asp.net下的网页编辑器
    在Visual C#中访问不同数据库
    VS2008 sp1中文版下载地址
    常用封装链接数据库类
    常用封装日志类
    动态构建OrderBy的Lambda表达式
    用户管理抽象类
    存储过程导出数据库数据
    应用程序xml 配置文件抽象基类
    ini文件示例说明
  • 原文地址:https://www.cnblogs.com/lexiaofei/p/6992894.html
Copyright © 2011-2022 走看看