2012-01-04 133 views
7

我想使用maven替換我的war文件中的一些* .properties文件。Maven資源不復制文件

因此我在我的資源文件夾中創建了文件夾dev,test和prod。現在我只想在執行相應的配置文件時在我的war文件的資源文件夾路徑中使用這些文件夾中的一個。結果是maven正在複製所有其他文件夾,所以這些文件夾在類路徑中是雙重的。下面是我的配置:

<profile> 
     <id>dev</id> 

     <activation> 
      <activeByDefault>true</activeByDefault> 
     </activation> 

     <build> 
      <plugins> 
       <plugin> 
        <artifactId>maven-resources-plugin</artifactId> 
        <version>2.3</version> 
        <executions> 
         <execution> 
          <id>copy-dev-resources</id> 
          <phase>process-resources</phase> 
          <goals> 
           <goal>copy-resources</goal> 
          </goals> 
          <configuration> 

           <overwrite>true</overwrite> 

           <outputDirectory>${basedir}/target/classes</outputDirectory> 
           <resources> 
            <resource> 
             <!-- source --> 
             <directory>src/main/resources/dev</directory> 
            </resource> 
           </resources> 
          </configuration> 
         </execution> 
        </executions> 
       </plugin> 
      </plugins> 
     </build> 
    </profile> 

如何配置Maven,現在它的唯一拷貝文件夾的src /主/資源/開發的目標/班的內容是什麼?

謝謝你的幫助

回答

7

Maven的默認副本「的src/main /資源」輸出文件夾中的所有文件,所以在當前的配置它可能會創建「開發」,「測試」和「 prod「文件夾及其內容,然後另外複製開發資源而不用」dev「前綴。

我會建議保留默認資源文件夾,並將其僅用於配置文件無關的資源。在您的個人資料就可以配置更多的資源文件夾:

<profile> 
    <id>dev</id> 
    <build> 
     <resources> 
      <resource> 
       <directory>${basedir}/src/main/resources-dev</directory> 
      </resource> 
     </resources> 
    </build> 
</profile> 

它也應該可以使用在配置文件定義的屬性共同構建部分配置此:

<build> 
    <resources> 
     <resource> 
      <directory>${basedir}/src/main/resources-${projectStage}</directory> 
     </resource> 
    </resources> 
</build> 
<profiles> 
    <profile> 
     <id>dev</id> 
     <properties> 
      <projectStage>dev</projectStage> 
     </properties> 
    </profile> 
</profiles> 

如果你因爲某些原因無法更改您當前的目錄結構,那麼您必須配置默認資源文件夾的排除項以不復制嵌套的配置文件相關文件夾:

<build> 
     <resources> 
      <resource> 
       <directory>${basedir}/src/main/resources</directory> 
       <excludes> 
        <exclude>dev/**</exclude> 
        <exclude>test/**</exclude> 
        <exclude>prod/**</exclude> 
       </excludes> 
      </resource> 
     </resources> 
    </build> 
1

爲此目的存在maven目錄結構中的「main」部分。 而不是在資源目錄內創建目錄,你應該在你的src目錄中創建更多的目錄。 這是默認行爲,它已經通過讓測試資源在maven生命週期的測試階段覆蓋主要資源來實現。

你的結構應該是這樣的:

src 
    main 
    resources 
     a.properties 
     b.properties 
     c.properties 
    test 
    resources 
     b.properties 
     c.properties 
    dev 
    resources 
     b.properties 

通過這樣的結構,你的「主」的目標將有默認的屬性文件,測試階段將暫時覆蓋B和C,而只有開發配置文件將在最終的可部署版本中用另一個定製版本覆蓋b。

爲了達到這個目的,你必須配置maven-resources-plugin你已經在我們的問題中做了一個開發者配置文件。

而且當你這樣做時,還要考慮:在使用dev配置文件構建時運行單元測試時應該使用何種版本的屬性文件?

0

我使用Maven中的assembly.xml文件,並使用這組代碼,以確保我的資源文件是在應用程序的根目錄...

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd"> 

<id>distribution</id> 
<formats> 
    <format>tar.gz</format> 
</formats> 
<fileSets> 
    <fileSet> 
     <directory>${basedir}</directory> 
     <includes> 
      <include>bin/**</include> 
      <include>webapps/**</include> 
      <include>data/**</include> 
     </includes> 
    </fileSet> 
    <fileSet> 
     <directory>${basedir}/target</directory> 
     <includes> 
      <include>*.jar</include> 
     </includes> 
     <outputDirectory>lib</outputDirectory> 
    </fileSet> 
    <fileSet> 
     <directory>${basedir}/src/main/</directory> 
     <includes> 
      <include>resources/**</include> 
     </includes> 
     <outputDirectory>/</outputDirectory> 
    </fileSet> 
</fileSets> 

<dependencySets> 
    <dependencySet> 
     <outputDirectory>lib</outputDirectory> 
     <useProjectArtifact>false</useProjectArtifact> 
    </dependencySet> 
</dependencySets> 

    </assembly> 

不知道這是最好的方式,但它是我可以想象的。