顯示具有 war 標籤的文章。 顯示所有文章
顯示具有 war 標籤的文章。 顯示所有文章

2011年8月6日 星期六

Apache Maven 專案管理平台 - WAR 專案加入 Servlet

初設 AServlet 專案

$ mvn archetype:create -DgroupId=kvm.servlet -DartifactId=AServlet -DarchetypeArtifactId=maven-archetype-webapp

AServlet 專案目錄結構
$ tree AServlet/
AServlet/
|-- pom.xml
`-- src
`-- main
|-- resources
`-- webapp
|-- WEB-INF
| `-- web.xml
`-- index.jsp

5 directories, 3 files

產生 kvm.servlet 套件目錄

$ mkdir -p AServlet/src/main/java/kvm/servlet

撰寫 SimpleServlet.java 程式

$ nano AServlet/src/main/java/kvm/servlet/SimpleServlet.java
package kvm.servlet;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class SimpleServlet extends HttpServlet {
   public void doGet(HttpServletRequest request, 
                               HttpServletResponse response)
                               throws ServletException, IOException {
      PrintWriter out = response.getWriter();
      out.println( "SimpleServlet Executed" );
      out.flush();
      out.close();
   }
}

修改 AServlet 網站專案設定檔 - web.xml

$ nano AServlet/src/main/webapp/WEB-INF/web.xml

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>
    <servlet>
       <servlet-name>simple</servlet-name>
       <servlet-class>kvm.servlet.SimpleServlet</servlet-class>
    </servlet>
    <servlet-mapping>
       <servlet-name>simple</servlet-name>
       <url-pattern>/simple</url-pattern>
    </servlet-mapping>
</web-app>


第一次編譯 Servlet 程式
$ cd AServlet/
$ mvn compile
                                  :
[ERROR] /mnt/hda1/AServlet/src/main/java/kvm/servlet/SimpleServlet.java:[5,0] error: package javax.servlet.http does not exist
[ERROR] /mnt/hda1/AServlet/src/main/java/kvm/servlet/SimpleServlet.java:[7,35] error: cannot find symbol
[ERROR] class HttpServlet
[ERROR] /mnt/hda1/AServlet/src/main/java/kvm/servlet/SimpleServlet.java:[8,22] error: cannot find symbol
[ERROR] class SimpleServlet
[ERROR] /mnt/hda1/AServlet/src/main/java/kvm/servlet/SimpleServlet.java:[8,50] error: cannot find symbol
[ERROR] class SimpleServlet
[ERROR] /mnt/hda1/AServlet/src/main/java/kvm/servlet/SimpleServlet.java:[9,41] error: cannot find symbol
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

修改 pom.xml 設定檔

$ nano pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">

   <modelVersion>4.0.0</modelVersion>
   <groupId>kvm.servlet</groupId>
   <artifactId>AServlet</artifactId>
   <packaging>war</packaging>
   <version>1.0-SNAPSHOT</version>
   <name>AServlet Maven Webapp</name>
   <url>http://maven.apache.org</url>
   <dependencies>
      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>3.8.1</version>
         <scope>test</scope>
      </dependency>

      <dependency>
         <groupId>javax.servlet</groupId>
         <artifactId>servlet-api</artifactId>
         <version>2.4</version>
         <scope>provided</scope>
      </dependency>

   </dependencies>
   <build>
      <finalName>AServlet</finalName>
    </build>
</project>

再次編譯 servlet 程式
$ mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building AServlet Maven Webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: http://repo1.maven.org/maven2/javax/servlet/servlet-api/2.4/servlet-api-2.4.pom
Downloaded: http://repo1.maven.org/maven2/javax/servlet/servlet-api/2.4/servlet-api-2.4.pom (156 B at 0.0 KB/sec)
Downloading: http://repo1.maven.org/maven2/javax/servlet/servlet-api/2.4/servlet-api-2.4.jar
Downloaded: http://repo1.maven.org/maven2/javax/servlet/servlet-api/2.4/servlet-api-2.4.jar (96 KB at 36.1 KB/sec)
[INFO]
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ AServlet ---
[WARNING] Using platform encoding (ANSI_X3.4-1968 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ AServlet ---
[WARNING] File encoding has not been set, using platform encoding ANSI_X3.4-1968, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /mnt/hda1/AServlet/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.557s
[INFO] Finished at: Sun Aug 07 09:25:48 UTC 2011
[INFO] Final Memory: 6M/17M
[INFO] ------------------------------------------------------------------------

製作 AServlet 專案檔

$ mvn package

上載 AServlet 專案檔至 Tomcat
$ cp target/AServlet.war /mnt/hda1/apache-tomcat-7.0.19/webapps/

SimpleServlet 程式測試
$ curl http://localhost:8080/AServlet/simple
SimpleServlet Executed

Apache Maven 專案管理平台 - WAR 專案加入 JSP

初設 AJSP 專案

$ mvn archetype:create -DgroupId=kvm.jsp -DartifactId=AJSP
 -DarchetypeArtifactId=maven-archetype-webapp


Counter.java 資料物件設計

1.  建立 foo 套件目錄
$ mkdir -p AJSP/src/main/java/foo

2. 撰寫 Counter.java 程式

$ nano AJSP/src/main/java/foo/Counter.java
package foo;
public class Counter {
  private static int count;
  public static synchronized int getCount() {
     count++;
     return count;
  }
}


撰寫 MyJSP01.jsp 
$ nano AJSP/src/main/webapp/MyJSP01.jsp
<html>
<body>
The Page count is :
<%
  out.println(foo.Counter.getCount());
%>
</body>
</html>

製作 AJSP 專案檔 (AJSP.war)
$ cd AJSP
$ mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building AJSP Maven Webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ AJSP ---
[WARNING] Using platform encoding (ANSI_X3.4-1968 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ AJSP ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.4.3:testResources (default-testResources) @ AJSP ---
[WARNING] Using platform encoding (ANSI_X3.4-1968 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /mnt/hda1/AJSP/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ AJSP ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.7.2:test (default-test) @ AJSP ---
[INFO] No tests to run.
[INFO] Surefire report directory: /mnt/hda1/AJSP/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
There are no tests to run.

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-war-plugin:2.1.1:war (default-war) @ AJSP ---
[INFO] Packaging webapp
[INFO] Assembling webapp [AJSP] in [/mnt/hda1/AJSP/target/AJSP]
[INFO] Processing war project
[INFO] Copying webapp resources [/mnt/hda1/AJSP/src/main/webapp]
[INFO] Webapp assembled in [78 msecs]
[INFO] Building war: /mnt/hda1/AJSP/target/AJSP.war
[INFO] WEB-INF/web.xml already added, skipping
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.804s
[INFO] Finished at: Sun Aug 07 10:00:12 UTC 2011
[INFO] Final Memory: 5M/13M
[INFO] ------------------------------------------------------------------------

檢視 AJSP 專案檔製作後, AJSP 的目錄結構
$ tree .
.
|-- pom.xml
|-- src
|   `-- main
|       |-- java
|       |   `-- foo
|       |       `-- Counter.java
|       |-- resources
|       `-- webapp
|           |-- MyJSP01.jsp
|           |-- WEB-INF
|           |   `-- web.xml
|           `-- index.jsp
`-- target
    |-- AJSP
    |   |-- META-INF
    |   |-- MyJSP01.jsp
    |   |-- WEB-INF
    |   |   |-- classes
    |   |   |   `-- foo
    |   |   |       `-- Counter.class
    |   |   `-- web.xml
    |   `-- index.jsp
    |-- AJSP.war
    |-- classes
    |   `-- foo
    |       `-- Counter.class
    |-- maven-archiver
    |   `-- pom.properties
    `-- surefire

17 directories, 12 files

佈署 AJSP 專案至 Tomcat

$ cp target/AJSP.war /mnt/hda1/apache-tomcat-7.0.19/webapps/

啟動 Tomcat
$ /mnt/hda1/apache-tomcat-7.0.19/bin/startup.sh &
[1] 1321
Using CATALINA_BASE: /mnt/hda1/apache-tomcat-7.0.19
Using CATALINA_HOME: /mnt/hda1/apache-tomcat-7.0.19
Using CATALINA_TMPDIR: /mnt/hda1/apache-tomcat-7.0.19/temp
Using JRE_HOME: /mnt/hda1/jdk1.7.0
Using CLASSPATH: /mnt/hda1/apache-tomcat-7.0.19/bin/bootstrap.jar:/mnt/hda1/apache-tomcat-7.0.19/bin/tomcat-juli.jar

執行 MyJSP01.jsp
$ curl http://localhost:8080/AJSP/MyJSP01.jsp
<html>
<body>
The Page count is :
1
</body>
</html>

Apache Maven 專案管理平台 - WAR 專案建立與執行

1. 初設 AAAWebapp 專案

$ mvn  archetype:create  -DgroupId=kvm.aaa  -DartifactId=AAAWebapp  -DarchetypeArtifactId=maven-archetype-webapp
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-archetype-plugin:2.0:create (default-cli) @ standalone-pom ---
[WARNING] This goal is deprecated. Please use mvn archetype:generate instead
[INFO] Defaulting package to group ID: kvm.aaa
Downloading: http://repo1.maven.org/maven2/org/apache/maven/archetypes/maven-archetype-webapp/maven-metadata.xml
Downloaded: http://repo1.maven.org/maven2/org/apache/maven/archetypes/maven-archetype-webapp/maven-metadata.xml (498 B at 0.3 KB/sec)
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-webapp:RELEASE
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: kvm.aaa
[INFO] Parameter: packageName, Value: kvm.aaa
[INFO] Parameter: package, Value: kvm.aaa
[INFO] Parameter: artifactId, Value: AAAWebapp
[INFO] Parameter: basedir, Value: /mnt/hda1
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] ********************* End of debug info from resources from generated POM ***********************
[INFO] project created from Old (1.x) Archetype in dir: /mnt/hda1/AAAWebapp
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.480s
[INFO] Finished at: Sat Aug 06 18:06:32 UTC 2011
[INFO] Final Memory: 7M/18M
[INFO] ------------------------------------------------------------------------

2. AAAWebapp 專案目錄結構

$  tree AAAWebapp/
AAAWebapp/
|-- pom.xml
`-- src
    `-- main
        |-- resources
        `-- webapp
            |-- WEB-INF
            |   `-- web.xml
            `-- index.jsp

5 directories, 3 files

3. 產生 WAR 專案檔

$ cd AAAWebapp/

$ mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building AAAWebapp Maven Webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ AAAWebapp ---
[WARNING] Using platform encoding (ANSI_X3.4-1968 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ AAAWebapp ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-resources-plugin:2.4.3:testResources (default-testResources) @ AAAWebapp ---
[WARNING] Using platform encoding (ANSI_X3.4-1968 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /mnt/hda1/AAAWebapp/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ AAAWebapp ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.7.2:test (default-test) @ AAAWebapp ---
[INFO] No tests to run.
[INFO] Surefire report directory: /mnt/hda1/AAAWebapp/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
There are no tests to run.

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-war-plugin:2.1.1:war (default-war) @ AAAWebapp ---
[INFO] Packaging webapp
[INFO] Assembling webapp [AAAWebapp] in [/mnt/hda1/AAAWebapp/target/AAAWebapp]
[INFO] Processing war project
[INFO] Copying webapp resources [/mnt/hda1/AAAWebapp/src/main/webapp]
[INFO] Webapp assembled in [75 msecs]
[INFO] Building war: /mnt/hda1/AAAWebapp/target/AAAWebapp.war
[INFO] WEB-INF/web.xml already added, skipping
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.140s
[INFO] Finished at: Sat Aug 06 18:12:20 UTC 2011
[INFO] Final Memory: 5M/12M
[INFO] ------------------------------------------------------------------------

4. 佈署 AAAWebapp 專案

$ cp target/AAAWebapp.war /mnt/hda1/apache-tomcat-7.0.19/webapps/

5. 啟動 Tomcat
$ /mnt/hda1/apache-tomcat-7.0.19/bin/startup.sh &
[1] 1321
Using CATALINA_BASE:   /mnt/hda1/apache-tomcat-7.0.19
Using CATALINA_HOME:   /mnt/hda1/apache-tomcat-7.0.19
Using CATALINA_TMPDIR: /mnt/hda1/apache-tomcat-7.0.19/temp
Using JRE_HOME:        /mnt/hda1/jdk1.7.0
Using CLASSPATH:       /mnt/hda1/apache-tomcat-7.0.19/bin/bootstrap.jar:/mnt/hda1/apache-tomcat-7.0.19/bin/tomcat-juli.jar

6. 執行 AAAWebapp 專案

$ curl http://localhost:8080/AAAWebapp/
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>