我是靠谱客的博主 开放彩虹,最近开发中收集的这篇文章主要介绍maven自定义属性给properties文件赋值,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1、maven的pom文件和主pom文件要配置相关插件和资源路径

编译插件:maven-compiler-plugin

资源插件:maven-resources-plugin

配置资源路径:resources,指向到相关需要覆盖的路径(properties文件所在位置)

<build>
<finalName>useradmin-user-webapp</finalName>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*</include>
</includes>
</resource>
<resource>
<directory>${project.basedir}/src/main/webapp/WEB-INF</directory>
<filtering>true</filtering>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<!-- 设置编译的字符集编码和环境编码 -->
<encoding>UTF-8</encoding>
<compilerArguments>
<extdirs>src/main/webapp/WEB-INF/lib</extdirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>
dat
</nonFilteredFileExtension>
<nonFilteredFileExtension>
swf
</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
</plugins>
</build>
pom文件定义maven的自定义属性mvn.pwd

<profiles>
<profile>
<id>local</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<mvn.pwd>123</mvn.env>
</properties>
</profile>
</profiles>


2、properties文件中用maven自定义属性给password赋值

password=${mvn.pwd}
application的xml文件中加载properties文件

<!-- <bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>/WEB-INF/db-mysql.properties</value>
</list>
</property>
</bean> -->
<!-- <bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>/WEB-INF/db-mysql.properties</value>
</list>
</property>
</bean> -->
<context:property-placeholder location="classpath:db-mysql.properties"
ignore-unresolvable="true" />
(1)第一种加载properties文件,maven的自定义变量无法赋值到properties中

<!-- <bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>/WEB-INF/db-mysql.properties</value>
</list>
</property>
</bean> -->

不建议使用

(2)第二种加载properties文件,如果properties里面变量赋值的是数字,实际它是当作字符串处理

<!-- <bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>/WEB-INF/db-mysql.properties</value>
</list>
</property>
</bean> -->

如:

maxWait=1000

该属性变量用到项目中时

如:

<property name="maxWait" value="${maxWait}" />这里name=“maxWait”需要的是int类型,${maxWait}赋值的是数字,会报错类型不对

(3)第三种加载properties文件最好,不会出现其他奇怪的问题

<context:property-placeholder location="classpath:db-mysql.properties"
ignore-unresolvable="true" />
详细参考: http://blog.csdn.net/nokia_lc/article/details/52084172


3、maven自定义属性、内置属性、pom属性、环境变量属性,都可用于项目中内容赋值

java代码中赋值,利用spring的@value()

@value("mvn.pwd")

String pwd;

properties文件赋值

basepath=${project.basedir}

4、maven也可以获得系统环境变量

${env.JAVA_HOME}

可以获得JAVA_HOME环境变量的内容








最后

以上就是开放彩虹为你收集整理的maven自定义属性给properties文件赋值的全部内容,希望文章能够帮你解决maven自定义属性给properties文件赋值所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(60)

评论列表共有 0 条评论

立即
投稿
返回
顶部