2014-01-31 40 views
0

好吧,我有一個由WAR和3個JAR組成的多模塊Web項目(Spring,Hibernate和所有這些)。其中2個JAR是核心WAR和JAR的可選擴展。 maven配置文件使用這些擴展,將它們作爲依賴關係添加或不添加。這一切工作正常。如何將我的資源放入多模塊Web項目中已部署的WAR中

但是,當在Tomcat服務器上安裝項目時,我注意到我無法編輯擴展模塊的屬性文件,因爲它們打包在JAR文件中。所以我正在尋找一種方法,當我使用maven打包項目時,將我的屬性文件從我的擴展JAR移動到可部署的WAR資源文件夾。這意味着當分機被激活時,資源將只存在於WEB資源文件夾中。

我該如何做到最好?

EDIT-01

行,所以我一直在使用構建輔助性Maven的插件,它qza建議嘗試。

測試戰POM:

<project> 
...  
<build> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-war-plugin</artifactId> 
      <version>2.1.1</version> 
      <configuration> 
       <failOnMissingWebXml>false</failOnMissingWebXml> 
      </configuration> 
     </plugin> 
     <plugin> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>build-helper-maven-plugin</artifactId> 
      <version>1.8</version> 
      <executions> 
       <execution> 
        <id>add-resource</id> 
        <phase>process-resources</phase> 
        <goals> 
         <goal>add-resource</goal> 
        </goals> 
        <configuration> 
         <resources> 
          <resource> 
           <directory>../extTestA-jar/src/main/resources</directory> 
           <targetPath>resources</targetPath> 
          </resource> 
         </resources> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 
</build> 
</project> 

我的項目佈局如下:

<Parent> 
--><extTestA-jar> 
--><extTestB-jar> 
--><test-jar> 
--><test-war> 

用我的資源 「的src/main /資源」

任何人都可以發現問題在這?我嘗試過和沒有目標路徑,但似乎沒有工作。我也試圖在生成資源階段這樣做,但仍然沒有運氣。我在想這條道路可能是錯的,所以我嘗試了其他一些aswel,但仍然沒有運氣。

+0

如果您打算在Tomcat服務器中編輯屬性文件_in place_,那麼完全將它們從webapp外部化將成爲此處的途徑(如Mustafa Genc所述)。如果它們位於webapp中,那麼當新版本部署到Tomcat時,它們可能會被覆蓋。 –

+0

是的,我看過它並得出同樣的結論。然而,由於覆蓋並不是一個真正的問題,我的老闆希望它在WAR資源文件夾中。所以我接受了qza的建議。 這就是說,穆斯塔法的答案是更好的,如果兩種選擇都向你開放。然而,我受到我老闆的意願的限制。那麼任何人都可以在我的代碼中發現問題嗎? –

+0

這裏是一個小的演示,展示如何複製資源https://github.com/qza/maven-examples – qza

回答

1

一種解決方案是使用Maven Build Helper Plugin和執行目標:

附加資源

完整的例子是:

<plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>build-helper-maven-plugin</artifactId> 
    <version>1.3</version> 
    <executions> 
     <execution> 
      <id>add-resources</id> 
      <phase>process-resources</phase> 
      <goals> 
       <goal>add-resource</goal> 
      </goals> 
      <configuration> 
       <resources> 
        <resource> 
         <directory>../data-module/src/main/resources</directory> 
        </resource> 
       </resources> 
      </configuration> 
     </execution> 
    </executions> 
</plugin> 

另一個方法是使用Maven Resources Plugin和目標

複製資源

對於這個插件,使用去呈三角此:

<plugin> 
<artifactId>maven-resources-plugin</artifactId> 
<executions> 
    <execution> 
     <id>copy-resources</id> 
     <phase>generate-resources</phase> 
     <goals> 
      <goal>copy-resources</goal> 
     </goals> 
     <configuration> 
      <outputDirectory>src/main/resources</outputDirectory> 
      <overwrite>true</overwrite> 
      <resources> 
       <resource> 
        <directory>../data-module/src/main/resources</directory> 
        <includes> 
         <include>*.properties</include> 
        </includes> 
       </resource> 
      </resources> 
     </configuration> 
    </execution> 
</executions> 

兩個插件可以像這樣一個任務的幫助。

+0

添加資源看起來很有希望。將嘗試它。謝謝。我完成後會發布結果。 –

+0

好吧,所以我試過了添加資源,但我似乎無法得到它的工作。我沒有收到任何錯誤,但似乎沒有做任何事情。我已經更新了我的原始帖子,包括我的更改和我嘗試過的內容。你有沒有看到問題? –

+0

生成日誌顯示添加資源目標的執行情況。可能會出現「跳過非現有資源目錄」或其他錯誤消息等信息 – qza

1

最好從項目外讀取屬性。爲您的應用程序服務器創建一個配置文件夾,並在服務器啓動腳本,如定義它:

-Dconfiguration.dir=/etc/tomcatConf 

然後把你的配置文件/文件夾,該文件夾下,並掃描那些與彈簧性能佔位豆:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="locations"> 
     <list> 
      <value>file:${configuration.dir}/module1/auth.properties</value> 
     </list> 
    </property> 
</bean> 

現在,當您更改屬性時,您只需重新啓動服務器,不需要重新開始戰爭/戰場。

請注意,您不需要有系統參數。它用於爲應用程序服務器保留一個標準配置目錄。你可以使用:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="locations"> 
     <list> 
      <value>file:/etc/conf/auth.properties</value> 
     </list> 
    </property> 
</bean> 
相關問題