原文地址为: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 相关设置如下:
- <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,编辑其内容如下:
- <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 标签如下:
- <Context docBase="/home/www/项目名" path="" reloadable="true"/>
- </Host>
- </Engine>
- </Service>
- </Server>
7.
配置 Nginx
Nginx 的 nginx.conf 示例如下:
8.
重启 tomcat 以及 Nginx
重启 tomcat 以及 Nginx,这样子你的客户就可以在外网对项目进行访问了。可以参考《
在 Windows 上监控 linux 服务器上 tomcat 的控制台
》对项目进行实时监控。
9.
重新部署
如果项目代码有更新需要重新部署项目,只需(先关掉服务器 tomcat)执行以下步骤 4,然后用新生成的
项目名-版本号.jar
将老的覆盖即可。如果有新的依赖包,也要将依赖包考进来。