将本地开发的 Maven 2 项目部署到 Linux Tomcat 下的步骤


原文地址为:http://blog.csdn.net/defonds/article/details/7986878

本文是《集成 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 ?
  1. <build>  
  2.     <plugins>  
  3.         <plugin>  
  4.     <artifactId>maven-compiler-plugin</artifactId>  
  5.     <configuration>  
  6.         <source>1.6</source>  
  7.         <target>1.6</target>  
  8.         <encoding>UTF-8</encoding>  
  9.         <failOnError>false</failOnError>  
  10.     </configuration>  
  11. </plugin>  
  12.   
  13. <plugin>  
  14.     <artifactId>maven-jar-plugin</artifactId>  
  15.     <configuration>  
  16.         <archive>  
  17.             <addMavenDescriptor>false</addMavenDescriptor>  
  18.         </archive>  
  19.     </configuration>  
  20. </plugin>  
  21.   
  22. <plugin>  
  23.     <groupId>org.apache.maven.plugins</groupId>  
  24.     <artifactId>maven-source-plugin</artifactId>  
  25.     <executions>  
  26.         <execution>  
  27.             <id>attach-sources</id>  
  28.             <phase>verify</phase>  
  29.             <goals>  
  30.                 <goal>jar</goal>  
  31.             </goals>  
  32.         </execution>  
  33.     </executions>  
  34. </plugin>  
  35.   
  36. <plugin>  
  37.     <artifactId>maven-assembly-plugin</artifactId>  
  38.     <version>2.2-beta-1</version>  
  39.     <configuration>  
  40.         <descriptors>  
  41.             <descriptor>assembly.xml</descriptor>  
  42.         </descriptors>  
  43.         <appendAssemblyId>false</appendAssemblyId>  
  44.     </configuration>  
  45.   
  46.     <executions>  
  47.         <execution>  
  48.             <id>make-assembly</id>  
  49.             <phase>package</phase>  
  50.             <goals>  
  51.                 <goal>single</goal>  
  52.             </goals>  
  53.         </execution>  
  54.     </executions>  
  55. </plugin>  
  56.     </plugins>  
  57. </build>  

        3.assembly.xml 示例

        在 pom.xml 同目录下创建 assembly.xml,编辑其内容如下:

[html]  view plain  copy
 print ?
  1. <assembly>  
  2.     <id>assembly</id>  
  3.     <includeBaseDirectory>false</includeBaseDirectory>  
  4.     <formats>  
  5.         <format>dir</format>  
  6.     </formats>  
  7.     <dependencySets>  
  8.         <dependencySet>  
  9.             <outputDirectory>WEB-INF/lib</outputDirectory>  
  10.         </dependencySet>  
  11.   
  12.     </dependencySets>  
  13.   
  14.     <fileSets>  
  15.         <fileSet>  
  16.             <directory>./src/main/webapp</directory>  
  17.             <outputDirectory>/</outputDirectory>  
  18.             <excludes>  
  19.                 <exclude>*/classes/**</exclude>  
  20.                 <exclude>*/lib/**</exclude>  
  21.             </excludes>  
  22.         </fileSet>  
  23.     </fileSets>  
  24.   
  25.     <files>  
  26.         <file>  
  27.             <source>target/${project.artifactId}-${project.version}.jar</source>  
  28.             <outputDirectory>WEB-INF/lib</outputDirectory>  
  29.         </file>  
  30.     </files>  
  31.   
  32.   
  33. </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 ?
  1.           <Context docBase="/home/www/项目名" path="" reloadable="true"/>  
  2.       </Host>  
  3.     </Engine>  
  4.   </Service>  
  5. </Server>  


        7. 配置 Nginx

        Nginx 的 nginx.conf 示例如下:

[plain]  view plain  copy
 print ?
  1. user  nginx nginx ;  
  2. worker_processes  8;  
  3.   
  4. error_log  logs/error.log;  
  5. #error_log  logs/error.log  notice;  
  6. #error_log  logs/error.log  info;  
  7.   
  8. #pid        logs/nginx.pid;  
  9.   
  10.   
  11. events {  
  12.     use epoll;  
  13.     worker_connections  8192;  
  14. }  
  15.   
  16.   
  17. http {  
  18.     include       mime.types;  
  19.     default_type  application/octet-stream;  
  20.   
  21.     log_format  main  '[$time_local] $remote_addr - "$request" '  
  22.                       '$status "$http_user_agent" '  
  23.                       '"$args"';  
  24.   
  25.     access_log  logs/access.log  main;  
  26.   
  27.     sendfile        on;  
  28.     #tcp_nopush     on;  
  29.   
  30.     #keepalive_timeout  0;  
  31.     keepalive_timeout  120;  
  32.     client_max_body_size    120m;  
  33.     client_body_buffer_size 128k;  
  34.     server_names_hash_bucket_size 128;  
  35.     large_client_header_buffers 4 4k;  
  36.     open_file_cache max=8192 inactive=20s;  
  37.     open_file_cache_min_uses 1;  
  38.     open_file_cache_valid 30s;  
  39.       
  40.     gzip on;  
  41.     gzip_http_version 1.0;  
  42.     gzip_disable "MSIE [1-6].";  
  43.     gzip_min_length  1000;  
  44.     gzip_buffers     4 8k;  
  45.     gzip_types       text/plain text/css application/x-javascript;  
  46.   
  47.     server {  
  48.     listen       80;  
  49.     chunkin      on;  
  50.     server_name  www.defonds.com;  
  51.   
  52.     charset utf-8;  
  53.     access_log  logs/host.access.log  main;  
  54.   
  55.     #error_page  404              /404.html;  
  56.   
  57.     # redirect server error pages to the static page /50x.html  
  58.     #  
  59.   
  60.     error_page   500 502 503 504  /50x.html;  
  61.     location = /50x.html {  
  62.         root   html;  
  63.     }  
  64.     error_page 411 = @my_411_error;  
  65.     location @my_411_error {  
  66.         chunkin_resume;  
  67.     }  
  68.       
  69.     location = /favicon.ico {  
  70.         log_not_found off;  
  71.         access_log off;  
  72.         expires      90d;  
  73.     }  
  74.     location / {  
  75.         proxy_pass http://localhost:8080;  
  76.         include proxy.conf;  
  77.     }  
  78.     }  
  79.   
  80.     # HTTPS server  
  81.     #  
  82.     server {  
  83.         listen       443;  
  84.         server_name  www.defonds.com;  
  85.   
  86.         ssl                  on;  
  87.         ssl_certificate      defondssync.crt;  
  88.         ssl_certificate_key  defondssync.key;  
  89.       
  90.     charset utf-8;  
  91.     access_log  logs/ssl.host.access.log main;  
  92.       
  93.     error_page   500 502 503 504  /50x.html;  
  94.     location = /50x.html {  
  95.         root   html;  
  96.     }  
  97.   
  98.     location = /favicon.ico {  
  99.         log_not_found off;  
  100.         access_log off;  
  101.         expires      90d;  
  102.     }  
  103.       
  104.     location / {  
  105.         proxy_pass http://localhost:8080;  
  106.         include proxy.conf;  
  107.     }  
  108.     }  
  109.   
  110. }  

        8. 重启 tomcat 以及 Nginx
        重启 tomcat 以及 Nginx,这样子你的客户就可以在外网对项目进行访问了。可以参考《 在 Windows 上监控 linux 服务器上 tomcat 的控制台 》对项目进行实时监控。
        9. 重新部署
        如果项目代码有更新需要重新部署项目,只需(先关掉服务器 tomcat)执行以下步骤 4,然后用新生成的  项目名-版本号.jar  将老的覆盖即可。如果有新的依赖包,也要将依赖包考进来。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值