使用maven、jaxb等技术构建可配置编译打包及可配置项目运行
一、说在前面
apache ant是以个非常流行的项目构建打包开源工具。在很长一段时间里,ant简化了我们项目构建打包的过程,使得我们项目部署的速度有了大幅的提升。
然而,熟悉ant使用的朋友可能与我都有一个切身的感受:当我们在使用ant进行项目构建打包时,由于要分不同的环境(如:开发环境、测试环境、生产环境),构建打包的项目在不同环境中运行,所要加载的配置文件不同,因此,我们通常会将不同环境的配置文件放置多分,在打包的时候修改build.xml拷贝不同的配置,最终打包到war中。这种打包方式极为不便,我们期望有一种更简便的构建打包方式,不需要频繁修改build.xml,就能分别打出不同环境的war包。maven的出现为我们实现这个期望带来了希望。
本文就结合eclipse、maven eclipse插件、jaxb技术,实现不同环境情况下的灵活打包。
二、约定:
1、存在的运行环境有:测试环境与正式环境,分别用test与real两个关键字表示 2、文章假定您掌握了maven的使用,并且已经安装配置好了maven环境 3、文章假定您已经安装好了eclpse,并安装了eclipse maven插件 4、文章假定您理解xml与jaxb之间的关系 5、文章假定您理解xml文件与xsd文件之间的关系
三、设计与实现
1、创建maven web项目







2、创建相关配置文件(test.properties、real.properties、config.xml)

3、编辑相关配置文件内容

4、设置编译打包不同环境的配置文件

5、生成config.xml对应的config.xsd




6、生成config.xml对应的jaxb(通过config.xsd生成)



7、清除多余文件,并拷贝部署配置文件到特定目录

8、加入servlet-api.jar,并创建Servlet Listener类,用于加载config.xml配置

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;
}
}
web.xml:
<?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>
9、创建jsp用于读取配置
index.jsp:
<%@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>
10、设定与配置打包插件
pom.xml:
<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>
四、部署与运行
1、设定不同环境打包快捷方式,并运行




2、测试环境编译打包后部署运行的效果图:

3、正式环境编译打包后部署运行的效果图:

如果您觉得本文对您有益,请点击博文后的google广告,对作者表示支持,谢谢!