使用Maven和Spring控制项目:如何使用Maven配置文件设置Spring配置文件? [英] Controlling a project with Maven and Spring: How to set Spring config file using Maven profiles?

查看:114
本文介绍了使用Maven和Spring控制项目:如何使用Maven配置文件设置Spring配置文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据某个Maven配置文件是否处于活动状态来配置包含数据库信息的Spring配置文件。我已经看到了这些问题的答案,但我无法将它们放在一起。



我有一个像这样的Maven个人资料:

 < profiles> 
< profile>
< id> production< / id>
< activation>
< property>
< name> environment.type< / name>
< value> prod< / value>
< / property>
< / activation>
< / profile>

< profile>
< id> development< / id>
< activation>
< property>
< name> environment.type< / name>
< value> dev< / value>
< / property>
< / activation>

<! - Spring的数据库属性 - >
< properties>
< db.driver> oracle.jdbc.driver.OracleDriver< /db.driver>
< db.type> oracle< /db.type>
< db.host> 192.168.0.0< /db.host>
< db.port> 1521< /db.port>
< db.name> myDb< /db.name>
< db.url> jdbc:$ {db.type}:thin:@ $ {db.host}:$ {db.port}:$ {db.name}< /db.url>
< / properties>

还有一个这样的settings.xml文件:

 < servers> 
< server>
< id> development< / id>
< username> jsmith< / username>
< password> secret< / password>
< / server>
< / servers>

....

< profiles>
< profile>
< activation>
< activeByDefault> true< / activeByDefault>
< / activation>

< properties>
< environment.type> dev< /environment.type>
< / properties>
< / profile>
< / profiles>

在servlet-context.xml中:



< pre class =lang-xml prettyprint-override> < bean id =dataSourceclass =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> $ {db.username}< / value>
< / property>

< property name =password>
< value> $ {db.password}< / value>
< / property>

< property name =maxActive>
< value> 10< / value>
< / property>

< property name =maxIdle>
< value> 1< / value>
< / property>
< / bean>

我的问题基本上是,我如何将maven属性放入servlet-context.xml文件中?我需要.properties文件吗?我对Spring中的Maven和PropertyPlaceholderConfigurer的过滤有所了解,但我不知道如何将它们放在一起 - 或者它们是否一起使用?或者有更简单的方法吗?

解决方案

使用我从两个答案和我的研究中学到的东西,我得到了一个开发/生产系统,由pom控制,设置正确的数据库值。



首先,在pom中,我创建了两个配置文件。

 <! - 生产和开发资料 - > 

< profiles>
<! - 耐克个人资料需要到这里 - >
< profile>
< id> production< / id>
< activation>
< property>
< name> environment.type< / name>
< value> prod< / value>
< / property>
< / activation>
< / profile>

<! - 催化剂配置文件需要到这里 - >
< profile>
< id> development< / id>
< activation>
< property>
< name> environment.type< / name>
< value> dev< / value>
< / property>
< / activation>

< build>

<! - 这包含Spring上下文文件的属性。
数据库值将是对开发的更改。 - >
< filters>
< filter> src / main / resources / dev.database.properties< / filter>
< / filters>

< resources>

<! - 这是保存Spring上下文
文件的目录。搜索整个文件夹,因此要么没有
其他文件应该有占位符,要么
排除其他文件被过滤。 - >
< resource>
< directory> src / main / webapp / WEBINF /< / directory>
< filtering> true< / filtering>
< / resource>
< / resources>
< / profile>
< / profiles>

在servlet-context.xml中,在WEBINF目录中,我放置了占位符:

 <! - 对于数据库,使用maven过滤来填充占位符 - > 
< bean id =dataSourceclass =org.apache.commons.dbcp.BasicDataSourcedestroy-method =close>
< property name =driverClassNamevalue =$ {db.driver}/>
< property name =urlvalue =$ {db.url}/>
< property name =usernamevalue =$ {db.username}/>
< property name =passwordvalue =$ {db.password}/>
< property name =maxActive>
< value> 10< / value>
< / property>
< property name =maxIdle>
< value> 1< / value>
< / property>
< / bean>

然后我创建了一个属性文件,位于src / main / resources

 
#开发数据库属性文件

db.driver = oracle.jdbc.driver.OracleDriver
db.url = jdbc:oracle:thin:[USER / PASSWORD] @ [HOST] [:PORT]:SID
db.username = jsmith
db.password = s3cr3t

然后我可以用

 <$ c启动Maven $ c> mvn -P developpement clean install 

或者有一个settings.xml设置正确的配置文件我:

 < settings> 
< profiles>
< profile>
< activation>
< activeByDefault> true< / activeByDefault>
< / activation>

< properties>
< environment.type> dev< /environment.type>
< / properties>
< / profile>
< / profiles>
< / settings>


I am trying to configure a Spring configuration file with database information based on whether a certain Maven profile is active. I've seen pieces of answers to this but I'm having trouble putting it all together.

I have a Maven profile like this:

<profiles>
    <profile>
        <id>production</id>
        <activation>
            <property>
                <name>environment.type</name>
                <value>prod</value>
            </property>
        </activation>
    </profile>

    <profile>
        <id>development</id>
        <activation>
            <property>
                <name>environment.type</name>
                <value>dev</value>
            </property>
        </activation>

        <!-- Database properties for Spring -->
        <properties>
            <db.driver>oracle.jdbc.driver.OracleDriver</db.driver>
            <db.type>oracle</db.type>
            <db.host>192.168.0.0</db.host>
            <db.port>1521</db.port>
            <db.name>myDb</db.name>
            <db.url>jdbc:${db.type}:thin:@${db.host}:${db.port}:${db.name}</db.url>
        </properties>

And a settings.xml file like this:

<servers>
  <server>
    <id>development</id>
    <username>jsmith</username>
    <password>secret</password>
  </server>
</servers>

....

<profiles>
  <profile>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>

    <properties>
      <environment.type>dev</environment.type>
    </properties>
  </profile>
</profiles>

And in servlet-context.xml:

<bean id="dataSource" 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>${db.username}</value>
    </property>

    <property name="password">
        <value>${db.password}</value>
    </property>

    <property name="maxActive">
        <value>10</value>
    </property>

    <property name="maxIdle">
        <value>1</value>
    </property>
</bean>

My question is basically, how do I get the Maven properties into the servlet-context.xml file? Do I need a .properties file? I know a little about filtering in Maven and PropertyPlaceholderConfigurer in Spring but I don't know how to put them together -- or do they go together? Or is there a simpler way?

解决方案

Using what I learned from the two answers and my research, I was able to get a development/production system, controlled by the pom, that sets the correct database values.

First, in the pom, I created two profiles.

<!-- Production and Development Profiles -->

<profiles>
    <!-- Nike profile needs go here -->
    <profile>
        <id>production</id>
        <activation>
            <property>
                <name>environment.type</name>
                <value>prod</value>
            </property>
        </activation>
    </profile>

    <!-- Catalyst profile needs go here -->
    <profile>
        <id>development</id>
        <activation>
            <property>
                <name>environment.type</name>
                <value>dev</value>
            </property>
        </activation>

        <build>

            <!-- This holds the properties for the Spring context file.
                 Database values will be changes to development. -->
            <filters>
                <filter>src/main/resources/dev.database.properties</filter>
            </filters>

            <resources>

                <!-- This is the directory that holds the Spring context
                     file.  The entire folder is searched, so either no
                     other file should have place holders or you should
                     exclude other files from being filtered. -->
                <resource>
                    <directory>src/main/webapp/WEBINF/</directory>
                    <filtering>true</filtering>
                </resource>
            </resources>
    </profile>
</profiles>

In the servlet-context.xml, in the WEBINF directory, I put place holders:

<!-- For database, uses maven filtering to fill in placeholders -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${db.driver}" />
    <property name="url"             value="${db.url}" />
    <property name="username"        value="${db.username}" />
    <property name="password"        value="${db.password}" />
    <property name="maxActive">
        <value>10</value>
    </property>
    <property name="maxIdle">
        <value>1</value>
    </property>
</bean>

Then I created a properties file to sit in src/main/resources

#
# Development database properties file
#
db.driver=oracle.jdbc.driver.OracleDriver
db.url=jdbc:oracle:thin:[USER/PASSWORD]@[HOST][:PORT]:SID
db.username=jsmith
db.password=s3cr3t

I can then start Maven with

mvn -P developement clean install

or have a settings.xml that sets the correct profile for me:

<settings>
  <profiles>
    <profile>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>

      <properties>
        <environment.type>dev</environment.type>
      </properties>
    </profile>
  </profiles>   
</settings>

这篇关于使用Maven和Spring控制项目:如何使用Maven配置文件设置Spring配置文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆