zoukankan      html  css  js  c++  java
  • 使用maven属性变量和配置文件

    mvn archetype:generate

     1 <profiles>
     2         <profile>
     3             <!-- 本地环境 -->
     4             <id>local</id>
     5             <properties>                
     6                 <db-url>jdbc:oracle:thin:@localhost:1521:XE</db-url>
     7                 <db-username>***</db-username>
     8                 <db-password>***</db-password>
     9             </properties>
    10         </profile>
    11         <profile>
    12             <!-- 开发环境 -->
    13             <id>dev</id>
    14             <properties>                
    15                 <db-url>jdbc:oracle:thin:@172.21.129.51:1521:orcl</db-url>
    16                 <db-username>***</db-username>
    17                 <db-password>***</db-password>
    18             </properties>
    19             <!-- 默认激活本环境 -->
    20             <activation>
    21                 <activeByDefault>true</activeByDefault>
    22             </activation>
    23         </profile>
    24         ...
    25     </profiles>

    profiles节点中,定义了二种环境:local、dev(默认激活dev环境),可以在各自的环境中添加需要的property值,接下来修改build节点,参考下面的示例:

     1 <build>
     2         <resources>
     3             <resource>
     4                 <directory>src/main/resources</directory>
     5                 <filtering>true</filtering>
     6             </resource>
     7         </resources>
     8         <plugins>
     9             <plugin>
    10                 <groupId>org.apache.maven.plugins</groupId>
    11                 <artifactId>maven-compiler-plugin</artifactId>
    12                 <version>2.5.1</version>
    13                 <configuration>
    14                     <source>1.6</source>
    15                     <target>1.6</target>
    16                     <encoding>utf-8</encoding>
    17                 </configuration>
    18             </plugin>
    19         </plugins>
    20     </build>

    resource节点是关键,它表明了哪个目录下的配置文件(不管是xml配置文件,还是properties属性文件),需要根据profile环境来替换属性值。

    通常配置文件放在resources目录下,build时该目录下的文件都自动会copy到class目录下

    例:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="dataSource"
            class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
            <property name="url" value="${db-url}" />
            <property name="username" value="${db-username}" />
            <property name="password" value="${db-password}" />        
        </bean>
    </beans>

    各属性节点的值,用占位符"${属性名}"占位,maven在package时,会根据profile的环境自动替换这些占位符为实际属性值。

    默认情况下: 

    maven package

    将采用默认激活的profile环境来打包,也可以手动指定环境,比如:

    maven package -P dev

    将自动打包成dev环境的部署包(注:参数P为大写)

  • 相关阅读:
    android -- eclipse 环境搭建
    android AVD坑(1) -- no images system installed for this target
    android AVD坑(1) -- no images system installed for this target
    简单的轮播
    C# 使用NPOI 处理Excel(Datable与Excel相互转换)
    C盘突然爆满
    IIS中 flv、swf 文件无法播放
    webservice 尝试加载 Oracle 客户端库时引发 BadImageFormatException。如果在安装 32 位 Oracle 客户端组件的情况下运行,将出现此问题
    window server 2008 安装Oracle10g
    ora-01033 oracle initialization or
  • 原文地址:https://www.cnblogs.com/javaleon/p/3969226.html
Copyright © 2011-2022 走看看