2013-10-24 93 views
1

我試圖將我的資源複製到類路徑中,具體取決於使用maven選擇哪個配置文件。我的資源文件夾結構如下:具有共享資源的Maven多模塊多配置文件項目

src/main/resources: 
config 
production 
development 
staging 

我的當前未工作的配置是

<profile> 
     <id>development</id> 
     <activation> 
      <activeByDefault>true</activeByDefault> 
      <property> 
       <name>envtype</name> 
       <value>dev</value> 
      </property> 
     </activation> 

     <build> 
      <finalName>Corelay</finalName> 
      <plugins> 
       <plugin> 
        <groupId>org.apache.maven.plugins</groupId> 
        <artifactId>maven-compiler-plugin</artifactId> 
        <version>3.0</version> 
        <configuration> 
         <source>${java.version}</source> 
         <target>${java.version}</target> 
        </configuration> 
       </plugin> 
      </plugins> 
      <resources> 
       <resource> 
        <directory>src/main/resources</directory> 
        <includes> 
         <include>**/*.xml</include> 
         <include>**/*.properties</include> 
        </includes> 
        <excludes> 
         <exclude>**/production/**</exclude> 
         <exclude>**/staging/**</exclude> 
        </excludes> 
       </resource> 
      </resources> 
      <testResources> 
       <testResource> 
        <directory>src/test/resources</directory> 
        <includes> 
         <include>**/*.xml</include> 
         <include>**/*.properties</include> 
        </includes> 
        <excludes> 
         <exclude>**/production/**</exclude> 
         <exclude>**/staging/**</exclude> 
        </excludes> 
       </testResource> 
      </testResources> 
     </build> 
</profile> 

在所配置/休眠休眠配置文件/休眠-config.xml中請求從同一封裝的一些性質

<bean 
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="locations"> 
     <list> 
      <value>classpath*:**/jdbc.properties</value> 
      <value>classpath*:**/hibernate.properties</value> 
     </list> 
    </property> 
</bean> 

,但有一個錯誤:

Could not resolve placeholder 'jdbc.driverClassName' in string value "${jdbc.driverClassName}" 

該屬性在該文件中定義。怎麼了?另一個問題是如何使從這些配置文件文件夾複製的資源顯示在完全相同的輸出類路徑結構中?我的意思是應該沒有/生產,/開發或/分期:只是/ env

我知道我可以把他們分開,但如果有共享的(如配置在呈現結構),我怎麼能也包括它?

回答

1

在與src/main/resources相同的級別創建一個文件夾src/main/config。裏面創建3個子文件夾通用,開發和製作:

| _ _common

| | _ _common.properties

| _ _dev

| | _ _dev.properties

| _ _prod

| | _ _prod.properties

然後配置兩個配置文件,開發和生產:

<profiles> 
    <profile> 
     <id>dev</id> 
      <activation> 
       <activeByDefault>true</activeByDefault> 
      </activation>    
      <build> 
       <resources> 
        <resource> 
         <directory>src/main/config/common</directory> 
        </resource> 
        <resource> 
         <directory>src/main/config/dev</directory> 
        </resource> 
       </resources> 
      </build> 
    </profile> 
    <profile> 
     <id>prod</id> 
      <build> 
       <resources> 
        <resource> 
         <directory>src/main/config/common</directory> 
        </resource> 
        <resource> 
         <directory>src/main/config/prod</directory> 
        </resource> 
       </resources> 
      </build> 
    </profile>   
</profiles> 

有了這個,MVN全新安裝的副本common.properties和dev.properties到classpath的根目錄,作爲開發配置文件默認爲活動狀態。

mvn clean install -Pprod將安裝common.properties和production.properties,但不安裝dev.properties,也安裝到classpath的根目錄下。