[b]一、说在前面[/b]
apache ant是以个非常流行的项目构建打包开源工具。在很长一段时间里,ant简化了我们项目构建打包的过程,使得我们项目部署的速度有了大幅的提升。
然而,熟悉ant使用的朋友可能与我都有一个切身的感受:当我们在使用ant进行项目构建打包时,由于要分不同的环境(如:开发环境、测试环境、生产环境),构建打包的项目在不同环境中运行,所要加载的配置文件不同,因此,我们通常会将不同环境的配置文件放置多分,在打包的时候修改build.xml拷贝不同的配置,最终打包到war中。这种打包方式极为不便,我们期望有一种更简便的构建打包方式,不需要频繁修改build.xml,就能分别打出不同环境的war包。maven的出现为我们实现这个期望带来了希望。
本文就结合eclipse、maven eclipse插件、jaxb技术,实现不同环境情况下的灵活打包。
[b]二、约定:[/b]
1、存在的运行环境有:测试环境与正式环境,分别用test与real两个关键字表示
2、文章假定您掌握了maven的使用,并且已经安装配置好了maven环境
3、文章假定您已经安装好了eclpse,并安装了eclipse maven插件
4、文章假定您理解xml与jaxb之间的关系
5、文章假定您理解xml文件与xsd文件之间的关系
[b]三、设计与实现[/b]
[b]1、创建maven web项目[/b]
[img]http://dl.iteye.com/upload/attachment/0075/0778/faa76798-fc5f-3f45-9028-786302970463.png[/img]
[img]http://dl.iteye.com/upload/attachment/0075/0780/ed3b1906-a788-3413-ae28-e8e1af151e6c.png[/img]
[img]http://dl.iteye.com/upload/attachment/0075/0782/a8c1f2e6-5542-3a91-a56f-09b4d5e46310.png[/img]
[img]http://dl.iteye.com/upload/attachment/0075/0784/bec514eb-4c98-3f2c-93ba-419afd3a3e09.png[/img]
[img]http://dl.iteye.com/upload/attachment/0075/0786/b238a6a1-fc75-3866-95a7-3e1640585b4e.png[/img]
[img]http://dl.iteye.com/upload/attachment/0075/0788/a323092d-072e-3b86-8807-1c4e19e96c7c.png[/img]
[img]http://dl.iteye.com/upload/attachment/0075/0790/4b81d0de-54b6-3e2f-ae2b-8f06e920ce8b.png[/img]
[b]2、创建相关配置文件(test.properties、real.properties、config.xml)[/b]
[img]http://dl.iteye.com/upload/attachment/0075/0792/b7d14970-44ca-3728-8654-220f31dfcb7f.png[/img]
[b]3、编辑相关配置文件内容[/b]
[img]http://dl.iteye.com/upload/attachment/0075/1010/01adda66-a33b-30c5-b5f3-5b511313ec0e.png[/img]
[b]4、设置编译打包不同环境的配置文件[/b]
[img]http://dl.iteye.com/upload/attachment/0075/0794/7bb6f236-28f9-3d2f-ac8a-e514071edd04.png[/img]
[b]5、生成config.xml对应的config.xsd[/b]
[img]http://dl.iteye.com/upload/attachment/0075/0796/14fad7ce-177c-3f5b-a49e-a0f7ccaa8f75.png[/img]
[img]http://dl.iteye.com/upload/attachment/0075/0798/8523e914-ef7d-383e-bfcd-19311ff12471.png[/img]
[img]http://dl.iteye.com/upload/attachment/0075/0800/1110dced-5006-38a9-886b-82b58278ce0f.png[/img]
[img]http://dl.iteye.com/upload/attachment/0075/0802/0b43093b-41b1-33b5-a2aa-ab684a7f5ac1.png[/img]
[b]6、生成config.xml对应的jaxb(通过config.xsd生成)[/b]
[img]http://dl.iteye.com/upload/attachment/0075/0804/317accd0-302a-3aa0-a414-da859c5efb0c.png[/img]
[img]http://dl.iteye.com/upload/attachment/0075/0806/f4dec220-23d9-3019-8531-17f0170ce1ab.png[/img]
[img]http://dl.iteye.com/upload/attachment/0075/0808/69dd0f45-dc47-3a92-9ec7-7b648f9947c9.png[/img]
[b]7、清除多余文件,并拷贝部署配置文件到特定目录[/b]
[img]http://dl.iteye.com/upload/attachment/0075/0812/07a6202e-fdcf-37f8-b771-cf72dbcdd4e1.png[/img]
[b]8、加入servlet-api.jar,并创建Servlet Listener类,用于加载config.xml配置[/b]
[img]http://dl.iteye.com/upload/attachment/0075/0814/3e6102f4-176b-3b78-b90a-fac517044094.png[/img]
[code]
package com.javaedu.web;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.Properties;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.xml.bind.JAXB;
import org.springframework.util.ResourceUtils;
import com.javaedu.cfg.jaxb.Attribute;
import com.javaedu.cfg.jaxb.Attributes;
import com.javaedu.cfg.jaxb.MyCfg;
public class ConfigLoaderListener implements ServletContextListener {
public static Properties appConfig = new Properties();
public void contextInitialized(ServletContextEvent arg0) {
File clsFile = null;
try {
clsFile = ResourceUtils.getFile("classpath:config.xml");
} catch (FileNotFoundException e) {
System.out.println("loading a config.xml from classpath error!");
}
MyCfg cfg = JAXB.unmarshal(clsFile, MyCfg.class);
Attributes atts = cfg.getAttributes();
List<Attribute> attList = atts.getAttribute();
if (attList != null && !attList.isEmpty()) {
for (Attribute att : attList) {
appConfig.put(att.getKey(), att.getValue());
}
}
}
public void contextDestroyed(ServletContextEvent arg0) {
appConfig = null;
}
}
[/code]
[b]web.xml:[/b]
[code]
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<listener>
<listener-class>
com.javaedu.web.ConfigLoaderListener
</listener-class>
</listener>
</web-app>
[/code]
[b]9、创建jsp用于读取配置[/b]
[b]index.jsp:[/b]
[code]
<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
<%@page import="com.javaedu.web.ConfigLoaderListener" %>
<html>
<head>
<title>红色黑客博客老地址</title>
</head>
<body>
<h2>红色黑客博客老地址:<%= ConfigLoaderListener.appConfig.get("old.url") %></h2>
<h2>红色黑客博客新地址:<%= ConfigLoaderListener.appConfig.get("new.url") %></h2>
</body>
</html>
[/code]
[b]10、设定与配置打包插件[/b]
[b]pom.xml:[/b]
[code]
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<packagingExcludes>
WEB-INF/classes/real.properties,
WEB-INF/classes/test.properties,
</packagingExcludes>
<filters>
<filter>src/main/resources/real.properties</filter>
</filters>
<webappDirectory>target/xmlconfig</webappDirectory>
<webResources>
<resource>
<!-- 元配置文件的目录,相对于pom.xml文件的路径 -->
<directory>src/main/pkgfilter</directory>
<!-- 是否过滤文件,也就是是否启动auto-config的功能 -->
<filtering>true</filtering>
<!-- 目标路径 -->
<targetPath>WEB-INF/classes</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
[/code]
[b]四、部署与运行[/b]
[b]1、设定不同环境打包快捷方式,并运行[/b]
[img]http://dl.iteye.com/upload/attachment/0075/0816/2f5565c8-a559-3e35-927e-c46bab232714.png[/img]
[img]http://dl.iteye.com/upload/attachment/0075/0818/15f86917-249b-30f9-bf59-dc1af32e043a.png[/img]
[img]http://dl.iteye.com/upload/attachment/0075/0820/94f63e6e-e673-32c8-8055-97e6eb9fcc0c.png[/img]
[img]http://dl.iteye.com/upload/attachment/0075/0874/ef6cdff3-4229-334f-adc1-e795b95a8045.png[/img]
[b]2、测试环境编译打包后部署运行的效果图:[/b]
[img]http://dl.iteye.com/upload/attachment/0075/0872/30845d40-70ba-3d07-8454-5d7b78b1249a.png[/img]
[b]3、正式环境编译打包后部署运行的效果图:[/b]
[img]http://dl.iteye.com/upload/attachment/0075/0870/b65ca61b-8aa0-3517-aeca-3fcb80bb7def.png[/img]
[color=gray]如果您觉得本文对您有益,请点击博文后的google广告,对作者表示支持,谢谢![/color]
apache ant是以个非常流行的项目构建打包开源工具。在很长一段时间里,ant简化了我们项目构建打包的过程,使得我们项目部署的速度有了大幅的提升。
然而,熟悉ant使用的朋友可能与我都有一个切身的感受:当我们在使用ant进行项目构建打包时,由于要分不同的环境(如:开发环境、测试环境、生产环境),构建打包的项目在不同环境中运行,所要加载的配置文件不同,因此,我们通常会将不同环境的配置文件放置多分,在打包的时候修改build.xml拷贝不同的配置,最终打包到war中。这种打包方式极为不便,我们期望有一种更简便的构建打包方式,不需要频繁修改build.xml,就能分别打出不同环境的war包。maven的出现为我们实现这个期望带来了希望。
本文就结合eclipse、maven eclipse插件、jaxb技术,实现不同环境情况下的灵活打包。
[b]二、约定:[/b]
1、存在的运行环境有:测试环境与正式环境,分别用test与real两个关键字表示
2、文章假定您掌握了maven的使用,并且已经安装配置好了maven环境
3、文章假定您已经安装好了eclpse,并安装了eclipse maven插件
4、文章假定您理解xml与jaxb之间的关系
5、文章假定您理解xml文件与xsd文件之间的关系
[b]三、设计与实现[/b]
[b]1、创建maven web项目[/b]
[img]http://dl.iteye.com/upload/attachment/0075/0778/faa76798-fc5f-3f45-9028-786302970463.png[/img]
[img]http://dl.iteye.com/upload/attachment/0075/0780/ed3b1906-a788-3413-ae28-e8e1af151e6c.png[/img]
[img]http://dl.iteye.com/upload/attachment/0075/0782/a8c1f2e6-5542-3a91-a56f-09b4d5e46310.png[/img]
[img]http://dl.iteye.com/upload/attachment/0075/0784/bec514eb-4c98-3f2c-93ba-419afd3a3e09.png[/img]
[img]http://dl.iteye.com/upload/attachment/0075/0786/b238a6a1-fc75-3866-95a7-3e1640585b4e.png[/img]
[img]http://dl.iteye.com/upload/attachment/0075/0788/a323092d-072e-3b86-8807-1c4e19e96c7c.png[/img]
[img]http://dl.iteye.com/upload/attachment/0075/0790/4b81d0de-54b6-3e2f-ae2b-8f06e920ce8b.png[/img]
[b]2、创建相关配置文件(test.properties、real.properties、config.xml)[/b]
[img]http://dl.iteye.com/upload/attachment/0075/0792/b7d14970-44ca-3728-8654-220f31dfcb7f.png[/img]
[b]3、编辑相关配置文件内容[/b]
[img]http://dl.iteye.com/upload/attachment/0075/1010/01adda66-a33b-30c5-b5f3-5b511313ec0e.png[/img]
[b]4、设置编译打包不同环境的配置文件[/b]
[img]http://dl.iteye.com/upload/attachment/0075/0794/7bb6f236-28f9-3d2f-ac8a-e514071edd04.png[/img]
[b]5、生成config.xml对应的config.xsd[/b]
[img]http://dl.iteye.com/upload/attachment/0075/0796/14fad7ce-177c-3f5b-a49e-a0f7ccaa8f75.png[/img]
[img]http://dl.iteye.com/upload/attachment/0075/0798/8523e914-ef7d-383e-bfcd-19311ff12471.png[/img]
[img]http://dl.iteye.com/upload/attachment/0075/0800/1110dced-5006-38a9-886b-82b58278ce0f.png[/img]
[img]http://dl.iteye.com/upload/attachment/0075/0802/0b43093b-41b1-33b5-a2aa-ab684a7f5ac1.png[/img]
[b]6、生成config.xml对应的jaxb(通过config.xsd生成)[/b]
[img]http://dl.iteye.com/upload/attachment/0075/0804/317accd0-302a-3aa0-a414-da859c5efb0c.png[/img]
[img]http://dl.iteye.com/upload/attachment/0075/0806/f4dec220-23d9-3019-8531-17f0170ce1ab.png[/img]
[img]http://dl.iteye.com/upload/attachment/0075/0808/69dd0f45-dc47-3a92-9ec7-7b648f9947c9.png[/img]
[b]7、清除多余文件,并拷贝部署配置文件到特定目录[/b]
[img]http://dl.iteye.com/upload/attachment/0075/0812/07a6202e-fdcf-37f8-b771-cf72dbcdd4e1.png[/img]
[b]8、加入servlet-api.jar,并创建Servlet Listener类,用于加载config.xml配置[/b]
[img]http://dl.iteye.com/upload/attachment/0075/0814/3e6102f4-176b-3b78-b90a-fac517044094.png[/img]
[code]
package com.javaedu.web;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.Properties;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.xml.bind.JAXB;
import org.springframework.util.ResourceUtils;
import com.javaedu.cfg.jaxb.Attribute;
import com.javaedu.cfg.jaxb.Attributes;
import com.javaedu.cfg.jaxb.MyCfg;
public class ConfigLoaderListener implements ServletContextListener {
public static Properties appConfig = new Properties();
public void contextInitialized(ServletContextEvent arg0) {
File clsFile = null;
try {
clsFile = ResourceUtils.getFile("classpath:config.xml");
} catch (FileNotFoundException e) {
System.out.println("loading a config.xml from classpath error!");
}
MyCfg cfg = JAXB.unmarshal(clsFile, MyCfg.class);
Attributes atts = cfg.getAttributes();
List<Attribute> attList = atts.getAttribute();
if (attList != null && !attList.isEmpty()) {
for (Attribute att : attList) {
appConfig.put(att.getKey(), att.getValue());
}
}
}
public void contextDestroyed(ServletContextEvent arg0) {
appConfig = null;
}
}
[/code]
[b]web.xml:[/b]
[code]
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<listener>
<listener-class>
com.javaedu.web.ConfigLoaderListener
</listener-class>
</listener>
</web-app>
[/code]
[b]9、创建jsp用于读取配置[/b]
[b]index.jsp:[/b]
[code]
<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
<%@page import="com.javaedu.web.ConfigLoaderListener" %>
<html>
<head>
<title>红色黑客博客老地址</title>
</head>
<body>
<h2>红色黑客博客老地址:<%= ConfigLoaderListener.appConfig.get("old.url") %></h2>
<h2>红色黑客博客新地址:<%= ConfigLoaderListener.appConfig.get("new.url") %></h2>
</body>
</html>
[/code]
[b]10、设定与配置打包插件[/b]
[b]pom.xml:[/b]
[code]
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<packagingExcludes>
WEB-INF/classes/real.properties,
WEB-INF/classes/test.properties,
</packagingExcludes>
<filters>
<filter>src/main/resources/real.properties</filter>
</filters>
<webappDirectory>target/xmlconfig</webappDirectory>
<webResources>
<resource>
<!-- 元配置文件的目录,相对于pom.xml文件的路径 -->
<directory>src/main/pkgfilter</directory>
<!-- 是否过滤文件,也就是是否启动auto-config的功能 -->
<filtering>true</filtering>
<!-- 目标路径 -->
<targetPath>WEB-INF/classes</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
[/code]
[b]四、部署与运行[/b]
[b]1、设定不同环境打包快捷方式,并运行[/b]
[img]http://dl.iteye.com/upload/attachment/0075/0816/2f5565c8-a559-3e35-927e-c46bab232714.png[/img]
[img]http://dl.iteye.com/upload/attachment/0075/0818/15f86917-249b-30f9-bf59-dc1af32e043a.png[/img]
[img]http://dl.iteye.com/upload/attachment/0075/0820/94f63e6e-e673-32c8-8055-97e6eb9fcc0c.png[/img]
[img]http://dl.iteye.com/upload/attachment/0075/0874/ef6cdff3-4229-334f-adc1-e795b95a8045.png[/img]
[b]2、测试环境编译打包后部署运行的效果图:[/b]
[img]http://dl.iteye.com/upload/attachment/0075/0872/30845d40-70ba-3d07-8454-5d7b78b1249a.png[/img]
[b]3、正式环境编译打包后部署运行的效果图:[/b]
[img]http://dl.iteye.com/upload/attachment/0075/0870/b65ca61b-8aa0-3517-aeca-3fcb80bb7def.png[/img]
[color=gray]如果您觉得本文对您有益,请点击博文后的google广告,对作者表示支持,谢谢![/color]