初設 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
2011年8月6日 星期六
訂閱:
張貼留言 (Atom)
No Response to "Apache Maven 專案管理平台 - WAR 專案加入 Servlet"
張貼留言