意义在于 消除重复 ,示例代码如下。
<project> <properties> <springframework.groupId>org.springframework</springframework.groupId> <springframework.version>5.2.1.RELEASE</springframework.version> </properties> <dependencies> <dependency> <groupId>${springframework.groupId}</groupId> <artifactId>spring-web</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>${springframework.groupId}</groupId> <artifactId>spring-aop</artifactId> <version>${springframework.version}</version> </dependency> </dependencies> </project>
内置属性
两个常用的内置属性:
${basedir} ${version}
pom属性
用户可以使用该类属性引用pom文件中对应元素的值。
常用的pom属性如下:
${project.build.sourceDirectory} ${project.build.testSourceDirectory} ${project.build.directory} ${project.outputDirectory} ${project.testOutputDirectory} ${project.groupId} ${project.artifactId} ${project.version} ${project.build.finalName}
settings属性
用户可以使用该类属性引用settings文件中对应元素的值。
${settings.localRepository}
:用户本地仓库 Java系统属性
mvn help:system ${user.home}
环境变量属性
mvn help:system ${env.JAVA_HOME}
遇到的问题
解决
示例
数据库配置
# src/main/resources/application.properties database.jdbc.driverClass=${db.driver} database.jdbc.connectionUrl=${db.url} database.jdbc.username=${db.user} database.jdbc.password=${db.pw}
开启资源过滤
<project> <properties> <db.driver>com.mysql.jdbc.Driver</db.driver> <db.url>jdbc:mysql//localhost:3306/test</db.url> <db.user>dev-user</db.user> <db.pw>dev-pwd</db.pw> </properties> <build> <resources> <resource> <directory>${project.basedir}/src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </build> </project>
编译代码 mvn compile
database.jdbc.driverClass=com.mysql.jdbc.Driver database.jdbc.connectionUrl=jdbc:mysql//localhost:3306/test database.jdbc.username=aaa database.jdbc.password=aaa-pwd
针对不同环境的profile
示例:基于开发和测试环境的profile
<project> <profiles> <profile> <id>dev</id> <properties> <db.driver>com.mysql.jdbc.Driver</db.driver> <db.url>jdbc:mysql//localhost:3306/test</db.url> <db.user>dev</db.user> <db.pw>dev-pwd</db.pw> </properties> </profile> <profile> <id>test</id> <properties> <db.driver>com.mysql.jdbc.Driver</db.driver> <db.url>jdbc:mysql//localhost:3306/test</db.url> <db.user>test</db.user> <db.pw>test-pwd</db.pw> </properties> </profile> </profiles> </project>
激活profile
命令行激活
-P
加上profile的 id
,多个id之间逗号分隔 mvn clean compile -Pdev
默认激活
配置 profile > activation > activationByDefault
元素的值为 true
,该profile默认激活
<project> <profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <db.driver>com.mysql.jdbc.Driver</db.driver> <db.url>jdbc:mysql//localhost:3306/test</db.url> <db.user>dev</db.user> <db.pw>dev-pwd</db.pw> </properties> </profile> </profiles> </project>
系统属性激活
mvn ... -D属性xx=属性xx的值
指定的属性存在,并且值等于x时,激活profile
<project> <profiles> <profile> <id>dev</id> <activation> <property> <name>test</name> <value>x</value> </property> </activation> <properties> <db.driver>com.mysql.jdbc.Driver</db.driver> <db.url>jdbc:mysql//localhost:3306/test</db.url> <db.user>dev</db.user> <db.pw>dev-pwd</db.pw> </properties> </profile> </profiles> </project>
文件存在与否激活
<project> <profiles> <profile> <id>dev</id> <activation> <file> <exists>x.properties</exists> <missing>y.properties</missing> </file> </activation> <properties> <db.driver>com.mysql.jdbc.Driver</db.driver> <db.url>jdbc:mysql//localhost:3306/test</db.url> <db.user>dev</db.user> <db.pw>dev-pwd</db.pw> </properties> </profile> </profiles> </project>
查看当前激活的profile
mvn help:active-profiles
列出所有的profile
mvn help:all-profiles