Maven Profile定制化构建

参考来自:http://suhuanzheng7784877.iteye.com/blog/1100412,我自己参考他的内容,在做配置方面有补充和改变!由于一些maven打包丢失文件的原因,导致我花了点时间才成功!


1.  前言

有时候我们开发的软件有不同的环境,开发阶段是开发环境,也就是我们这些研发人员平时使用的环境,大多数人开发还是在Windows下面吧,少数人连开发环境都需要在Linux或者Unix下进行,因为Java自身的跨平台性可能在哪个操作系统下开发差别不大,如果Java调用C/C++执行特定服务,就需要C/C++人员开发时最好和生产环境一样在Linux(而且版本一致)下进行研发,省得到时候测试的时候还得放到Linux下重新编译一遍。开发到了一定阶段后往往就是测试那边Team的加入了,功能测试、压力测试等等需要一套测试环境,而往往测试环境也分很多种情况,功能测试环境比较接近于开发环境,而压力测试环境往往接近于生产环境,甚至就是生产环境。面对这么多环境,以前我们可能都是手工进行配置,在不同环境下赋予不同的配置值。而手工配置就面临着可能出现错误,效率比较低等问题。

使用Maven的Profile配置,可以帮助我们灵活的进行构建项目,使之通过统一的定制化pom.xml配置就能让它适应不同的环境,如此一来显得软件更有应变性和适应性。

2.  屏蔽构建差异

笔者就以之前那个持久层模块作为基础,假如数据库在开发环境和测试环境有差异,怎么办,一般我们将数据库信息写到了配置文件properties中。

db.properties内容如下

db.Driver=${db.Driver}
db.url=${db.url}
db.user=${db.user}
db.password=${db.password}

  大家有可能奇怪,这里应该是具体的配置值了,是啊,配置值在pom.xml中

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>com.tk.test</groupId>
	<artifactId>maven-profile-test</artifactId>
	<version>0.0.1</version>
	<packaging>jar</packaging>
	<name>maven-profile-test</name>
	<url>http://maven.apache.org</url>

	<!-- 加入junit依赖包 -->
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<type>jar</type>
			<scope>compile</scope>
		</dependency>
	</dependencies>
	<build>
		<!-- 注意::maven执行package命令打包时,
		src/main/java路径下的properties文件偶尔丢失,
		因此需要加入resources配置 -->
		<resources>
			<resource>
				<directory>src/main/resources</directory>
				<includes>
					<include>**/*.properties</include>
				</includes>
				<filtering>true</filtering>
			</resource>
		</resources>
		<plugins>
			<!-- 为了打印出dbUser这个属性,
			向pom.xml中添加一个maven-antrun-plugin插件,
			执行命令"mvn initialize" 或 "mvn initialize -Ptest2" -->
			<plugin>
				<artifactId>maven-antrun-plugin</artifactId>
				<executions>
					<execution>
						<phase>initialize</phase>
						<goals>
							<goal>run</goal>
						</goals>
						<configuration>
							<tasks>
								<echo>dbUser:${db.user}</echo>
							</tasks>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>


	<profiles>
		<profile>
			<id>test1</id>
			<activation>
				<!-- 默认激活test1配置 -->
				<activeByDefault>true</activeByDefault>
				<property>
					<name>env</name>
					<value>dev1</value>
				</property>
			</activation>
			<properties>
				<db.Driver>org.gjt.mm.mysql.Driver</db.Driver>
				<db.url>jdbc:mysql://localhost:3306/test</db.url>
				<db.user>my</db.user>
				<db.password>111111</db.password>
			</properties>
		</profile>
		<profile>
			<id>test2</id>
			<activation>
				<property>
					<name>env</name>
					<value>dev2</value>
				</property>
			</activation>
			<properties>
				<db.Driver>com.mysql.jdbc.Driver</db.Driver>
				<db.url>jdbc:mysql://192.168.1.109:3306/test</db.url>
				<db.user>dba</db.user>
				<db.password>dba</db.password>
			</properties>
			<build>
			<plugins>
				<!-- 在开发时,我们使用“mvn clean install”,此时名为production的Profile并没有被激活,
				所以还是采用Java编译默认的配置(即在结果中包含了调试信息)。
				当需要为生产部署环境编译时,便可以使用“mvn clean install -Ptest2”。 -->
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-compiler-plugin</artifactId>
					<configuration>
						<debug>false</debug>
					</configuration>
				</plugin>
			</plugins>
		</build>
		</profile>
	</profiles>
</project>

activation标签代表激活该特性的配置,这里所谓激活,其实就是让该个性化配置生效的意思。

下面我们写一个Java类简单读取配置文件

package util;


import java.io.IOException;  
import java.io.InputStream;  
import java.util.Properties;  
  
public class ReadConfig {  
  
    public static String read() throws IOException {  
//        InputStream in = ClassLoader.getSystemResourceAsStream("/db.properties");  
        InputStream in = Object.class.getResourceAsStream("/db.properties"); 
        Properties p = new Properties();  
        p.load(in);  
        String dbDriver = p.getProperty("db.Driver"); 
        String dburl = p.getProperty("db.url");  
        String dbuser = p.getProperty("db.user");  
        String dbpassword = p.getProperty("db.password");  
          
        System.out.println(dbDriver);  
        System.out.println(dburl);  
        System.out.println(dbuser);  
        System.out.println(dbpassword);  
        return dbpassword;  
          
    }
  
}


 之后为其写单元测试

package util;


import static org.junit.Assert.*;
import java.io.IOException;
import org.junit.Test;

public class ReadConfigTest {

	@Test
	public void testRead() throws IOException {
		String password = ReadConfig.read();  
        System.out.println("password: "+password);  
        assertTrue(password.equals("dba"));
	}

}


 代码很简单,下面我们在控制台输入如下命令看看


mvn clean package -Ptest2


 控制台输出结果如下




命令行执行:mvn initialize -Ptest2


 可以看到,构建后的配置文件内容${db.Driver}的临时变量信息



在控制台输入如下命令:

mvn clean package -Denv=dev1


 结果如下表示profile的test1特性被激活了,测试结果与预期预料不符合。激活profile有以上两种方式,显示指定配置参数方式、系统属性=某些具体值的时候。还有在用户级setting.xml或者全局级setting.xml文件中配置默认信息的。不过这种方式不是很赞成。因为配置了此信息意味着只能在自己本机环境中得到正确的相应,项目打包,发布后并不会保存到自身的pom文件中,那么别人构建的时候也许会报错。所以尽量在项目级别的pom.xml中配置相关定制化信息。




附加:当属性越来越多的时候,在pom.xml文件配置就会显得pom文件很臃肿。因此如下配件较好:

<profiles>
	<profile>
		<id>rd</id>
		<activation>
			<activeByDefault>true</activeByDefault>
		</activation>
		<properties>
			<conf-dir>rd</conf-dir>
		</properties>
	</profile>
	<profile>
		<id>qa</id>
		<properties>
			<conf-dir>qa</conf-dir>
		</properties>
	</profile>
	<profile>
		<id>online</id>
		<properties>
			<conf-dir>online</conf-dir>
		</properties>
	</profile>
</profiles>

<build>
	<finalName>survey</finalName>
	<resources>
		<resource>
			<directory>src/main/resources/${conf-dir}</directory>
		</resource>
		<resource>
			<directory>src/main/resources/base</directory>
		</resource>
	</resources>
</build>


base文件夹用来存公共的
rd,qa,online分别放配置,一般是properties,你的数据库配置可以放各自的文件夹里边,这样POM不会显得很累赘 


3.  总结

Profile是轮廓;外形;外观;形象; 侧面(像),侧影的意思,我觉得叫做个性化配置更好。个性化配置还可以过滤web项目资源、集成测试。因为网上有相关的资源,就不总结了。多数应用还是屏蔽个性化构建的差异。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值