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环境变量的内容








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值