2009-07-27 42 views
2

這個問題是一個有點「最佳實踐」的問題,我的團隊是新的Maven和我正在尋找最好的方式前進在這個題目:將webapp的資源部署到maven倉庫?

在具有包裝= war一個項目時,將其部署到Maven存儲庫的最佳方式是什麼(如公司範圍內的Nexus安裝)?

例如,假設您有一個Web應用程序,其中包含最終打包的特定於環境的屬性文件(例如數據庫連接字符串,日誌記錄配置等,基於dev/QA /生產環境)基於屬性或環境變量的應用程序。在構建這種類型的應用程序時,最適合部署到存儲庫的是什麼?它應該是帶有生產設置的WAR文件,還是所有類的JAR,而不是任何資源?

我問的原因是,當你正在構建一個像這樣的webapp時,你確實有一個代碼庫和一組環境特定的資源文件。前者對於部署到您的存儲庫非常有意義,因此可用於其他項目,但後者分享並提供給其他項目沒有多大意義。

回答

3

我建議你建立一個原始的戰爭項目,可能有一些默認的開發文件並將其部署到存儲庫。

然後,您可以建立多個項目,這些項目依賴於戰爭項目並提供自己的特定資源。


2009演示Getting Serious About Build Automation: Using Maven in the Real World對如何可以做到這一點的好例子的JavaOne

  1. 創建「MYAPP部署」 Maven項目
  2. 在部署項目中,引用戰項目

    <dependency> 
        <groupId>myorg.myapp</groupId> 
        <groupId>myapp-web</groupId> 
        <version>${webapp.version}</version> 
        <type>war</type> 
    </dependency> 
    
  3. 在部署項目中,使用覆蓋從導入文件Web項目

    <plugin> 
        <artifactId>maven-war-plugin</artifactId> 
        <configuration> 
         <warName>myapp-web</warName> 
         <overlays> 
          <overlay> 
           <groupId>myorg.myapp</groupId> 
           <artifactId>myapp-web</artifactId> 
           <excludes> 
            <exclude>WEB-INF/classes/config.properties</exclude> 
           </excludes> 
          </overlay> 
          </overlays> 
          <!-- some config elements not shown here --> 
    </plugin> 
    
  4. 在部署項目,包括從部署項目的額外資源到web項目

    <plugin> 
        <artifactId>maven-war-plugin</artifactId> 
        <configuration> 
         <!-- some config elements not shown here --> 
         <webResources> 
          <resource> 
           <directory>${patch.path}</directory> 
          </resource> 
         </webResources> 
        </configuration> 
    </plugin> 
    
+0

我還沒有機會使用這種方法還,但它聽起來很合理,發言,它依賴於申請人名單芯)。我也非常喜歡它在鏈接到的演示文稿中的概述(加上它還有很多其他好的信息),所以非常感謝! – 2009-07-31 17:52:07

0

我的僅具有彼此的一個版本的超級粉絲部署。如果你有一個生產版本,一個測試版本等,這將會減慢你的檢查和重建。 建立戰爭一次,併爲每個環境使用相同的戰爭。

這會導致問題,您如何處理環境特定的屬性。我通常採取混合方式。在可能的情況下,所有環境的所有必需屬性都打包在戰爭中。然後,戰爭根據它所處的環境加載正確的屬性。應用程序可以確定使用系統屬性時的環境。

看到這裏使用彈簧的例子:http://www.developer.com/java/ent/article.php/3811931

這可以很容易地雖然適用於沒有春天。

一些性質不適合用於在戰爭中例如包裝起來生產密碼。對於這些鏈接的方法可以很容易地用來從外部文件路徑加載屬性。

1

我認爲你應該建立一場戰爭。對於開發人員來說,「爲這種環境下降」是一種誘惑。

對環境變量使用JNDI resources,對外部JNDI資源文件使用attach(如果需要,您可以爲每種類型的環境附加一個)。

當然,你則需要一個過程,以獲取正確的資源文件,並將其應用到您的環境(密碼和其它敏感的變量調整它),但是這是一個相當簡單的腳本練習。

1

我把屬性在我的Maven的pom.xml。首先,我砍掉了整個網絡應用程序,以便爲不同的圖層提供單獨的罐子; persistenc/db,service,web(例如,用於spring mvc的控制器)和war。戰爭項目在src/main/resources中包含jsps和properties/config/xml文件。

例如,我的父POM開始是這樣的:

<project 
xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation=" 
    http://maven.apache.org/POM/4.0.0 
    http://maven.apache.org/maven-v4_0_0.xsd"> 
<modelVersion>4.0.0</modelVersion> 

<groupId>edu.berkeley.ist.waitlist</groupId> 

<artifactId>waitlist-parent</artifactId> 

<modules> 
    <module>../waitlist-core</module> 
    <module>../waitlist-db</module> 
    <module>../waitlist-web</module> 
    <module>../waitlist-war</module> 
    <module>../waitlistd</module> 
</modules> 

<build> 
    <resources> 
     <resource> 
      <directory>src/main/resources</directory> 
      <filtering>true</filtering> 
     </resource> 

     <resource> 
      <directory>src/main/hbm</directory> 
      <filtering>true</filtering> 
     </resource> 
    </resources> 

<dependencyManagement> 
    <!-- dependencies with exclusions --> 
    <dependencies> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-aop</artifactId> 

      <version>${version.springframework}</version> 

      <exclusions> 
       <exclusion> 
        <groupId>commons-logging</groupId> 
        <artifactId>commons-logging</artifactId> 
       </exclusion> 
      </exclusions> 
     </dependency> 

然後我有一大堆的配置文件,爲各種構建:

<profiles> 
    <!-- ========================================================== --> 
    <!-- local, hsql            --> 
    <!-- ========================================================== --> 
    <profile> 
     <id>localhost-hsql</id> 

     <activation> 
      <activeByDefault>false</activeByDefault> 
     </activation> 

     <properties> 
      <!-- need to specify ${basedir} so that hsql creates its files in the right place --> 
      <db2.url>jdbc:hsqldb:file:${basedir}/target/waitlistv2;shutdown=true</db2.url> 

      <db2.user>sa</db2.user> 
      <db2.password /> 

      <jdbc-db2.driverClass>org.hsqldb.jdbcDriver</jdbc-db2.driverClass> 

      <db2.schema>public</db2.schema> 

      <db2.hibernate.default_schema>public</db2.hibernate.default_schema> 

      <db2.hibernate.dialect>org.hibernate.dialect.HSQLDialect</db2.hibernate.dialect> 

      <dbunit.dataTypeFactoryName>org.dbunit.ext.hsqldb.HsqldbDataTypeFactory</dbunit.dataTypeFactoryName> 

      <hbmtool.haltOnError>false</hbmtool.haltOnError> 

      <log.filename>/tmp/waitlist.log</log.filename> 
     </properties> 
    </profile> 

例如,在等候名單戰/ src目錄/主/資源是文件logback.xml,並將其與

<configuration debug="true"> 
<appender class="ch.qos.logback.core.rolling.RollingFileAppender" name="RootFileAppender"> 
    <!-- set in the pom.xml file --> 
    <file>${log.filename}</file> 

開始所以,你可以看到從POM中的log.filename屬性用於因到maven的過濾。 (我指定了父類的所有版本,並且子項目只是指定了它們使用的依賴關係,但沒有版本號,我不確定這是否是正確的方法,最佳實踐等等。

此外,waitlist-war取決於waitlist-web,waitlist-web取決於waitlist-core(我的服務層),而waitlist-core取決於waitlist-db。Waitlistd是一個獨立的應用程序,

+0

我忘了說,我喜歡我的方法,而不是,例如,pablojim的(通過鏈接)是與我我只有1個配置/所有層的XML文件,和Maven生成真正的文件,它是投入戰爭。例如,我只有一個logback.xml,一個applicationContext.xml,web.xml等。 – lumpynose 2009-07-28 23:03:13

相關問題