博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Gradle加入Tomcat运行HelloWorld测试
阅读量:91 次
发布时间:2019-02-26

本文共 5088 字,大约阅读时间需要 16 分钟。

前提条件:Eclipse已安装Gradle的buildship插件,本机已安装和配置Gradle环境。

测试环境:Gradle-version:2.4    JDK:1.7.0_51。

1.创建Gradle项目

New >Others...>选择Gradle视图目录:

选择Gradle Project点击Next:

输入项目名称"MyGradle",点击Next:

选择Gradle的安装路径,点击Next:

最后点击Finish完成项目创建。

2.修改build.gradle配置

build.gradle初始文件配置如下:

/* * This build file was auto generated by running the Gradle 'init' task * by 'pengjunlin' at '16-11-25 上午11:12' with Gradle 2.4 * * This generated file contains a sample Java project to get you started. * For more details take a look at the Java Quickstart chapter in the Gradle * user guide available at http://gradle.org/docs/2.4/userguide/tutorial_java_projects.html */// Apply the java plugin to add support for Javaapply plugin: 'java'// In this section you declare where to find the dependencies of your projectrepositories {    // Use 'jcenter' for resolving your dependencies.    // You can declare any Maven/Ivy/file repository here.    jcenter()}// In this section you declare the dependencies for your production and test codedependencies {    // The production code uses the SLF4J logging API at compile time    compile 'org.slf4j:slf4j-api:1.7.12'    // Declare the dependency for your favourite test framework you want to use in your tests.    // TestNG is also supported by the Gradle Test task. Just change the    // testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add    // 'test.useTestNG()' to your build script.    testCompile 'junit:junit:4.12'}
此文件需要做相应的修改,修改成如下配置:

/* * This build file was auto generated by running the Gradle 'init' task * by 'pengjunlin' at '16-11-25 上午9:48' with Gradle 2.4 * * This generated file contains a sample Java project to get you started. * For more details take a look at the Java Quickstart chapter in the Gradle * user guide available at http://gradle.org/docs/2.4/userguide/tutorial_java_projects.html */// Apply the java plugin to add support for Javaapply plugin: 'java'// Apply the war plugin to add support for Javaapply plugin: 'war' // Tomcat pluginapply plugin: 'com.bmuschko.tomcat'// In this section you declare where to find the dependencies of your projectrepositories {    // Use 'jcenter' for resolving your dependencies.    // You can declare any Maven/Ivy/file repository here.    jcenter()}// In this section you declare the dependencies for your production and test codedependencies {    // The production code uses the SLF4J logging API at compile time    compile 'org.slf4j:slf4j-api:1.7.12'    // Declare the dependency for your favourite test framework you want to use in your tests.    // TestNG is also supported by the Gradle Test task. Just change the    // testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add    // 'test.useTestNG()' to your build script.    testCompile 'junit:junit:4.12'}// TomcatServerdependencies {    def tomcatVersion = '7.0.59'    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}"}// Build scriptbuildscript {    repositories {        jcenter()    }    dependencies {        classpath 'com.bmuschko:gradle-tomcat-plugin:2.2.2'    }}

刷新项目:

3.创建业务实体类HelloWorld.java

package com.boonya.gradle.entity;public class HelloWorld {	private String greeting;	public String getGreeting() {		if (greeting == null)			greeting = sayHelloWorld();		return greeting;	}	public void setGreeting(String greeting) {		this.greeting = greeting;	}	public String sayHelloWorld() {		return "Hello world!";	}}

4.创建JSP页面helloworld.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"	pageEncoding="UTF-8"%>
Hello World

${helloworld.greeting}

最后展示一下项目结构:

5.将Gradle项目运行至Tomcat

Gradle>build项目:

build项目日志:

Working Directory: C:\DEVELOPER_WORKSPACE\DEVELOPERS\ECLIPSE\eclipse_luna_SR2\workspace\MyGradleGradle User Home: C:\Users\pengjunlin\.gradleGradle Distribution: Local installation at C:\DEVELOPER_INSTALLS\gradle-2.4Gradle Version: 2.4Java Home: C:\Program Files\Java\jdk1.7.0_51JVM Arguments: NoneProgram Arguments: NoneGradle Tasks: build:compileJava UP-TO-DATE:processResources UP-TO-DATE:classes UP-TO-DATE:war UP-TO-DATE:assemble UP-TO-DATE:compileTestJava UP-TO-DATE:processTestResources UP-TO-DATE:testClasses UP-TO-DATE:test UP-TO-DATE:check UP-TO-DATE:build UP-TO-DATEBUILD SUCCESSFULTotal time: 4.616 secs
项目右键>Run as>Run  Configurations:

输入如上图所示的参数,然后选择项目运行需要的Java Home:

点击Run按钮即可运行。后台启动日志:

Working Directory: C:\DEVELOPER_WORKSPACE\DEVELOPERS\ECLIPSE\eclipse_luna_SR2\workspace\MyGradleGradle User Home: C:\Users\pengjunlin\.gradleGradle Distribution: Gradle wrapper from target buildGradle Version: 2.4Java Home: C:\Program Files\Java\jdk1.7.0_51JVM Arguments: NoneProgram Arguments: NoneGradle Tasks: tomcatRun:compileJava UP-TO-DATE:processResources UP-TO-DATE:classes UP-TO-DATE:tomcatRunStarted Tomcat ServerThe Server is running at http://localhost:8080/MyGradle
注意:Gradle默认在Tomcat的8080端口运行。运行效果如下:

6.Eclipse运行处理进程问题

运行期间右下角进程中一直处于加载过程中,对此不是很明白,但是控制台确实是启动成功了的。

7.Gradle 可以配置的Tasks

你可能感兴趣的文章