2007年5月27日 星期日

使用Maven2佈署wicket-examples

最近打算開始玩Wicket,最好的參考當然是Wicket自帶的wicket-examples。
不過我早已習慣使用Maven2進行所有Project的開發,如何將Wicket的Examples直接拿進Maven使用呢?


其本上Maven2的pom.xml檔設置如下:
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>arch.webapps</groupId>
<artifactId>arch-pom-webapps</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<description>
Archetype Web Wicket
進行Jetty測試:
mvn package jetty:run
本範例將會連同wicket-examples一同進行deploy供開發參考。
wicket-examples原始碼,請參考target/wicket-examples
連結網址:
http://localhost:9876/wicket
http://localhost:9876/wicket-examples
</description>

<artifactId>archetype-web-wicket</artifactId>
<packaging>war</packaging>
<name>Arch WebApp Archetype Wicket Project</name>

<!-- 定義在maven2使用的相關參數,在此所指定的參數,下面都有用到 -->
<properties> <web.port>9876</web> <wicket.examples.context.name>wicket-examples</wicket.examples.context.name> <wicket.examples.context.path>${project.build.directory}/${wicket.examples.context.name}</wicket.examples.context.path> </properties>

<build>
<finalName>wicket</finalName>
<plugins>
<!-- 使用
maven-dependency-plugin當maven在process-resources階段時,
進行
dependency:unpack這個Goal,自動下載wicket-examples這個artifact。
並且解開
wicket-examples這個war檔。將解壓縮的資料置於outputDirectory下。 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack-wicket-examples</id>
<phase>process-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>wicket</groupId>
<artifactId>wicket-examples</artifactId>
<version>${wicket.version}</version>
<type>war</type>
<outputDirectory>${wicket.examples.context.path}</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>


<!-- 當使用mvn jetty:run時會使用
maven-jetty-plugin。
這邊定義了進行佈署的
contextPath為上面設定的finalName
而除了佈署目前專案的war之外還需要佈署wicket-examples這個context
在jetty中,佈署多個context必須設定於jetty.xml中。
本範例中也指定了Server起來後的port為9876
-->
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<configuration>
<contextPath>/${project.build.finalName}</contextPath> <jettyConfig>${project.build.directory}/classes/config/maven/jetty.xml</jettyConfig> <connectors> <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector"> <port>${web.port}</port> </connector> </connectors>
</configuration>
</plugin>

</plugins>
</build>
</project>

本範例用的jetty.xml設置如下:

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN"
"http://jetty.mortbay.org/configure.dtd">
<Configure id="Server" class="org.mortbay.jetty.Server">
<Set name="handlers">
<Array type="org.mortbay.jetty.Handler">
<!-- ======================================================= -->
<!-- Configure a test web application with web.xml -->
<!-- ======================================================= -->
<Item>
<New id="pageWebAppContext" class="org.mortbay.jetty.webapp.WebAppContext">
<Set name="contextPath">/${wicket.examples.context.name}</Set> <Set name="war">${wicket.examples.context.path}</Set>
</New>
</Item>
</Array>
</Set>
</Configure>

注意:
contextPath與war屬性的設置,我是使用maven2本身的filtering功能進行替代。

當所有設定皆完成後。可直接運行mvn package jetty:run
將會進行專案的封裝並執行jetty http server。
之後可直接進行

自行建置的專案 http://localhost:9876/wicket
下載下來的範例 http://localhost:9876/wicket-examples

沒有留言: