java-項目環境隔離實現

實現爲參考開源項目https://github.com/shuzheng/zheng.git,很不錯的項目java

1.demo結構git

2.pom中添加web項目各類包,重點在下面,profiles節點用來配置須要的環境,build --> filters讀取profile中的env加載指定的配置文件github

<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.test.group</groupId>
	<artifactId>mvnprofile</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>mvnprofile Maven Webapp</name>
	<url>http://maven.apache.org</url>

	<properties>
		<java-version>1.7</java-version>
		<org.springframework-version>4.2.1.RELEASE</org.springframework-version>
		<org.slf4j-version>1.7.12</org.slf4j-version>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>

		<!-- standard -->
		<dependency>
			<groupId>taglibs</groupId>
			<artifactId>standard</artifactId>
			<version>1.1.2</version>
		</dependency>
	</dependencies>

	<profiles>
		<profile>
			<id>dev</id>
			<properties>
				<env>dev</env>
			</properties>
			<activation>
				<activeByDefault>true</activeByDefault>
			</activation>
		</profile>
		<profile>
			<id>test</id>
			<properties>
				<env>test</env>
			</properties>
		</profile>
		<profile>
			<id>prod</id>
			<properties>
				<env>prod</env>
			</properties>
		</profile>
	</profiles>

    <build>
        <finalName>zheng-upms-server</finalName>
        <filters>
            <filter>src/main/resources/profiles/${env}.properties</filter>
        </filters>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <!-- jetty插件 -->
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <!--<version>9.0.0.v20130308</version>-->
                <version>9.2.7.v20150116</version>
                <configuration>
                    <scanIntervalSeconds>3</scanIntervalSeconds>
                    <webApp>
                        <contextPath>/</contextPath>
                    </webApp>
                    <httpConnector>
                        <port>1111</port>
                    </httpConnector>
                    <reload>automatic</reload>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <skipTests>true</skipTests>
                    <testFailureIgnore>true</testFailureIgnore>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

3.新建springMVC-servlet.xml測試配置是否生效web

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xmlns:mvc="http://www.springframework.org/schema/mvc"
	   xmlns:context="http://www.springframework.org/schema/context"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

	<!-- 根目錄'/'對應頁面 -->
	<mvc:view-controller path="/" view-name="/index.jsp"/>

	<context:property-placeholder location="classpath:config.properties"/>

	<!-- thymeleaf視圖 -->
	<bean id="templateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
		<property name="order" value="0"/>
		<property name="prefix" value="${zheng.ui.path}"/>
		<property name="suffix" value=""/>
		<property name="templateMode" value="HTML"/>
		<property name="cacheable" value="false"/>
		<property name="characterEncoding" value="UTF-8"/>
		<!--<property name="cacheable" value="true"/>-->
		<!--<property name="cacheTTLMs" value="10000"/>-->
	</bean>
	<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
		<property name="templateResolver" ref="templateResolver"/>
		<property name="enableSpringELCompiler" value="true"/>
	</bean>
	<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
		<property name="templateEngine" ref="templateEngine"/>
		<property name="characterEncoding" value="UTF-8"/>
		<property name="viewNames" value="/${app.name}/*"/>
	</bean>

</beans>

4.confi.properties文件內容spring

app.name=${app.name}
env=${profile.env}

5.dev.properties文件中的內容apache

profile.env=dev
app.name=xxxx-web-dev
zheng.ui.path=http://xxxxx:1000/

6.執行maven打包命令,默認會使用dev模式,指定打包的環境信息使用,mvn clean package -P test便可打包指定環境配置的包spring-mvc

7.打包後解壓查看xml文件中的佔位符會被替換爲指定環境的參數mvc

相關文章
相關標籤/搜索