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>
- <profiles>
- <profile>
- <id>local</id>
- <activation>
- <activeByDefault>true</activeByDefault>
- </activation>
- <properties>
- <mvn.pwd>123</mvn.env>
- </properties>
- </profile>
- </profiles>
- password=${mvn.pwd}
- <!-- <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" />
- <!-- <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" />
3、maven自定义属性、内置属性、pom属性、环境变量属性,都可用于项目中内容赋值
java代码中赋值,利用spring的@value()
@value("mvn.pwd")
String pwd;
properties文件赋值
basepath=${project.basedir}
4、maven也可以获得系统环境变量
${env.JAVA_HOME}
可以获得JAVA_HOME环境变量的内容