代码之家  ›  专栏  ›  技术社区  ›  matt b

将webapp的资源部署到maven存储库?

  •  2
  • matt b  · 技术社区  · 16 年前

    这个问题有点像“最佳实践”问题,我的团队是Maven的新手,我正在寻找这个话题的最佳解决方法:

    在处理包含打包的项目时= war ,将其部署到Maven存储库(例如公司范围的Nexus安装)的最佳方式是什么?

    4 回复  |  直到 16 年前
        1
  •  3
  •   Robert Munteanu    16 年前

    我建议您构建一个原始war项目,其中可能包含一些用于开发的默认文件,并将其部署到存储库中。

    然后,您可以构建多个依赖于war项目的项目,并提供它们自己的特定资源。


    JavaOne 2009演示文稿 Getting Serious About Build Automation: Using Maven in the Real World

    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. <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <configuration>
              <!-- some config elements not shown here -->
              <webResources>
                  <resource>
                      <directory>${patch.path}</directory>
                  </resource> 
              </webResources>
           </configuration>
       </plugin>
      
        2
  •  1
  •   Rich Seller    16 年前

    我认为你应该建立一场战争。对于开发人员来说,在覆盖层上乱搞是一种太大的诱惑,他们“只是为了那个环境而放弃这个补丁”。

    使用 JNDI resources attach 外部JNDI资源文件(如果需要,可以为每种类型的环境附加一个)。

    当然,您需要一个过程来检索正确的资源文件并将其应用到您的环境中(使用密码和其他敏感变量对其进行调整),但这是一个相当简单的脚本编写练习。

        3
  •  1
  •   lumpynose    16 年前

    我将属性放在我的maven pom.xml中。首先,我将整个web应用程序切碎,以便为不同的层提供单独的罐子;persistenc/db、服务、web(例如springmvc的控制器)和war。war项目的jsp和properties/config/xml文件位于src/main/resources中。

    <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>
    

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

    因此,您可以看到由于maven的过滤,pom中的log.filename属性是如何使用的。

        4
  •  0
  •   Pablojim    16 年前

    我非常喜欢每个可部署系统只有一个版本。如果您有生产版本、测试版本等,这将通过必要的检查和重建降低您的速度。 构建一次war,并对每个环境使用相同的war。

    有关使用spring的示例,请参见此处: http://www.developer.com/java/ent/article.php/3811931

    这可以很容易地适应使用,但没有弹簧。

    有些属性不适合在战争中打包,例如生产密码。对于这些属性,可以轻松使用上述链接方法从外部文件路径加载属性。

    推荐文章