A 類別程式中, f() 及 g() 這二個方法, 因宣告為 private, 在繼承的關係中是不會被繼承, 所以根本沒覆蓋的討論, 下面程式範例, 編譯會過, 既使 B 類別程式中有相同宣告的 f() 及 g() 這二個方法
class A {
// Identical to "private" alone:
private final void f() {
System.out.println("A.f()");
}
// Also automatically "final":
private void g() {
System.out.println("A.g()");
}
}
class B extends A {
private final void f() {
System.out.println("B.f()");
}
private void g() {
System.out.println("B.g()");
}
}
在繼承關係中, 變數是沒有覆蓋 (Override) 的討論, 而是以 shadow 來討論, 以下範例程式, 編譯是會通過, 因沒有覆蓋討論, I 介面中的變數 KEY 既使宣告 final, A 類別一樣可以宣告同名的變數 KEY.
public interface I {
public final String KEY = "a";
}
public class A implements I {
public String KEY = "b";
public String getKey() {
return KEY;
}
}
參考文章
1. Why can final constants in Java beoverriden ?
http://stackoverflow.com/questions/205239/why-can-final-constants-in-java-be-overriden
2011年8月16日 星期二
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
$ 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>
$ 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>
$ 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>
2011年8月5日 星期五
Apache Maven 專案管理平台 - Java 專案建立與執行
初設專案
Maven 專案命令是 mvn, mvn 主命令參數 archetype:generate 會根據 archetypeArtifactId 這參數, 來決定要初設何種專案.
Maven 小組提供一個專案模版網站, 網址 : http://www.mvnbrowser.com/index.html
初設 maven-archetype-quickstart 專案, 命令如下 :
Maven 專案命令是 mvn, mvn 主命令參數 archetype:generate 會根據 archetypeArtifactId 這參數, 來決定要初設何種專案.
Maven 小組提供一個專案模版網站, 網址 : http://www.mvnbrowser.com/index.html
初設 maven-archetype-quickstart 專案, 命令如下 :
$ mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
:Downloaded: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-interactivity-api/1.0-alpha-5/plexus-interactivity-api-1.0-alpha-5.jar (14 KB at 6.6 KB/sec)
Downloaded: http://repo1.maven.org/maven2/org/apache/maven/shared/maven-invoker/2.0.10/maven-invoker-2.0.10.jar (28 KB at 12.8 KB/sec)
Downloaded: http://repo1.maven.org/maven2/commons-collections/commons-collections/3.2.1/commons-collections-3.2.1.jar (562 KB at 97.0 KB/sec)
[INFO] Generating project in Batch mode
Downloading: http://repo1.maven.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.jar
Downloaded: http://repo1.maven.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.jar (5 KB at 7.4 KB/sec)
Downloading: http://repo1.maven.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.pom
Downloaded: http://repo1.maven.org/maven2/org/apache/maven/archetypes/maven-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.pom (703 B at 1.2 KB/sec)
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.mycompany.app
[INFO] Parameter: packageName, Value: com.mycompany.app
[INFO] Parameter: package, Value: com.mycompany.app
[INFO] Parameter: artifactId, Value: my-app
[INFO] Parameter: basedir, Value: /mnt/hda1/apache-maven-3.0.3
[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/apache-maven-3.0.3/my-app
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1:35.260s
[INFO] Finished at: Sat Aug 06 09:14:53 UTC 2011
[INFO] Final Memory: 7M/18M
[INFO] ------------------------------------------------------------------------
上述命令, 會下載 archetypeArtifactId 參數所指 maven-archetype-quickstart 專案, 所需的所有檔案 (*.jar, *.pom) 至指定的儲存目錄, 並且會根據 artifactId 參數, 產生 my-app 目錄
檢視儲存目錄內容, 如下 :
drwxr-xr-x 14 root root 4096 Aug 6 09:14 .
drwxr-xr-x 10 root root 4096 Aug 6 07:43 ..
drwxr-xr-x 3 root root 4096 Aug 6 09:14 classworlds
drwxr-xr-x 3 root root 4096 Aug 6 09:14 commons-cli
drwxr-xr-x 3 root root 4096 Aug 6 09:14 commons-collections
drwxr-xr-x 3 root root 4096 Aug 6 09:14 commons-io
drwxr-xr-x 3 root root 4096 Aug 6 09:14 commons-lang
drwxr-xr-x 3 root root 4096 Aug 6 09:14 dom4j
drwxr-xr-x 3 root root 4096 Aug 6 09:14 jdom
drwxr-xr-x 3 root root 4096 Aug 6 09:14 junit
drwxr-xr-x 3 root root 4096 Aug 6 09:14 net
drwxr-xr-x 4 root root 4096 Aug 6 09:13 org
drwxr-xr-x 3 root root 4096 Aug 6 09:14 oro
drwxr-xr-x 3 root root 4096 Aug 6 09:14 xml-apis
my-app 目錄結構, 如下 :
my-app |-- pom.xml `-- src |-- main | `-- java | `-- com | `-- mycompany | `-- app | `-- App.java `-- test `-- java `-- com `-- mycompany `-- app `-- AppTest.java
在 my-app 目錄中的 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>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>my-app</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
專案建立
$ cd my-app # 一定要執行此命令
$ mvn package
:
:
xus-io-1.0.pom
Downloaded: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-io/1.0/plexus-io-1.0.pom (2 KB at 2.1 KB/sec)
Downloading: http://repo1.maven.org/maven2/org/apache/maven/maven-archiver/2.4.1/maven-archiver-2.4.1.jar
Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-archiver/1.0/plexus-archiver-1.0.jar
Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-io/1.0/plexus-io-1.0.jar
Downloaded: http://repo1.maven.org/maven2/org/apache/maven/maven-archiver/2.4.1/maven-archiver-2.4.1.jar (20 KB at 16.3 KB/sec)
Downloaded: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-io/1.0/plexus-io-1.0.jar (50 KB at 19.8 KB/sec)
Downloaded: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-archiver/1.0/plexus-archiver-1.0.jar (174 KB at 46.9 KB/sec)
[INFO] Building jar: /mnt/hda1/my-app/target/my-app-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1:20.944s
[INFO] Finished at: Sat Aug 06 10:20:02 UTC 2011
[INFO] Final Memory: 10M/25M
[INFO] ------------------------------------------------------------------------
執行專案
$ java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App
Hello World!
參考文章
1. Maven in 5 Minutes
http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
Apache Maven 專案管理平台 - 安裝
Apache Maven 是一個 Java 的專案管理平台,就好像 C 語言中的 make 與 Java 的 Ant 等專案產生器一樣。
Apache Maven 官方網址 : http://maven.apache.org/
Apache Maven 官方網址 : http://maven.apache.org/
本系列文章的實作, 均使用 TinyCore 這個發行套件, 為使實作順利,
還需事先安裝以下套件 :
$ tce-load -wi tree.tcz # 列出目錄架構
$ tce-load -wi curl.tcz # 文字模式 WWW 瀏覽器
如需更多 TinyCore 操作與設定, 請參考 : http://linuxkvm.blogspot.com/
安裝 Apache Maven
1. 下載 maven
2. 解壓縮 apache-maven-3.0.x-bin.zip
3. 設定 M2_HOME 環境變數
Linux Operating Systems
export M2_HOME=/usr/local/apache-maven-3.0.x
Windows 2000/XP
set M2_HOME="c:\program files\apache-maven-3.0.x
4. 設定 PATH 環境變數
Linux Operating Systems
export PATH=$M2_HOME/bin:$PATH
Windows 2000/XP
set PATH="%M2_HOME%\bin";%PATH%
5. 測試
$ mvn -version
Apache Maven 3.0.3 (r1075438; 2011-02-28 17:31:09+0000)
Maven home: /mnt/hda1/apache-maven-3.0.3
Java version: 1.7.0, vendor: Oracle Corporation
Java home: /mnt/hda1/jdk1.7.0/jre
:6. 設定下載套件 (*.jar) 的儲存位址
修改 %M2_HOME%\conf\settings.xml 檔案中的 <localRepository>
標籤內容, 內定儲存位址 ${user.home}/.m2
$ nano %M2_HOME%/conf/settings
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd"> <!-- localRepository | The path to the local repository maven will use to store artifacts. | | Default: ~/.m2/repository --> <localRepository>${env.M2_HOME}\mavenrepo</localRepository>
訂閱:
文章 (Atom)