maven 自定义标签给properties文件赋值

本文详细介绍如何在Maven项目中配置编译插件、资源插件及自定义属性,并探讨了不同方式加载properties文件的效果及其优缺点。此外,还介绍了如何在Java代码和properties文件中使用这些自定义属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

编译插件:maven-compiler-plugin

资源插件:maven-resources-plugin

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

[html]  view plain  copy
  1. <build>  
  2.     <finalName>useradmin-user-webapp</finalName>  
  3.     <resources>  
  4.             <resource>  
  5.                 <directory>${project.basedir}/src/main/resources</directory>  
  6.                 <filtering>true</filtering>  
  7.                 <includes>  
  8.                     <include>**/*</include>  
  9.                 </includes>  
  10.             </resource>  
  11.             <resource>  
  12.                 <directory>${project.basedir}/src/main/webapp/WEB-INF</directory>  
  13.                 <filtering>true</filtering>  
  14.                 <includes>  
  15.                     <include>**/*</include>  
  16.                 </includes>  
  17.             </resource>  
  18.         </resources>  
  19.         <plugins>  
  20.             <plugin>  
  21.                 <groupId>org.apache.maven.plugins</groupId>  
  22.                 <artifactId>maven-compiler-plugin</artifactId>  
  23.                 <version>3.5.1</version>  
  24.                 <configuration>  
  25.                     <source>1.8</source>  
  26.                     <target>1.8</target>  
  27.                     <!-- 设置编译的字符集编码和环境编码 -->  
  28.                     <encoding>UTF-8</encoding>  
  29.                     <compilerArguments>  
  30.                         <extdirs>src/main/webapp/WEB-INF/lib</extdirs>  
  31.                     </compilerArguments>  
  32.                 </configuration>  
  33.             </plugin>  
  34.             <plugin>  
  35.                 <groupId>org.apache.maven.plugins</groupId>  
  36.                 <artifactId>maven-resources-plugin</artifactId>  
  37.                 <version>2.6</version>  
  38.                 <configuration>  
  39.                     <nonFilteredFileExtensions>  
  40.                         <nonFilteredFileExtension>  
  41.                             dat  
  42.                         </nonFilteredFileExtension>  
  43.                         <nonFilteredFileExtension>  
  44.                             swf  
  45.                         </nonFilteredFileExtension>  
  46.                     </nonFilteredFileExtensions>  
  47.                 </configuration>  
  48.             </plugin>  
  49.         </plugins>  
  50.   </build>  
pom文件定义maven的自定义属性mvn.pwd

[html]  view plain  copy
  1. <profiles>  
  2.         <profile>  
  3.             <id>local</id>  
  4.             <activation>  
  5.                 <activeByDefault>true</activeByDefault>  
  6.             </activation>  
  7.             <properties>  
  8.                 <mvn.pwd>123</mvn.env>  
  9.             </properties>  
  10.         </profile>  
  11.   </profiles>  


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

[html]  view plain  copy
  1. password=${mvn.pwd}  
application的xml文件中加载properties文件

[html]  view plain  copy
  1. <!-- <bean id="propertyConfigurer"  
  2.         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  3.         <property name="locations">  
  4.             <list>  
  5.                 <value>/WEB-INF/db-mysql.properties</value>  
  6.             </list>  
  7.         </property>  
  8.     </bean> -->  
  9.     <!-- <bean id="propertyConfigurer"  
  10.         class="org.springframework.beans.factory.config.PropertiesFactoryBean">  
  11.         <property name="locations">  
  12.             <list>  
  13.                 <value>/WEB-INF/db-mysql.properties</value>  
  14.             </list>  
  15.         </property>  
  16.     </bean> -->  
  17.       
  18.     <context:property-placeholder location="classpath:db-mysql.properties"  
  19.         ignore-unresolvable="true" />  
(1)第一种加载properties文件,maven的自定义变量无法赋值到properties中

[html]  view plain  copy
  1. <!-- <bean id="propertyConfigurer"  
  2.         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  3.         <property name="locations">  
  4.             <list>  
  5.                 <value>/WEB-INF/db-mysql.properties</value>  
  6.             </list>  
  7.         </property>  
  8.     </bean> -->  

不建议使用

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

[html]  view plain  copy
  1. <!-- <bean id="propertyConfigurer"  
  2.         class="org.springframework.beans.factory.config.PropertiesFactoryBean">  
  3.         <property name="locations">  
  4.             <list>  
  5.                 <value>/WEB-INF/db-mysql.properties</value>  
  6.             </list>  
  7.         </property>  
  8.     </bean> -->  

如:

maxWait=1000

该属性变量用到项目中时

如:

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

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

[html]  view plain  copy
  1. <context:property-placeholder location="classpath:db-mysql.properties"  
  2.         ignore-unresolvable="true" />  
详细参考: http://blog.youkuaiyun.com/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 3.9.9 安装与配置 IntelliJ IDEA 指南 #### 一、Maven 的安装路径 在 Windows 系统上,如果使用的是 JetBrains 提供的自带 Maven,则其默认安装路径位于 `C:\Program Files\JetBrains\IntelliJ IDEA xxx\plugins\maven\lib\maven3\conf`[^1]。然而,为了更灵活地管理项目依赖和版本控制,建议手动安装独立版 Maven。 #### 二、下载并解压 Maven 访问官方网址 https://maven.apache.org,在 Download 页面选择适合的操作系统版本进行下载。下载完成后将其解压至目标目录,例如 `D:\Program Files\apache-maven-3.9.9-bin`[^4]。 #### 三、配置环境变量 完成解压后,需设置系统的环境变量以便全局调用 Maven 命令: 1. 打开系统属性 -> 高级 -> 环境变量; 2. 新建或编辑 `MAVEN_HOME` 变量,赋值Maven 解压后的根目录(如 `D:\Program Files\apache-maven-3.9.9-bin`); 3. 将 `%MAVEN_HOME%\bin` 添加到 `Path` 环境变量中。 验证是否成功通过命令行输入 `mvn -v`,应返回当前 Maven 版本及相关 JDK 信息。 #### 四、修改本地仓库地址 打开 Maven 的配置文件 `settings.xml`,通常位于 `${MAVEN_HOME}\conf` 文件夹下。在此文件中的 `<localRepository>` 节点指定自定义存储库路径,例如: ```xml <localRepository>D:\Program Files\apache-maven-3.9.9-bin\maven-repository</localRepository> ``` 此操作可优化磁盘空间分配,并便于统一管理不同项目的依赖项[^2]。 #### 五、IDEA 中集成 Maven 启动 IntelliJ IDEA 后按照如下步骤操作: 1. **新建项目时启用 Maven 支持** 创建新工程时勾选 “Create from template” 并选择支持的语言模板,随后确认自动加载 pom.xml 文件。 2. **已有项目导入 Maven** 对于已存在的 Java 工程,可通过 File -> Project Structure -> Modules 设置模块对应的 SDK 和语言级别;接着切换到 Dependencies Tab 导入所需的 jar 包或者更新现有的构建脚本。 3. **调整编译器兼容性** 进入 Settings/Preferences -> Build, Execution, Deployment -> Compiler -> Java Compiler,确保源码编码标准匹配实际使用的 JRE/JDK 版本号。另外还需参照以下 XML 结构补充 profile 数据来锁定具体的 jdk 参数[^3]: ```xml <profile> <id>jdk-1.8</id> <activation> <activeByDefault>true</activeByDefault> <jdk>1.8</jdk> </activation> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion> </properties> </profile> ``` 至此完成了整个流程描述,后续可以利用插件执行打包任务或是调试远程服务端口等功能扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值