本系列旨在詳細記錄如何利用 Maven 2、MyEclipse 6.5、MySQL 5、JDK 1.6 等工具,從零開始構建一個基於 Struts 2、Hibernate 3、Spring 2.5(SSH)的企業級 Web 應用專案。首篇將建立標準 Maven 目錄結構,配置編譯插件與基礎依賴,並導入 MyEclipse 進行後續開發。
mvn archetype:create \
-DgroupId=net.selitech.ssim \
-DartifactId=ssim \
-DarchetypeArtifactId=maven-archetype-webapp
生成的目錄僅包含 src/main/webapp 與 pom.xml,需手動補全 Java 源碼與測試目錄:
cd ssim/src
mkdir -p main/java test/java test/resources
關鍵配置點:
packaging=war、finalName=${artifactId}。maven-compiler-plugin 設定 source/target=1.6、encoding=UTF-8。maven-surefire-plugin 跳過測試(skipTests=true),後續單獨運行。maven-war-plugin 指定 webappDirectory 與 warSourceDirectory 為 src/main/webapp。junit:junit:4.5 (test scope),SSH 相關依賴將在後續篇章陸續添加。mvn eclipse:eclipse
導入後,需修改兩個隱藏配置文件以適配 MyEclipse Web 專案識別:
<?xml version="1.0" encoding="UTF-8"?>
<project-module type="WEB" name="ssim" id="myeclipse.1234059775961"
context-root="/ssim" j2ee-spec="1.4" archive="ssim.war">
<attributes>
<attribute name="webrootdir" value="src/main/webapp" />
</attributes>
</project-module>
添加 Maven 2 Nature (org.maven.ide.eclipse.maven2Nature) 與 Web Nature (com.genuitec.eclipse.j2eedt.core.webnature),並包含 Java Builder、JS Validator、J2EE Validator 等構建命令。
在 MyEclipse 中右鍵專案 → Run As → Maven install,控制台輸出 BUILD SUCCESSFUL,目標目錄生成 ssim.war,標準 Maven Web 專案骨架建立完成。後續將在此基礎上整合 Spring、Hibernate、Struts 2 等框架。