本文是《 整合 Maven 2 外掛程式到 Eclipse 的過程》的姊妹篇,介紹如何將你開發好的 Maven 2 項目部署到 Linux 的 Tomcat 下。
1.
配置 Maven 環境變數
配置 Path 環境變數,指向 Maven 2 的 bin 目錄。比如我的 Maven 2 安裝在 D 盤的 tools 目錄下,於是我在環境變數 Path 裡加進一條:
D:\tools\apache-maven-2.0.11\bin;
然後 commandLine 輸入 mvn -v,列印出 maven 的版本號碼等資訊,證明環境變數配置成功。
2.
添加 assembly 外掛程式實現自訂打包
修改 pom.xml 相關設定如下: [html] view plain copy print ? <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> <encoding>UTF-8</encoding> <failOnError>false</failOnError> </configuration> </plugin> <plugin> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <addMavenDescriptor>false</addMavenDescriptor> </archive> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <executions> <execution> <id>attach-sources</id> <phase>verify</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.2-beta-1</version> <configuration> <descriptors> <descriptor>assembly.xml</descriptor> </descriptors> <appendAssemblyId>false</appendAssemblyId> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
3.assembly.xml 樣本
在 pom.xml 同目錄下建立 assembly.xml,編輯其內容如下: [html] view plain copy print ? <assembly> <id>assembly</id> <includeBaseDirectory>false</includeBaseDirectory> <formats> <format>dir</format> </formats> <dependencySets> <dependencySet> <outputDirectory>WEB-INF/lib</outputDirectory> </dependencySet> </dependencySets> <fileSets> <fileSet> <directory>./src/main/webapp</directory> <outputDirectory>/</outputDirectory> <excludes> <exclude>*/classes/**</exclude> <exclude>*/lib/**</exclude> </excludes> </fileSet> </fileSets> <files> <file> <source>target/${project.artifactId}-${project.version}.jar</source> <outputDirectory>WEB-INF/lib</outputDirectory> </file> </files> </assembly>
4. 項目打包
需要打包的項目根目錄下執行命令列命令如下:
mvn package
這時,項目根目錄下的 target 目錄下會有 項目名-版本號碼.dir 檔案夾產生,該檔案夾下有 WEB-INF 目錄,WEB-INF 目錄下有原項目 WEB-INF 下的各設定檔,lib 下可以找到新產生的 項目名-版本號碼.jar 檔案以及所有相關依賴包。
5. Linux 建立項目部署的目錄
/home/www 目錄下建立(最好以項目名命名)目錄,然後將步驟 4 產生的 WEB-INF 拷貝到該目錄下。
6. 配置 tomcat
修改 Linux 下安裝的 tomcat 根目錄下的 conf 目錄下的 server.xml Context 標籤如下: [html] view plain copy print ? <Context docBase="/home/www/項目名" path="" reloadable="true"/> </Host> </Engine> </Service> </Server>
7. 配置 Nginx
Nginx 的 nginx.conf 樣本如下: [plain] view plain copy print ? user nginx nginx ; worker_processes 8; error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { use epoll; worker_connections 8192; } http { include mime.types; default_type application/octet-stream; log_format main '[$time_local] $remote_addr - "$request" ' '$status "$http_user_agent" ' '"$args"'; access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 120; client_max_body_size 120m; client_body_buffer_size 128k; server_names_hash_bucket_size 128; large_client_header_buffers 4 4k; open_file_cache max=8192 inactive=20s; open_file_cache_min_uses 1; open_file_cache_valid 30s; gzip on; gzip_http_version 1.0; gzip_disable "MSIE [1-6]."; gzip_min_length 1000; gzip_buffers 4 8k; gzip_types text/plain text/css application/x-javascript; server { listen 80; chunkin on; server_name www.defonds.com; charset utf-8; access_log logs/host.access.log main; #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } error_page 411 = @my_411_error; location @my_411_error { chunkin_resume; } location = /favicon.ico { log_not_found off; &nb