spring+maven不同环境读取配置详解
来源:互联网 发布:linux添加路由指定网卡 编辑:程序博客网 时间:2024/05/22 12:40
首先这个我看了网上很多资料,但我发现,由于自己一些技术的不熟悉,对于他人的文章有些误解,导致我打包部署失败。
话不多说,现在我们开始一步步工程
第一步,项目读取不通环境的配置文件
<?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:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- 测试环境配置文件 --> <beans profile="local"> <context:property-placeholder location="classpath:local/config.properties" /> </beans> <!-- 生产环境配置文件 --> <beans profile="product"> <context:property-placeholder location="classpath:product/config.properties" /> </beans></beans>
这是我定义的一个读配置文件的xml——applicationContext-profile.xml
这是由web.xml里面配置,来区分去读哪个配置文件
<!-- 配置spring的默认profile 可选值:product(生产环境) local(本地环境) --> <context-param> <param-name>spring.profiles.active</param-name> <param-value>product</param-value> </context-param> <!-- spring hibernate --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:public/spring/applicationContext-profile.xml classpath:public/spring/spring-hibernate.xml </param-value> </context-param>
web.xml的一部分,根据配置去读product还是local,这里根据配置字段能看到对于的配置文件,这对应关系应该是能看懂的!
到这里,你应该能根据自己的配置文件去运行程序
第二步,根据pom文件打包
<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/xsd/maven-4.0.0.xsd"> <parent> <artifactId>officialProject</artifactId> <groupId>com.wm</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>offical-web</artifactId> <packaging>war</packaging> <name>offical-web</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <shiro-version>1.3.2</shiro-version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>com.wm</groupId> <artifactId>offical-manager-service</artifactId> <type>jar</type> </dependency> <dependency> <groupId>com.wm</groupId> <artifactId>official-manager-core</artifactId> <type>jar</type> </dependency> <dependency> <groupId>com.wm</groupId> <artifactId>offical-common</artifactId> <type>jar</type> </dependency> <!-- mysql数据库驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.37</version> </dependency> <!--后台权限控制框架--> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>${shiro-version}</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>${shiro-version}</version> </dependency> </dependencies> <profiles> <profile> <id>local</id><!-- 本地开发环境 --> <properties> <package.env>local</package.env> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <id>product</id><!-- 生产环境 --> <properties> <package.env>product</package.env> </properties> </profile> </profiles> <build> <finalName>${project.artifactId}</finalName> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <excludes> <exclude>local/*</exclude> <exclude>product/*</exclude> </excludes> <includes> <include>public/**/*</include> </includes> </resource> <resource> <directory>src/main/java</directory> <includes> <include>com/wm/back/entity/**/hbm/*.hbm.xml</include> </includes> </resource> </resources> <plugins> <!-- jetty插件 --> <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.3.8.v20160314</version> <configuration> <webAppConfig> <contextPath>/</contextPath> <defaultsDescriptor>src/main/resources/public/webdefault.xml</defaultsDescriptor> </webAppConfig> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.1.1</version> <configuration> <archive> <addMavenDescriptor>false</addMavenDescriptor> </archive> <warName>${project.artifactId}</warName> <webResources> <!-- 不同的环境,使用不同的配置文件 --> <resource> <directory>src/main/resources/${package.env}</directory> <targetPath>WEB-INF/classes/${package.env}</targetPath> <filtering>true</filtering> </resource> <!-- 公共的配置文件 --> <resource> <directory>src/main/resources/public</directory> <!--targetPath用来指定文件放到哪里--> <targetPath>WEB-INF/classes/public</targetPath> <filtering>true</filtering> </resource> <resource> <directory>src/main/webapp/WEB-INF/config</directory> <targetPath>WEB-INF</targetPath> <filtering>true</filtering> <includes> <include>web.xml</include> </includes> </resource> </webResources> </configuration> </plugin> </plugins> </build></project>
先贴出全部代码,然后再一一作出解释
<profiles> <profile> <id>local</id><!-- 本地开发环境 --> <properties> <package.env>local</package.env> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <id>product</id><!-- 生产环境 --> <properties> <package.env>product</package.env> </properties> </profile></profiles>
properties里配置的,下文都是可以使用的,我这里配置package.env ,所以下面使用package.env就是代表local或者product根据我传入的 -P后面的那个值来决定
打包语句是mvn clean package -P +(我传入的值 profile的id)来决定package.env的变量
完整的打包语句是mvn clean package -P local
<activeByDefault>true</activeByDefault>来决定激活哪个profile
<resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <excludes> <exclude>local/*</exclude> <exclude>product/*</exclude> </excludes> <includes> <include>public/**/*</include> </includes> </resource> <resource> <directory>src/main/java</directory> <includes> <include>com/wm/back/entity/**/hbm/*.hbm.xml</include> </includes> </resource></resources>
在我的理解下,不加resources的话,所有项目里的resources文件都会被加载,但java里面的配置文件可能还是加载不了的
但你定义了resources,你就要把你需要通过加载的文件都要写出来,不然他不会去加载。
我这里把各环境的配置文件都取消了加载,把通用的资源文件进行了加载。
忘记提一句,这样下,项目本地启动的时候会报错,因为你把local或product都去掉了,程序找不到里面的文件。这只是打包的时候才试用
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.1.1</version> <configuration> <archive> <addMavenDescriptor>false</addMavenDescriptor> </archive> <warName>${project.artifactId}</warName> <webResources> <!-- 不同的环境,使用不同的配置文件 --> <resource> <directory>src/main/resources/${package.env}</directory> <targetPath>WEB-INF/classes/${package.env}</targetPath> <filtering>true</filtering> </resource> <!-- 公共的配置文件 --> <resource> <directory>src/main/resources/public</directory> <!--targetPath用来指定文件放到哪里--> <targetPath>WEB-INF/classes/public</targetPath> <filtering>true</filtering> </resource> <resource> <directory>src/main/webapp/WEB-INF/config</directory> <targetPath>WEB-INF</targetPath> <filtering>true</filtering> <includes> <include>web.xml</include> </includes> </resource> </webResources> </configuration></plugin>
打war包的时候需要这样配置
directory:代表你要加载的目录
targetPath:代表你要把你加载的文件放在哪里
<filtering>true</filtering>这个网上说一定要设置为true,在我看来没什么关系,因为我根据命令打包,也能把文件打出来
<resource> <directory>src/main/webapp/WEB-INF/config</directory> <targetPath>WEB-INF</targetPath> <filtering>true</filtering> <includes> <include>web.xml</include> </includes> </resource>
我这里是为了把web.xml选择读哪个配置文件给替换掉
为什么要这样设计能,因为如果不这样,我们本地启动项目,pom是不会吧${package.env}这个替换的。所以这样会报错。
但我们打包的时候希望灵活替换。所以就写一个config文件目录,里面放过web.xml,当要打包的时候替换WEB-INF下的web.xml文件就可以了
到此,一个完整的流程就走完了
阅读全文
0 0
- spring+maven不同环境读取配置详解
- Maven根据Profile读取不同配置环境配置文件
- Maven根据Profile读取不同配置环境配置文件
- Maven根据Profile读取不同配置环境配置文件
- Maven根据Profile读取不同配置环境配置文件
- Maven根据Profile读取不同配置环境配置文件
- Maven不同环境配置
- maven 不同环境加载不同spring配置文件
- maven不同环境的参数配置
- maven解决不同环境属性配置问题
- springboot + profile(不同环境读取不同配置)
- 第十四章 springboot + profile(不同环境读取不同配置)
- springboot + profile(不同环境读取不同配置)
- SpringBoot 不同环境读取不同的配置信息文件
- maven项目配置,不同环境使用不同参数
- maven根据不同环境打包不同的配置
- Maven多环境配置使用filter过滤不同环境
- spring boot 不同的环境使用不同的配置
- 欢迎使用CSDN-markdown编辑器
- 脏读、幻读、不可重复读和丢失更新
- java基础
- ADS软件连接SQLSERVER报错
- Javaweb开发为什么需要分前后端,放弃原生JSP
- spring+maven不同环境读取配置详解
- Druid 介绍及配置
- JAVA基础知识!!
- linux关闭防火墙&开启防火墙
- Linux常用操作命令
- redis客户端连接(error) NOAUTH Authentication required
- 数独
- 【深度分析】Java的volatile
- sdut 3346 &&sdut 3344 Runtime Error Runtime Error?