赞
踩
spring和spring
In this post, we will develop and test simple Spring MVC Gradle application using Eclipse Gradle Plugin.
在本文中,我们将使用Eclipse Gradle插件开发和测试简单的Spring MVC Gradle应用程序。
This post is not intended to train you on Spring MVC Framework Basics. If you are not familiar with Spring MVC, Please read Spring MVC Example.
这篇文章无意于培训您Spring MVC Framework基础知识。 如果您不熟悉Spring MVC,请阅读Spring MVC Example 。
We are going to convert Spring MVC maven example from Spring MVC Tutorial into spring mvc gradle example.
我们将把Spring MVC教程中的 Spring MVC maven示例转换为spring mvc gradle示例。
It’s also recommended to go through my previous Gradle posts at Gradle Tutorial.
还建议您阅读我以前在Gradle Tutorial上的Gradle帖子。
Below are the steps to follow for converting maven to gradle application.
以下是将Maven转换为Gradle应用程序要遵循的步骤。
pom.xml
file 删除pom.xml
文件 build.gradle
file. 创建一个空的build.gradle
文件。 build.gradle
build.gradle
- apply plugin: 'java'
- apply plugin: 'eclipse'
-
- sourceCompatibility = 1.7
-
- repositories {
- mavenCentral()
- }
-
- dependencies {
- // Dependencies goes here
- }
We will take this one as base build file and update it with required elements one by one.
我们将以此为基础构建文件,并用所需的元素逐一更新它。
Also Provide war file name and it’s version.
还提供war文件名及其版本。
- apply plugin: "war"
- war {
- baseName = 'SpringMVCExample'
- version = '1.0.0-BUILD-SNAPSHOT'
- }
- dependencies {
- compile("org.springframework:spring-context:4.0.0.RELEASE")
- compile("org.springframework:spring-webmvc:4.0.0.RELEASE")
- compile("org.aspectj:aspectjrt:1.7.4")
- }
- dependencies {
- compile("javax.inject:javax.inject:1")
- compile("javax.servlet:servlet-api:2.5")
- compile("javax.servlet:jstl:2.5")
- compile("javax.servlet.jsp:jsp-api:1.2")
- compile("org.slf4j:slf4j-api:1.7.5")
- compile("org.slf4j:jcl-over-slf4j:1.7.5")
- compile("org.slf4j:slf4j-log4j12:1.7.5")
- compile("log4j:log4j:1.2.15")
- }
- dependencies {
- testCompile("junit:junit:4.7")
- }
We use “compile()” element to add dependencies.
我们使用“ compile()”元素添加依赖项。
compile("org.springframework:spring-context:4.0.0.RELEASE")
We use “providedRuntime()” element to add dependencies at runtime.
我们使用“ providedRuntime()”元素在运行时添加依赖项。
providedRuntime("javax.servlet:jstl:2.5")
We use “testCompile()” element to add dependencies for running only unit tests.
我们使用“ testCompile()”元素来添加仅用于运行单元测试的依赖项。
testCompile("junit:junit:4.7")
build.gradle
build.gradle
- dependencies {
- compile("org.springframework:spring-context:4.0.0.RELEASE")
- compile("org.springframework:spring-webmvc:4.0.0.RELEASE")
- compile("org.aspectj:aspectjrt:1.7.4")
-
- compile("javax.inject:javax.inject:1")
- compile("javax.servlet:servlet-api:2.5")
- compile("javax.servlet:jstl:2.5")
- compile("javax.servlet.jsp:jsp-api:1.2")
- compile("org.slf4j:slf4j-api:1.7.5")
- compile("org.slf4j:jcl-over-slf4j:1.7.5")
- compile("org.slf4j:slf4j-log4j12:1.7.5")
- compile("log4j:log4j:1.2.15")
-
- testCompile("junit:junit:4.7")
- }

- gradle clean
- gradle build
Or execute both commands at once using the following syntax. It’s similar to executing Maven commands.
或使用以下语法一次执行两个命令。 这类似于执行Maven命令。
gradle clean build
I always recommend everyone to use “clean” command before building our application. It cleans our old build stuff, compiles and creates new WAR/JAR with latest files.
我总是建议每个人在构建我们的应用程序之前使用“ clean”命令。 它会清理我们的旧版本内容,编译并使用最新文件创建新的WAR / JAR。
War file is created with given Name and Version in build.gradle file. i.e. SpringMVCExample-1.0.0-BUILD-SNAPSHOT.war
在build.gradle文件中使用给定的名称和版本创建War文件。 即SpringMVCExample-1.0.0-BUILD-SNAPSHOT.war
We have developed Spring MVC gradle application successfully. Now it’s time to deploy and test this application. You can deploy this application into any Web/Application Server. I’m going to test this application using Apache Tomcat 8.
我们已经成功开发了Spring MVC gradle应用程序。 现在是时候部署和测试该应用程序了。 您可以将此应用程序部署到任何Web /应用程序服务器中。 我将使用Apache Tomcat 8测试该应用程序。
- ${TOMCAT8_HOME} = D:\apache-tomcat-8.0.24
- ${TOMCAT8_BIN} = D:\apache-tomcat-8.0.24\bin
D:\apache-tomcat-8.0.24\bin>startup.bat
Access homepage using this url: https://localhost:8888/SpringMVCExample-1.0.0-BUILD-SNAPSHOT/
使用此URL访问主页: https://localhost:8888/SpringMVCExample-1.0.0-BUILD-SNAPSHOT/
Here our application RootContext is “SpringMVCExample-1.0.0-BUILD-SNAPSHOT”. If required, we can give proper name like “SpringMVCExample” to the WAR file.
在这里,我们的应用程序RootContext是“ SpringMVCExample-1.0.0-BUILD-SNAPSHOT”。 如果需要,我们可以为WAR文件指定一个适当的名称,例如“ SpringMVCExample”。
Access Login page using this url: localhost:8888/SpringMVCExample-1.0.0-BUILD-SNAPSHOT/login/
使用以下URL访问登录页面: localhost:8888/SpringMVCExample-1.0.0-BUILD-SNAPSHOT/login/
Provide Username,click on “Login” button and observe next page as shown below:
提供用户名,单击“登录”按钮,然后观察下一页,如下所示:
If we observe our both SpringMVCExample Maven and Gradle based projects, we can observe the following things.
如果我们同时观察基于SpringMVCExample Maven和Gradle的项目,则可以观察到以下内容。
Apart from these benefits, We can also get many more benefits from Gradle file. Please refer them at my first post about Gradle.
除了这些好处,我们还可以从Gradle文件中获得更多好处。 请在我关于Gradle的第一篇文章中参考他们。
Because of these reasons, now-a-days most of the new projects are starting with Gradle build tool and existing projects are converting from Maven to Gradle.
由于这些原因,当今大多数新项目都从Gradle构建工具开始,而现有项目正从Maven转换为Gradle。
That’s it for gradle example with spring mvc application. Like this we can develop any application using Gradle build tool.
这是Spring MVC应用程序的gradle示例。 这样,我们可以使用Gradle构建工具开发任何应用程序。
spring和spring
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。