您的位置:首页 > 运维架构 > Tomcat

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

2016-12-02 09:33 633 查看
原文地址为: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?

<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;  

        access_log off;  

        expires      90d;  

    }  

    location / {  

        proxy_pass http://localhost:8080;  

        include proxy.conf;  

    }  

    }  

  

    # HTTPS server  

    #  

    server {  

        listen       443;  

        server_name  www.defonds.com;  

  

        ssl                  on;  

        ssl_certificate      defondssync.crt;  

        ssl_certificate_key  defondssync.key;  

      

    charset utf-8;  

    access_log  logs/ssl.host.access.log main;  

      

    error_page   500 502 503 504  /50x.html;  

    location = /50x.html {  

        root   html;  

    }  

  

    location = /favicon.ico {  

        log_not_found off;  

        access_log off;  

        expires      90d;  

    }  

      

    location / {  

        proxy_pass http://localhost:8080;  

        include proxy.conf;  

    }  

    }  

  

}  

        8.重启 tomcat 以及 Nginx
        重启 tomcat 以及 Nginx,这样子你的客户就可以在外网对项目进行访问了。可以参考《在
Windows 上监控 linux 服务器上 tomcat 的控制台》对项目进行实时监控。
        9.重新部署
        如果项目代码有更新需要重新部署项目,只需(先关掉服务器 tomcat)执行以下步骤 4,然后用新生成的 项目名-版本号.jar 将老的覆盖即可。如果有新的依赖包,也要将依赖包考进来。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐