Using Maven settings.xml properties inside Spring context

前端 未结 3 1778
隐瞒了意图╮
隐瞒了意图╮ 2020-12-20 18:17

I\'ve got a Maven settings.xml file in my ~/.m2 directory; it looks like this:


    
        

        
相关标签:
3条回答
  • 2020-12-20 18:57

    I recommend you to use a property file in the middle. I mean: Spring application would load properties values form the property file using context:property-placeholder and Maven would be the one who replace ${...} variables using values from settings.xml using filtering.

    Your property file:

    db.driver=${db.driver}
    db.url=${db.url}
    username=${username}
    password=${password}
    

    Your servlet-context.xml file

    <context:property-placeholder location="classpath:your-property-file.properties" />
    
    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName"><value>${db.driver}</value></property>
        <property name="url"><value>${db.url}</value></property>
        <property name="username"><value>${username}</value></property>
        <property name="password"><value>${password}</value></property>
    </bean>
    

    In your pom.xml

    <resources>
        ...
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
        ...
    </resources>
    
    0 讨论(0)
  • 2020-12-20 19:03

    There is a way of filtering web resources by configuration of Maven War Plugin. Look at this for a snippet from official plugin's docs.

    And by the way, I strongly recommend reconsidering this filtering-based way for providing de facto run-time configuration at build-time. Just notice that you have to rebuild the same code to just prepare package for another environment (or alternatively edit package contents). You can use application server's specific stuff for this (at least JBoss has one) or use Spring that AFAIR also can be configured like this.

    0 讨论(0)
  • 2020-12-20 19:15

    I haven't tried it, but as per this maven wiki page, you should be able to refer to properties in settings.xml using settings. prefix. So ${settings.servers.server.username} should ideally return the username in settings.xml.

    0 讨论(0)
提交回复
热议问题