spring和spring_Spring MVCGradle

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

Spring MVCGradle (Spring MVC Gradle)

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帖子。

Spring MVC将Maven转换为Gradle (Spring MVC Convert Maven to Gradle)

Below are the steps to follow for converting maven to gradle application.

以下是将Maven转换为Gradle应用程序要遵循的步骤。

  1. Develop the Maven based Spring MVC Application by following this link: Spring MVC Tutorial or download the code from below link.

    通过以下链接来开发基于Maven的Spring MVC应用程序: Spring MVC教程或从下面的链接下载代码。
  2. Import this spring mvc maven project into Eclipse IDE.

    将此spring mvc maven项目导入Eclipse IDE。
  3. Remove pom.xml file

    删除pom.xml文件
  4. Create empty build.gradle file.

    创建一个空的build.gradle文件。
  5. Now start converting each and every component from pom.xml to build.gradle file.

    现在开始将每个组件从pom.xml转换为build.gradle文件。
    1. Base build.gradle template file

      基本的build.gradle模板文件
    2. 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.

      我们将以此为基础构建文件,并用所需的元素逐一更新它。

    3. As it is a Spring MVC web application, we need to create WAR file. So Please apply war plugin and define war attributes as shown below.

      由于它是Spring MVC Web应用程序 ,因此我们需要创建WAR文件。 因此,请应用war插件并定义war属性,如下所示。
    4. Also Provide war file name and it’s version.

      还提供war文件名及其版本。

      apply plugin: "war"
      war {
          baseName = 'SpringMVCExample'
          version = '1.0.0-BUILD-SNAPSHOT'
      }
    5. Add Spring Dependencies as shown below

      添加Spring依赖关系,如下所示
    6. 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")
      }
    7. Add rest of our application Dependencies as shown below

      添加其余的应用程序依赖项,如下所示
    8. 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")
      }
    9. Add our Application Unit Test dependencies as shown below.

      添加我们的应用程序单元测试依赖项,如下所示。
    10. 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")
    11. Our Application complete build.gradle dependencies are shown below.

      我们的应用程序完整的build.gradle依赖关系如下所示。
    12. 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") 
      }
    13. Final project structure will looks like below image now.

      现在,最终的项目结构如下图所示。
    14. Open command prompt at Project Root Directory to execute gradle commands.

      在项目根目录中打开命令提示符以执行gradle命令。
    15. Now build war file by executing the following gradle commands.

      现在,通过执行以下gradle命令来构建war文件。
    16. 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。

    17. Observe our newly created war file at ${PROJECT_ROOT_DIR}/build/libs/ folder as shown below.

      观察我们新创建的war文件,位于$ {PROJECT_ROOT_DIR} / build / libs /文件夹,如下所示。
    18. 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

测试Spring MVC Gradle应用程序 (Test Spring MVC Gradle Application)

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
  1. Copy our war file “SpringMVCExample-1.0.0-BUILD-SNAPSHOT.war” to ${TOMCAT8_HOME}/webapps/ folder

    将我们的战争文件“ SpringMVCExample-1.0.0-BUILD-SNAPSHOT.war”复制到$ {TOMCAT8_HOME} / webapps /文件夹中
  2. Open CMD Prompt at ${TOMCAT8_BIN} and start Tomcat server.

    在$ {TOMCAT8_BIN}中打开CMD Prompt,然后启动Tomcat服务器。
  3. D:\apache-tomcat-8.0.24\bin>startup.bat
  4. Test our application

    测试我们的应用
  5. 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:

    提供用户名,单击“登录”按钮,然后观察下一页,如下所示:

Gradle和Maven项目之间的观察 (Observations between Gradle and Maven Projects)

If we observe our both SpringMVCExample Maven and Gradle based projects, we can observe the following things.

如果我们同时观察基于SpringMVCExample Maven和Gradle的项目,则可以观察到以下内容。

  1. We are getting same war file and output.

    我们将获得相同的战争文件和输出。
  2. Maven’s build.xml is XML file where as Gradle’s build.gradle file is plain text file.

    Maven的build.xml是XML文件,而Gradle的build.gradle文件是纯文本文件。
  3. Maven’s build.xml is too big and contains lot of XML configuration. Gradle’s build.gradle contains very minimal configuration.

    Maven的build.xml太大,并且包含许多XML配置。 Gradle的build.gradle包含非常少的配置。
  4. Gradle’s build.gradle file contains less configuration and also very light weight build script file.

    Gradle的build.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构建工具开发任何应用程序。

翻译自: https://www.journaldev.com/8398/spring-mvc-gradle

spring和spring

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值