说明
本篇主要展示两种不同的方式构建环境隔离,第一是利用filter,第二种是配置maven插件的方式。构建的环境分dev和pord两种环境。代码已经放到github上,讲解过程中涉及比较多的文件和代码,建议各位先把代码下载下来,配合代码看这片文章。
示例通过IDEA构建,构建环境隔离主要是通过修改pom.xml文件。通过单元测试和maven命令构建的方式来测试结果,不会使用IDEA的小伙伴也能参照使用。
1 filter模式
正如字面意思所表达的那样,你可以指定需要过滤的文件,然后根据你环境配置的属性,应用到指定过滤的文件上。所有通过这种方式实现的环境隔离,需要指定属性和过滤的文件。示例参见:https://github.com/gitlxp1101/isolation/tree/model1
1.1 添加属性
添加属性的方式有两种,第一是直接在properties下添加相应的属性,如下,我添加了person.name和person.agel两个属性。
<properties>
<person.name>jack</person.name>
<person.age>55</person.age>
</properties>
上面那种情况适用于属性较少的情况,通常用于配置日志的隔离级别。但对于一个比较大的项目,往往需要配置很多属性,如果全部配置在properties下,整个pom文件就会很大,可读性大大降低。所有我们可以采取第二种方式,将这些属性专门添加到一个文件中。比如,我将所有属性放到一个名为filter-dev.properties文件中(演示项目中与src是同一级目录),然后添加如下配置。
<build>
<filters>
<filter> filter-dev.properties</filter>
</filters>
</build>
filter-dev.properties内容如下
school.name=dev-university
1.2 配置文件过滤
属性配置好了,现在还需要指定那些文件需要被过滤。如下代码,指定filtering为true,表示src/main/resources目录下所有的文件都会被过滤。
<resources>
<resource>
<directory>src/main/resources</directory>
<!-- 启用过滤 -->
<filtering>true</filtering>
</resource>
</resources>
1.3 完整的配置
完整的配置如下(不是整个pom文件,只包含build结点和profiles结点,build结点和profiles结点都位于pom文件下project结点下面,完整的pom文件请把代码下载下来查看)
<build>
<finalName>isolation</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<!-- 启用过滤 -->
<filtering>true</filtering>
</resource>
</resources>
</build>
<profiles>
<profile>
<id>dev</id>
<properties>
<person.name>tom</person.name>
<person.age>18</person.age>
</properties>
<build>
<filters>
<filter> filter-dev.properties</filter>
</filters>
</build>
</profile>
<profile>
<id>prod</id>
<properties>
<person.name>jack</person.name>
<person.age>55</person.age>
</properties>
<build>
<filters>
<filter> filter-prod.properties</filter>
</filters>
</build>
</profile>
</profiles>
1.4 测试
用个Demo来测试下效果吧!创建两个类Person和School
public class Person {
private int age;
private String name;
//get set toString方法省略
}
public class School {
private String shcoolName;
//get set toString方法省略
}
在XML文件配置这两个Bean
<bean id="person" class="com.example.domain.Person">
<property name="name" value="${person.name}"></property>
<property name="age" value="${person.age}"></property>
</bean>
<bean id="school" class="com.example.domain.School">
<property name="shcoolName" value="${school.name}"></property>
</bean>
添加一个测试用例
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class BaseTest {
@Autowired
private Person person;
@Autowired
private School school;
@Test
public void test1(){
System.out.println(person);
}
@Test
public void test2(){
System.out.println(school);
}
}
进入项目目录,输入如下命令(记得配置maven的环境变量,否则不能使用mvn命令进行编译)。简单解释下这条命令的意思,先清除原来已经编译好的文件,然后再编译打包。"-P"后面跟的就是我们之前配置环境的名称(profile结点下id配置的名称),"dev"说明我们采用了dev的配置。
mvn clean package -Pdev
打包过程中会执行测试代码,所有我们在控制台下可以看到测试结果

我们在换成"prod"试下,输入如下命令,结果也相应改变了。
mvn clean package -Prod

如果大家使用的是IDEA,右侧有Maven Projects选项,点击Profiles选择你需要的环境。直接运行即可。
2 插件的方式
这种方式就是利用插件打包指定环境下的文件。代码参见:https://github.com/gitlxp1101/isolation/tree/model2
2.1 为不同的环境创建不同的文件夹
首先我们先建立几个不同的文件夹,resources.dev和resources.prod,里面都放着一个名为data.properties的文件,有着相同的属性,但是值不同。

2.2 声明profile
下面这段配置主要有用的是声明了一个属性deploy.type,值为dev。
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<deploy.type>dev</deploy.type>
</properties>
</profile>
2.3 指定打包目录
声明要打包的目录,注意这条配置:"src/main/resources.${deploy.type}","${deploy.type}"的值就是我们在"profile"中配置的属性。这样maven会先去读取deploy.type的值,然后去找到相应的文件夹,将其打包。
<resources>
<resource>
<!--根据不同的环境deploy.type值也会不同-->
<directory>src/main/resources.${deploy.type}</directory>
<!--排除不需要打包的文件-->
<excludes>
<exclude>*.jsp</exclude>
</excludes>
</resource>
<resource>
<!--这个也是maven默认会打包的文件夹-->
<directory>src/main/resources</directory>
</resource>
</resources>
2.4 测试
测试Bean:Student
public class Student {
private String studentName;
//get set toString方法省略
}
在XML文件中加载属性文件和配置Student的实例
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<value>classpath:data.properties</value>
</list>
</property>
<property name="fileEncoding" value="utf-8"/>
</bean>
<bean id="student" class="com.example.domain.Student">
<property name="studentName" value="${studen.name}"></property>
</bean>
测试类:BaseTest
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class BaseTest {
@Autowired
private Student student;
@Test
public void test(){
System.out.println(student);
}
}
输入相应命令查看结果:
mvn clean package -Pdev
mvn clean package -Pprod
具体结果与上节类似,这里就不贴出来了。
本文详细介绍了如何使用Maven实现构建环境隔离,包括filter模式和插件方式。通过filter模式,通过配置属性和过滤文件实现环境配置。而插件方式则通过为不同环境创建不同文件夹,结合Maven profiles指定打包目录。文中提供了具体的配置示例和测试步骤,适合开发者参考实践。

被折叠的 条评论
为什么被折叠?



