Ant - Spring MVCとWARファイルの例

Ant – Spring MVCおよびWARファイルの例

このチュートリアルでは、Antビルドスクリプトを使用して、Spring MVC Webアプリケーションプロジェクトを管理し、WARファイルを作成し、Tomcatにデプロイする方法を示します。

使用される技術:

  1. Eclipse 4.2

  2. JDK 1.7

  3. Ant 1.9.4

  4. アントアイビー2.4

  5. logback 1.1.2

  6. jstl 1.2

  7. Spring 4.1.3.RELEASE

  8. Tomcat 7

1. プロジェクトディレクトリ

最終的なプロジェクト構造を確認します。

Ant-SpringMVC-Project

  1. リソース– Javaソースコードに必要なxmlおよびプロパティファイルを配置します。 log4j.properties、logback.xml、db.propertiesなどのように

  2. src – Javaソースコードを配置します。

  3. target –最終的なWARファイルはここに保存されます。

  4. war –ウェブ関連のもの。

  5. war/WEB-INF – A standard WEB-INF directory structure for WAR file.

  6. war/resources – For css and js files

2. プロジェクトの依存関係

Ivyを使用して、プロジェクトの依存関係をダウンロードします。

ivy.xml


    

    
    
        
        
        
    

    
        
        
        
        
        
        
        
        
    

3. Antビルドスクリプト

このSpring MVCプロジェクトを管理するAntスクリプトは、一目瞭然の称賛を読んでください。

build.xml


    
        Create a Spring MVC (WAR) with Ant build script
    

    
    
    
    
    
    
    
    
    
    

    
    
        
        

        
        
        

    

    
    
        
        
    
    

    
    
        
        
            
        
    

    
    
        
            
                
                
            
        
    

    
    

        
        

        

            
            
            
        
    

    
    
        
        
        
    

    
    
        
        
    

    

4. Spring MVC、JSPおよびXMLファイル

興味がある場合に備えて、完全なJava、XML、およびJSPファイルを以下に示します。

4.1 Spring MVC controller class.

WelcomeController.java

package com.example.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class WelcomeController {

    private static final String VIEW_INDEX = "index";
    private final static Logger logger = LoggerFactory.getLogger(WelcomeController.class);

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String welcome(ModelMap model) {

        model.addAttribute("message", "");
        logger.debug("[welcome]");

        return VIEW_INDEX;

    }

    @RequestMapping(value = "/{name}", method = RequestMethod.GET)
    public String welcomeName(@PathVariable String name, ModelMap model) {

        model.addAttribute("message", "Welcome " + name);
        logger.debug("[welcomeName] name : {}", name);
        return VIEW_INDEX;

    }

}

4.2 Simple logback configuration file, for logging.

logback.xml



    
      

        
            ANT + LogBack : %-5level %logger{36} - %msg%n
        

      
    

    
      
    

4.3 JSP and CSS file.

war/WEB-INF/pages/index.jsp

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>







Ant + Spring MVC Web Project Example

Message : ${message}

war/resources/theme1/css/core.css

h1 {
    font-size: 50px;
}

p {
    font-size: 20px;
    border: 5px solid red;
    padding:10px;
}

4.4 Spring Configuration.

mvc-dispatcher-servlet.xml



    

    
        
            /WEB-INF/pages/
        
        
            .jsp
        
    

    
    

    

4.5 A standard web.xml, integrate with Spring container.

web.xml



    Ant + Spring MVC Web Application

    
        mvc-dispatcher
        org.springframework.web.servlet.DispatcherServlet
        1
    

    
        mvc-dispatcher
        /
    

    
        contextConfigLocation
        /WEB-INF/mvc-dispatcher-servlet.xml
    

    
        org.springframework.web.context.ContextLoaderListener
    

5. 実行とテスト

5.1 Default, it will run the package task.

$ pwd
/Users/example/Documents/workspace/AntSpringMVC

$ ant

Buildfile: /Users/example/Documents/workspace/AntSpringMVC/build.xml

clean:
   [delete] Deleting directory /Users/example/Documents/workspace/AntSpringMVC/war/WEB-INF/classes
   [delete] Deleting directory /Users/example/Documents/workspace/AntSpringMVC/target

init:
    [mkdir] Created dir: /Users/example/Documents/workspace/AntSpringMVC/target
    [mkdir] Created dir: /Users/example/Documents/workspace/AntSpringMVC/war/WEB-INF/classes

resolve:
     [echo] Getting dependencies...
[ivy:retrieve] :: Apache Ivy 2.4.0-rc1 - 20140315220245 :: http://ant.apache.org/ivy/ ::
[ivy:retrieve] :: loading settings :: url = jar:file:/Users/example/.ant/lib/ivy.jar!/org/apache/ivy/core/settings/ivysettings.xml
[ivy:retrieve] :: resolving dependencies :: org.apache#WebProject;[email protected]
[ivy:retrieve]  confs: [compile, runtime, test]
[ivy:retrieve]  found org.slf4j#slf4j-api;1.7.6 in public
[ivy:retrieve]  found jstl#jstl;1.2 in public
[ivy:retrieve]  found ch.qos.logback#logback-classic;1.1.2 in public
[ivy:retrieve]  found ch.qos.logback#logback-core;1.1.2 in public
[ivy:retrieve]  found org.springframework#spring-core;4.1.3.RELEASE in public
[ivy:retrieve]  found commons-logging#commons-logging;1.2 in public
[ivy:retrieve]  found org.springframework#spring-beans;4.1.3.RELEASE in public
[ivy:retrieve]  found org.springframework#spring-context;4.1.3.RELEASE in public
[ivy:retrieve]  found org.springframework#spring-aop;4.1.3.RELEASE in public
[ivy:retrieve]  found aopalliance#aopalliance;1.0 in public
[ivy:retrieve]  found org.springframework#spring-expression;4.1.3.RELEASE in public
[ivy:retrieve]  found org.springframework#spring-web;4.1.3.RELEASE in public
[ivy:retrieve]  found org.springframework#spring-webmvc;4.1.3.RELEASE in public
[ivy:retrieve] :: resolution report :: resolve 974ms :: artifacts dl 19ms
    ---------------------------------------------------------------------
    |                  |            modules            ||   artifacts   |
    |       conf       | number| search|dwnlded|evicted|| number|dwnlded|
    ---------------------------------------------------------------------
    |      compile     |   13  |   0   |   0   |   0   ||   13  |   0   |
    |      runtime     |   13  |   0   |   0   |   0   ||   13  |   0   |
    |       test       |   13  |   0   |   0   |   0   ||   13  |   0   |
    ---------------------------------------------------------------------
[ivy:retrieve] :: retrieving :: org.apache#WebProject
[ivy:retrieve]  confs: [compile, runtime, test]
[ivy:retrieve]  2 artifacts copied, 11 already retrieved (682kB/31ms)

compile:
    [javac] Compiling 1 source file to /Users/example/Documents/workspace/AntSpringMVC/war/WEB-INF/classes

copy-resources:
     [copy] Copying 1 file to /Users/example/Documents/workspace/AntSpringMVC/war/WEB-INF/classes

package:
[ivy:retrieve] :: retrieving :: org.apache#WebProject
[ivy:retrieve]  confs: [runtime]
[ivy:retrieve]  0 artifacts copied, 13 already retrieved (0kB/11ms)
      [war] Building war: /Users/example/Documents/workspace/AntSpringMVC/target/helloproject.war

main:

BUILD SUCCESSFUL
Total time: 3 seconds

上記の結果は、開発環境によって異なる場合があります。 ビルドが成功すると、最終的なWARファイルがtargetフォルダーに作成されます。

5.2 Review the content of the generated WAR file – helloproject.war

example-mac:AntSpringMVC example$ tar -tvf target/helloproject.war
drwxrwxrwx  0 0      0           0 Dec 26 16:55 META-INF/
-rwxrwxrwx  0 0      0         103 Dec 26 16:55 META-INF/MANIFEST.MF
drwxrwxrwx  0 0      0           0 Dec 26 16:55 WEB-INF/
-rwxrwxrwx  0 0      0         897 Dec 24 15:05 WEB-INF/web.xml
drwxrwxrwx  0 0      0           0 Dec 26 16:55 WEB-INF/classes/
drwxrwxrwx  0 0      0           0 Dec 26 16:55 WEB-INF/classes/com/
drwxrwxrwx  0 0      0           0 Dec 26 16:55 WEB-INF/classes/com/example/
drwxrwxrwx  0 0      0           0 Dec 26 16:55 WEB-INF/classes/com/example/controller/
drwxrwxrwx  0 0      0           0 Dec 24 17:05 WEB-INF/lib/
drwxrwxrwx  0 0      0           0 Dec 24 15:04 WEB-INF/pages/
-rwxrwxrwx  0 0      0        1903 Dec 26 16:55 WEB-INF/classes/com/example/controller/WelcomeController.class
-rwxrwxrwx  0 0      0         372 Dec 26 16:55 WEB-INF/classes/logback.xml
-rwxrwxrwx  0 0      0        1030 Dec 25 13:41 WEB-INF/mvc-dispatcher-servlet.xml
-rwxrwxrwx  0 0      0         311 Dec 25 13:44 WEB-INF/pages/index.jsp
-rwxrwxrwx  0 0      0        4467 Aug  1  2005 WEB-INF/lib/aopalliance-1.0.jar
-rwxrwxrwx  0 0      0       61829 Jul  6 02:12 WEB-INF/lib/commons-logging-1.2.jar
-rwxrwxrwx  0 0      0      414240 Jul 22  2006 WEB-INF/lib/jstl-1.2.jar
-rwxrwxrwx  0 0      0      270750 Apr  3  2014 WEB-INF/lib/logback-classic-1.1.2.jar
-rwxrwxrwx  0 0      0      427729 Apr  3  2014 WEB-INF/lib/logback-core-1.1.2.jar
-rwxrwxrwx  0 0      0       28688 Feb  6  2014 WEB-INF/lib/slf4j-api-1.7.6.jar
-rwxrwxrwx  0 0      0      359018 Dec  9 18:48 WEB-INF/lib/spring-aop-4.1.3.RELEASE.jar
-rwxrwxrwx  0 0      0      707820 Dec  9 18:48 WEB-INF/lib/spring-beans-4.1.3.RELEASE.jar
-rwxrwxrwx  0 0      0     1026840 Dec  9 18:48 WEB-INF/lib/spring-context-4.1.3.RELEASE.jar
-rwxrwxrwx  0 0      0     1006709 Dec  9 18:48 WEB-INF/lib/spring-core-4.1.3.RELEASE.jar
-rwxrwxrwx  0 0      0      259164 Dec  9 18:48 WEB-INF/lib/spring-expression-4.1.3.RELEASE.jar
-rwxrwxrwx  0 0      0      713531 Dec  9 18:48 WEB-INF/lib/spring-web-4.1.3.RELEASE.jar
-rwxrwxrwx  0 0      0      781676 Dec  9 18:48 WEB-INF/lib/spring-webmvc-4.1.3.RELEASE.jar
drwxrwxrwx  0 0      0           0 Dec 26 16:48 resources/
drwxrwxrwx  0 0      0           0 Dec 25 13:30 resources/theme1/
drwxrwxrwx  0 0      0           0 Dec 25 13:27 resources/theme1/css/
-rwxrwxrwx  0 0      0          88 Dec 25 13:45 resources/theme1/css/core.css

すべてのファイルはプロパティに配置されます。デプロイするには、helloproject.warをコピーして、$Tomcat/webappsフォルダーに配置します。

ant-spring-mvc-example1

ant-spring-mvc-example2

Note
フォローアップ:how to debug this ant-ivy project in Eclipse IDE

ソースコードをダウンロード

ダウンロード–Ant-SpringMVC-Example.zip(26 KB)