web.xml詳解
1.在啓動Web項目時,容器(比如Tomcat)會讀web.xml配置文件中的兩個節點<listener>和<contex-param>。
2.接着容器會創建一個ServletContext(上下文),應用範圍內即整個WEB項目都能使用這個上下文。
3.接着容器會將讀取到<context-param>轉化爲鍵值對,並交給ServletContext。
4.容器創建<listener></listener>中的類實例,即創建監聽(備註:listener定義的類可以是自定義的類但必須需要繼承ServletContextListener)。
5.在監聽的類中會有一個contextInitialized(ServletContextEvent event)初始化方法,在這個方法中可以通過event.getServletContext().getInitParameter("contextConfigLocation") 來得到context-param 設定的值。在這個類中還必須有一個contextDestroyed(ServletContextEvent event) 銷燬方法.用於關閉應用前釋放資源,比如說數據庫連接的關閉。
6.得到這個context-param的值之後,你就可以做一些操作了.注意,這個時候你的WEB項目還沒有完全啓動完成.這個動作會比所有的Servlet都要早。
pom.xml詳解
1.
<modelVersion>4.0.0</modelVersion>
當前POM模型的版本,對於maven2及maven3來說,它只能是4.0.0。
2.
<parent>
項目或者組織的唯一標誌,並且配置時生成的路徑也是由此生成,如org.codehaus.mojo生成的相對路徑爲:/org/codehaus/mojo
<groupId>com.liuli.pro</groupId>
項目的通用名稱
<artifactId>syspro</artifactId>
項目的版本
<version>0.0.1-SNAPSHOT</version>
</parent>
3.當前項目的名稱
<artifactId>syspro-web</artifactId>
4.打包的機制,如pom, jar, maven-plugin, ejb, war, ear, rar, par
<packaging>war</packaging>
5.<name>mfts-web Maven Webapp</name>
6.profile可以讓我們定義一系列的配置信息,然後指定其激活條件。這樣我們就可以定義多個profile,
然後每個profile對應不同的激活條件和配置信息,從而達到不同環境使用不同配置信息的效果。比如說,
我們可以通過profile定義在jdk1.5以上使用一套配置信息,在jdk1.5以下使用另外一套配置信息;
或者有時候我們可以通過操作系統的不同來使用不同的配置信息,比如windows下是一套信息
,linux下又是另外一套信息,等等。具體的激活條件有哪些我在後文會講到。
<profiles>
<profile>
<id>dev</id>
<properties>
<target.dist>dev</target.dist>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<target.dist>test</target.dist>
</properties>
</profile>
<profile>
<id>product</id>
<properties>
<target.dist>product</target.dist>
</properties>
</profile>
<profile>
<id>learning</id>
<properties>
<target.dist>learning</target.dist>
</properties>
</profile>
<profile>
<id>personal</id>
<!-- 默認激活開發配製,使用config-learning.properties來替換設置過慮的資源文件中的${key} -->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<target.dist>personal</target.dist>
</properties>
</profile>
</profiles>
7.jar版本
<properties>
<slf4j.version>1.6.4</slf4j.version>
</properties>
8.build 創建項目時必須的信息。
<build> build開始
resources: resource的列表,用於包括所有的資源
<resources>
<resource>
資源所在的位置
<directory>src/main/java</directory>
樣式,包括那些資源
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
排除的資源
<excludes>
<exclude>*.sql</exclude>
<exclude>log4j.xml</exclude>
<exclude>logback.xml</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>log4j.xml</include>
<include>logback.xml</include>
<include>jdbc.properties</include>
<include>common.properties</include>
</includes>
是否替換資源中的屬性placehold
<filtering>true</filtering>
</resource>
</resources>
9.
<filters>
定義過濾,用於替換相應的屬性文件,使用maven定義的屬性。設置所有placehold的值
<filter>src/main/resources/config/config-${target.dist}.properties</filter>
</filters>
生成最後的文件的樣式
<finalName>mfts-web-${target.dist}</finalName>
10.plugins
<plugins>
<plugin>
的唯一的標識符
爲了使項目結構更爲清晰,Maven區別對待Java代碼文件和資源文件,maven-compiler-plugin用來編譯Java代碼,maven-resources-plugin則用來處理資源文件。
默認的主資源文件目錄是src/main/resources,很多用戶會需要添加額外的資源文件目錄,這個時候就可以通過配置maven-resources-plugin來實現。
此外,資源文件過濾也是Maven的一大特性,你可以在資源文件中使用${propertyName}形式的Maven屬性,然後配置maven-resources-plugin開啓對資源文件的過濾,
之後就可以針對不同環境通過命令行或者Profile傳入屬性的值,以實現更爲靈活的構建。
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
用於配置execution目標,一個插件可以有多個目標。
<executions>
<execution>
<id>copy-resources</id>
表示階段,目標將會在什麼階段執行
<phase>validate</phase>
表示目標
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>src/main/webapp/WEB-INF/</outputDirectory>
<resources>
<resource>
<directory>src/main/resources/fis/</directory>
<filtering>true</filtering>
</resource>
</resources>
<filters>
<filter>src/main/resources/config/config-${target.dist}.properties</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
build完成
</build>
借鑑:
http://www.infoq.com/cn/news/2011/05/xxb-maven-8-plugin/
http://www.cnblogs.com/davidwang456/p/3915031.html
http://blog.csdn.net/eclipser1987/article/details/5755603
http://blog.csdn.net/carolzhang8406/article/details/13622287