maven的属性过滤功能

mavenproperties filter功能可以帮你自动替换配置文件中以${}包裹的变量。 
1. spring bean
的配置文件app.xml内容: 

Xml代码  <!--[if gte vml 1]><v:shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"> <v:stroke joinstyle="miter"/> <v:formulas> <v:f eqn="if lineDrawn pixelLineWidth 0"/> <v:f eqn="sum @0 1 0"/> <v:f eqn="sum 0 0 @1"/> <v:f eqn="prod @2 1 2"/> <v:f eqn="prod @3 21600 pixelWidth"/> <v:f eqn="prod @3 21600 pixelHeight"/> <v:f eqn="sum @0 0 1"/> <v:f eqn="prod @6 1 2"/> <v:f eqn="prod @7 21600 pixelWidth"/> <v:f eqn="sum @8 21600 0"/> <v:f eqn="prod @7 21600 pixelHeight"/> <v:f eqn="sum @10 21600 0"/> </v:formulas> <v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"/> <o:lock v:ext="edit" aspectratio="t"/> </v:shapetype><v:shape id="_x0000_i1025" type="#_x0000_t75" alt="收藏代码" href="javascript:void()" title="&quot;收藏这段代码&quot;" style='width:11.25pt; height:10.5pt' o:button="t"> <v:imagedata src="file:///C:\Users\ADMINI~1\AppData\Local\Temp\msohtml1\01\clip_image001.png" o:href="http://ztc.iteye.com/images/icon_star.png"/> </v:shape><![endif]--><!--[if !vml]--><!--[endif]-->

<!--[if !supportLists]-->1.      <!--[endif]-->...  

<!--[if !supportLists]-->2.      <!--[endif]-->      <bean id="dataSource" destroy-method="close"   

<!--[if !supportLists]-->3.      <!--[endif]-->           class="org.apache.commons.dbcp.BasicDataSource">  

<!--[if !supportLists]-->4.      <!--[endif]-->      <property name="driverClassName" value="${jdbc.driverClassName}"/>  

<!--[if !supportLists]-->5.      <!--[endif]-->      <property name="url" value="${jdbc.url}"/>  

<!--[if !supportLists]-->6.      <!--[endif]-->      <property name="username" value="${jdbc.username}"/>  

<!--[if !supportLists]-->7.      <!--[endif]-->      <property name="password" value="${jdbc.password}"/>  

<!--[if !supportLists]-->8.      <!--[endif]-->  </bean>  

<!--[if !supportLists]-->9.      <!--[endif]-->...  


该配置文件放置在src/main/resources目录下。 

2.pom.xml
中的build标记内添加: 

Xml代码  <!--[if gte vml 1]><v:shape id="_x0000_i1026" type="#_x0000_t75" alt="收藏代码" href="javascript:void()" title="&quot;收藏这段代码&quot;" style='width:3.75pt;height:3pt' o:button="t"> <v:imagedata src="file:///C:\Users\ADMINI~1\AppData\Local\Temp\msohtml1\01\clip_image001.png" o:href="http://ztc.iteye.com/images/icon_star.png"/> </v:shape><![endif]--><!--[if !vml]--><!--[endif]-->

<!--[if !supportLists]-->1.      <!--[endif]--><resources>  

<!--[if !supportLists]-->2.      <!--[endif]-->        <resource>  

<!--[if !supportLists]-->3.      <!--[endif]-->            <directory>src/main/resources</directory>  

<!--[if !supportLists]-->4.      <!--[endif]-->            <filtering>true</filtering>  

<!--[if !supportLists]-->5.      <!--[endif]-->        </resource>  

<!--[if !supportLists]-->6.      <!--[endif]-->    </resources>  


以上xml片段的功能的目的是开启对src/main/resources目录下的property filtering功能。所以上面的配置表示在该目录下的所有文件都会被扫描。如果要指定具体的文件,可以使用includes标记,该标记支持ant路径表达式。 

3.
pom.xml中定义属性。 
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->

Xml代码  <!--[if gte vml 1]><v:shape id="_x0000_i1027" type="#_x0000_t75" alt="收藏代码" href="javascript:void()" title="&quot;收藏这段代码&quot;" style='width:3.75pt;height:3pt' o:button="t"> <v:imagedata src="file:///C:\Users\ADMINI~1\AppData\Local\Temp\msohtml1\01\clip_image001.png" o:href="http://ztc.iteye.com/images/icon_star.png"/> </v:shape><![endif]--><!--[if !vml]--><!--[endif]-->

<!--[if !supportLists]-->1.      <!--[endif]--><properties>  

<!--[if !supportLists]-->2.      <!--[endif]-->  <jdbc.driverClassName>  

<!--[if !supportLists]-->3.      <!--[endif]-->    com.mysql.jdbc.Driver</jdbc.driverClassName>  

<!--[if !supportLists]-->4.      <!--[endif]-->  <jdbc.url>jdbc:mysql://localhost:3306/development_db</jdbc.url>  

<!--[if !supportLists]-->5.      <!--[endif]-->  <jdbc.username>dev_user</jdbc.username>  

<!--[if !supportLists]-->6.      <!--[endif]-->  <jdbc.password>s3cr3tw0rd</jdbc.password>  

<!--[if !supportLists]-->7.      <!--[endif]--></properties>  



4.
编译。 

mvn compile
 


现在查看target/classes目录的app.xml。你会发现xml中的所有变量都被替换了: 

Xml代码  <!--[if gte vml 1]><v:shape id="_x0000_i1028" type="#_x0000_t75" alt="收藏代码" href="javascript:void()" title="&quot;收藏这段代码&quot;" style='width:11.25pt;height:10.5pt' o:button="t"> <v:imagedata src="file:///C:\Users\ADMINI~1\AppData\Local\Temp\msohtml1\01\clip_image001.png" o:href="http://ztc.iteye.com/images/icon_star.png"/> </v:shape><![endif]--><!--[if !vml]--><!--[endif]-->

<!--[if !supportLists]-->1.      <!--[endif]--><bean id="dataSource" destroy-method="close"   

<!--[if !supportLists]-->2.      <!--[endif]-->            class="org.apache.commons.dbcp.BasicDataSource">  

<!--[if !supportLists]-->3.      <!--[endif]-->       <property name="driverClassName" value="com.mysql.jdbc.Driver"/>  

<!--[if !supportLists]-->4.      <!--[endif]-->       <property name="url" value="jdbc:mysql://localhost:3306/development_db"/>  

<!--[if !supportLists]-->5.      <!--[endif]-->       <property name="username" value="dev_user"/>  

<!--[if !supportLists]-->6.      <!--[endif]-->       <property name="password" value="s3cr3tw0rd"/></bean>  



结合profile功能,你可以在软件发布之前,定义不同的变量。 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 我可以为您提供一些有关如何配置Druid和Maven的建议。首先,您需要将Druid JAR文件添加到Maven的本地存储库中,然后在您的Maven POM文件中添加Druid的依赖项。其次,您需要在运行Maven构建过程之前,使用特定的插件将Druid添加到您的应用程序中。最后,您可以使用Maven启动Druid实例,以便能够实现您想要的功能。 ### 回答2: Maven是一个开源的项目管理工具,可以用于构建、管理和发布Java项目。而Druid是一个开源数据库连接池框架,提供了强大的监控和扩展功能。 要在Maven中配置Druid,首先需要在项目的pom.xml文件中添加Druid的依赖项。可以在Maven仓库中搜索Druid的groupId和artifactId,然后将其添加到pom.xml文件的dependencies标签中。 配置Druid的数据源可以通过在Spring Boot配置文件中进行设置。可以在application.properties或application.yaml文件中添加Druid的相关配置项,如jdbc.url、jdbc.username、jdbc.password等。 在配置Druid的过程中,还可以设置一些Druid的监控和统计功能。可以通过添加FilterRegistrationBean来将Druid的StatFilter和WebStatFilter注册到Servlet容器中,以便进行监控和统计数据库访问情况。同时,还可以配置Druid的监控页面路径和用户名密码等信息。 完成以上配置后,就可以在项目中使用Druid的连接池功能了。可以在代码中通过注解或配置文件的方式获取Druid的数据源,并在需要数据库访问的地方使用该数据源。 总之,通过在Maven配置文件中添加Druid的依赖项,然后在Spring Boot配置文件中进行Druid的相关配置,就可以在项目中使用Druid的连接池功能以及监控和统计功能了。这样可以更好地管理和优化数据库的使用。 ### 回答3: 要配置Maven来使用Druid数据库连接池,需要进行以下几个步骤: 1. 在项目的pom.xml文件中,添加Druid的Maven依赖。可以在Maven仓库中找到最新版本的Druid依赖,将其添加到dependencies标签中。 2. 在项目的src/main/resources目录下创建一个名为application.properties(或者其他支持的配置文件格式,如application.yml)的文件。在该文件中,配置Druid的相关属性,如数据库URL、用户名、密码等。 3. 创建一个配置类,例如DruidConfig.java。在该类中,使用@Configuration注解将其声明为一个配置类,并使用@Value注解来读取application.properties中的属性。 4. 在DruidConfig类中,使用@Bean注解创建一个DruidDataSource对象,并将读取到的配置信息设置给该对象。 5. 可选地,可以在DruidConfig类中添加一些其他的配置,例如监控页面的访问路径、过滤器等。 6. 将DruidConfig类添加到Spring Boot应用程序的主类(通常是项目名Application.java)中,使用@EnableConfigurationProperties注解启用配置属性。 7. 重新编译和运行项目,Maven会自动下载Druid的依赖并将其配置到应用程序中。 通过以上步骤,我们就成功地配置了Maven来使用Druid数据库连接池。Druid连接池可以提供高性能、稳定可靠的数据库连接管理,适用于各种规模的项目。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值