使用「mvn site-deploy」部署站點(WebDAV例子)


這裡有一個指南,向您展示如何使用「mvn site:deploy」來自動部署生成的文件站點到伺服器,這裡通過WebDAV機制說明。

P.S 在這篇文章中,我們使用的是Apache伺服器2.x的WebDAV功能。

1. 啟用 WebDAV

請參見本指南,了解 如何啟用WebDAV存取Apache 2.x伺服器

2. 組態在何處部署

在 pom.xml 中,組態在 「distributionManagement」 標籤部署你的網站。

<distributionManagement>
    <site>
      <id>yiibaiserver</id>
      <url>dav:http://127.0.0.1/sites/</url>
    </site>
</distributionManagement>

「dav」字首是HTTP協定之前新增的,這意味著通過WebDAV機制部署您的網站。或者,可以用「scp」取代它,如果您的伺服器支援「scp」存取。

告訴Maven來使用「wagon-webdav-jackrabbit」擴充套件部署。

<build>
	<extensions>
		<extension>
			<groupId>org.apache.maven.wagon</groupId>
			<artifactId>wagon-webdav-jackrabbit</artifactId>
			<version>1.0-beta-7</version>
		</extension>
	</extensions>
</build>
wagon-webdav
一些人說可以使用「wagon-webdav」,但這不是我試了不能正常工作,所以這裡用「wagon-webdav-jackrabbit」代替。
<extension>
            <groupId>org.apache.maven.wagon</groupId>
            <artifactId>wagon-webdav</artifactId>
            <version>1.0-beta-2</version>
</extension>

 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.yiibai.core</groupId>
  <artifactId>yiibai-core</artifactId>
  <packaging>jar</packaging>
  <version>1</version>
  <name>yiibai-core</name>
  <url>http://maven.apache.org</url>
  <build>
	<extensions>
		<extension>
			<groupId>org.apache.maven.wagon</groupId>
			<artifactId>wagon-webdav-jackrabbit</artifactId>
			<version>1.0-beta-7</version>
		</extension>
	</extensions>
  </build>
  <distributionManagement>
    <site>
      <id>yiibaiserver</id>
      <url>dav:http://127.0.0.1/sites/</url>
    </site>
  </distributionManagement>
</project>

3. 組態WebDAV身份驗證

通常情況下,WebDAV是需要認證的存取。所以需要把相關的認證細節(使用者名和密碼)%MAVEN_PATH%/conf/settings.xml.

File : settings.xml

<servers>
	<server>
		<id>yiibaiserver</id>
		<username>admin</username>
		<password>123456</password>
	</server>
</servers>
 「yiibaiserver」 是什麼 ?
在Maven的「的settings.xml」檔案伺服器ID將在「的pom.xml」檔案被網站參照。

4. mvn site:deploy

「mvn site:deploy」 命令執行:

C:\worksp\yiibai-core>mvn site:deploy
... ...
Transfer finished. 11622 bytes copied in 0.021 seconds
十一月 03, 2015 9:00:07 下午 org.apache.commons.httpclient.auth.AuthChallengePro
cessor selectAuthScheme
資訊: digest authentication scheme selected
Uploading: .//project-info.html to http://127.0.0.1/sites/

##十一月 03, 2015 9:00:07 下午 org.apache.commons.httpclient.auth.AuthChallengeP
rocessor selectAuthScheme
資訊: digest authentication scheme selected
##http://127.0.0.1/sites//./project-info.html - Status code: 201

Transfer finished. 11170 bytes copied in 0.035 seconds
十一月 03, 2015 9:00:07 下午 org.apache.commons.httpclient.auth.AuthChallengePro
cessor selectAuthScheme
資訊: digest authentication scheme selected
Uploading: .//project-summary.html to http://127.0.0.1/sites/

##十一月 03, 2015 9:00:07 下午 org.apache.commons.httpclient.auth.AuthChallengeP
rocessor selectAuthScheme
資訊: digest authentication scheme selected
##http://127.0.0.1/sites//./project-summary.html - Status code: 201

Transfer finished. 10190 bytes copied in 0.021 seconds
http://127.0.0.1/sites/ - Session: Disconnecting
http://127.0.0.1/sites/ - Session: Disconnected
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 28.737 s
[INFO] Finished at: 2015-11-03T21:00:07+08:00
[INFO] Final Memory: 14M/156M
[INFO] ------------------------------------------------------------------------

所有站點檔案夾和檔案,在專案檔案夾- 「target/site」會被自動部署到伺服器。

5. 輸出

在本例中,可以通過這個網址存取該部署的站點:http://127.0.0.1/sites/,見下圖:

auto deploy site with Maven

完成.

參考

  1. http://maven.apache.org/plugins/maven-site-plugin/usage.html
  2. http://mojo.codehaus.org/wagon-maven-plugin/usage.html
  3. http://maven.apache.org/plugins/maven-site-plugin/deploy-mojo.html
  4. http://maven.40175.n5.nabble.com/site-deploy-using-DAV-with-digest-auth-td125042.html
  5. http://www.sonatype.com/books/maven-book/reference/site-generation-sect-deploy-site.html