应用场景
1:一个maven构建的web工程。
2:有开发环境,有生产环境。
3:将工程打包成war。
4:自动部署到Tomcat上。
构建工程
用Eclipse构建一个Maven的web工程。
首先创建一个maven的web工程,参考链接:http://www.cnblogs.com/noteless/p/5213075.html
使用Plugin打包成war
最新plugin列表以及说明的URL:http://maven.apache.org/plugins/index.html
自动部署到tomcat上
修改tomcat的tomcat-user.xml文件
<role rolename="admin-gui"/>
<role rolename="admin-script"/>
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
<user username="tomcat" password="12345678" roles="manager-gui,manager-script,manager-jmx,manager-status,admin-script,admin-gui"/>
方法一
修改工程的pom.xml文件,增加如下plugin
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<url>http://localhost:8080/manager/text</url>
<username>tomcat</username>
<password>12345678</password>
<path>/yiibaiWebApp</path>
</configuration>
</plugin>
方法二
这里直接把用户名和密码写在了pom.xml的文件里,还有另外一种处理方式就是将用户名和密码写在maven的setting.xml文件里。
setting.xml文件中添加配置信息:
<server>
<id>Tomcat7</id>
<username>tomcat</username>
<password>12345678</password>
</server>
pom.xml文件中的配置:
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<!-- 注意此处的url -->
<url>http://localhost:8080/manager/text</url>
<!-- 此处的名字必须和setting.xml中配置的ID一致 -->
<server>Tomcat7</server>
<!-- 此处的名字是项目发布的工程名-->
<path>/webMaven</path>
</configuration>
</plugin>
开发环境的配置文件在:
src/main/resources/dev/config.properties
A.name=DEV A Name
A.title=DEV A Title
B.name=DEV B Name
B.title=DEV B Title
生产环境的配置文件在:
src/main/resources/product/config.properties
A.name=PRODUCT A Name
A.title=PRODUCT A Title
B.name=PRODUCT B Name
B.title=PRODUCT B Title
正式的配置文件:
src/main/resources/configA.properties
A.name=${A.name}
A.title=${A.title}
src/main/resources/configB.properties
B.name=${B.name}
B.title=${B.title}
在<build>
内容里增加<resources>
部分内容
<resources>
<!-- 先指定 src/main/resources下所有文件及文件夹为资源文件,排除了dev和product文件夹下的内容 -->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
<excludes>
<exclude>dev/*</exclude>
<exclude>product/*</exclude>
</excludes>
</resource>
<!-- 设置对configA.properties,configB.properties进行过虑,即这些文件中的${key}会被替换掉为真正的值 -->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>configA.properties</include>
<include>configB.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
增加profiles,针对不同的环境设置不同的值。
<profiles>
<profile>
<id>dev</id>
<!-- 默认激活开发配制,使用config-dev.properties来替换设置过虑的资源文件中的${key} -->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<filters>
<filter>src/main/resources/dev/config.properties</filter>
</filters>
</build>
</profile>
<profile>
<id>product</id>
<build>
<filters>
<filter>src/main/resources/product/config.properties</filter>
</filters>
</build>
</profile>
</profiles>
修改index.jsp
将properties的内容打印出来!!
maven tomcat7:deploy -P dev
启用dev的配置。
访问index.jsp发现properties的内容打印出来是DEV相关的。
maven tomcat7:deploy -P product
访问index.jsp,发现打印出来的内容是product相关的。
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>cn.test</groupId>
<artifactId>webMaven</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>webMaven Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<!-- 设置JDK的版本,编译版本和运行版本,可以使用Plugin的模式 -->
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<!-- 指定编码 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>webMaven</finalName>
<resources>
<!-- 先指定 src/main/resources下所有文件及文件夹为资源文件 -->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
<excludes>
<exclude>dev/*</exclude>
<exclude>product/*</exclude>
</excludes>
</resource>
<!-- 设置对configA.properties,configB.properties进行过虑,即这些文件中的${key}会被替换掉为真正的值 -->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>configA.properties</include>
<include>configB.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<!-- 生成war的Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<!-- 删除了Log4j的jar包 -->
<warSourceExcludes>WEB-INF/lib/log4j-${log4j.version}.jar</warSourceExcludes>
</configuration>
</plugin>
<!-- 利用Maven直接将工程发布到Tomcat上,执行的命令(goal)是 tomcat7:deploy,重新部署使用tomcat7:redeploy -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<!-- 注意此处的url -->
<url>http://localhost:8080/manager/text</url>
<!--
可以将用户名和密码的信息配置在maven的setting.xml中
格式如下:
<server>
<id>Tomcat7</id>
<username>tomcat</username>
<password>12345678</password>
</server>
-->
<!-- 此处的名字必须和setting.xml中配置的ID一致 -->
<!--
<server>Tomcat7</server>
-->
<!-- 可以直接将用户名和密码写在部署文件上 -->
<username>tomcat</username>
<password>12345678</password>
<!-- 此处的名字是项目发布的工程名-->
<path>/webMaven</path>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>dev</id>
<!-- 默认激活开发配制,使用dev/config.properties来替换设置过虑的资源文件中的${key} -->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<filters>
<filter>src/main/resources/dev/config.properties</filter>
</filters>
</build>
</profile>
<profile>
<id>product</id>
<build>
<filters>
<filter>src/main/resources/product/config.properties</filter>
</filters>
</build>
</profile>
</profiles>
</project>
这样,我们第一个实战的例子就完成!
以后有新的实战场景会继续添加!!