向Maven构建的Web应用程序指定运行时配置参数 [英] Specifying runtime configuration parameters to a Maven built Web Application

查看:179
本文介绍了向Maven构建的Web应用程序指定运行时配置参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Maven的新手,我想知道是否有一个合理的方法在构建时指定配置信息参数给Web应用程序。

I am a newcomer to Maven, and I was wondering if there was a reasonable way of specifying configuration information parameters to a web application at buildtime.

我的意思这是以下。使用Ant,我通常创建一个文件夹(例如 config-params ),在该文件夹中,我放置一些属性文件或任何其他必要的文件,在我的应用程序运行环境的正确设置。

What I mean by this is the following. With Ant, I usually create a folder (e.g. config-params) and in that folder I place some property files or whatever other necessary files with the right settings for the environments in which my application will run.

例如:

- test.jdbc.properties
- cert.jdbc.properties
- prod.jdbc.properties
- test.log4j.properties
- test.myapplication.properties
- test.web.xml

... ad nauseum

然后,在我的ant构建脚本中,来自 project.properties 文件的配置文件配置变量,它只是表示要使用的文件集( test cert prod )。

Then, in my ant build script I just read a profile configuration variable from a project.properties file which simply indicates the set of files that I'd like to use (test, cert or prod).

这样,我不必担心在为已知环境构建应用程序时检查每个可能的框架配置参数。

This way, I don't have to worry about checking every possible framework configuration parameter when I build the application for a known environment.

这两个问题:


  1. 这是可能实现的Maven POM在一个非hackish的方式?是否有一个Maven文档,书或引用处理这个阶段的应用程序构建?

  1. Is this possible to achieve with a Maven POM in a non hackish way? Is there a piece of Maven documentation, book or reference which deals with this stage of an application build?

有一个更合理的方法来定位我的应用程序构建配置到特定的执行环境(测试,生产等)?我想这个人可能更容易辩论,但如果有一个更简单,更优雅的方式来处理这一点,我是耳朵:P。

Is there a more sensible way to target my application build profile towards a specific execution evironment (test, prod, etc)? I guess this one might be more open to debate but if there's a simpler, more elegant way of dealing with this, I'm all ears :P.

我感谢您提供任何帮助。干杯!

I thank you for any help you can provide. Cheers!

推荐答案

我建议将测试环境的配置放入src / test / resources,用于Maven自动测试。生产配置应位于src / main / resources中,这意味着生产配置将在包装和发布过程中自动使用。
其他配置目标(如cert)意味着您需要为不同的环境再次构建您的应用程序。

The first thing i would suggest to put your configuration for the test env into src/test/resources which can be used for test automatically by Maven. The production configuration should be located into src/main/resources which means that the production configuration will automatically be used during the packaging and releasing. The other configuration target like cert means you need to build your application a second time for a different environment.

您是否通过CI环境构建,从而为不同环境生成工件?是的,基于配置文件的解决方案不是一个好的选择,只是基于需要为每个环境单独构建您的应用程序。最好只构建一次具有代表不同环境的工件。
让我们开始这样的:

Do you build by a CI environment which produces artifacts for the different environments ? Yes than a solution based on profiles is not a good choice simply based on the need to build your application for every environment separately. It would be better to build only once having artifacts which represent the different environments. Lets start with something like this:

.
|-- pom.xml
`-- src
    |-- main
    |   |-- java
    |   |-- resources
    |   |-- environment
    |   |   |-- test
    |   |   |   `-- database.properties
    |   |   |-- qa
    |   |   |   `-- database.properties
    |   |   `-- production
    |   |       `-- database.properties
    |   `-- webapp

对于每个环境,您都有一个包含不同配置部分的文件夹属性文件)。
对于maven-assembly-plugin,您需要一个配置文件(对于每个环境),如下所示:

For every environment you have a folder which contains the different configuration parts (in this case property files). You need a configuration file (for every environment) for the maven-assembly-plugin like the following:

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

  <id>test</id>
  <formats>
    <format>war</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <dependencySet>
      <unpack>true</unpack>
      <useProjectArtifact>true</useProjectArtifact>
    </dependencySet>
  </dependencySets>
  <fileSets>
    <fileSet>
      <outputDirectory>WEB-INF</outputDirectory>
      <directory>${basedir}/src/main/environment/test/</directory>
      <includes>
        <include>**</include>
      </includes>
    </fileSet>
  </fileSets>
</assembly>

当然,maven-assembly-plugin的配置如下:

And of course a configuration for the maven-assembly-plugin like this:

 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
      <execution>
        <id>test</id>
        <phase>package</phase>
        <goals>
          <goal>single</goal>
        </goals>
        <configuration>
          <descriptors>
            <descriptor>${project.basedir}/src/main/assembly/test.xml</descriptor>
          </descriptors>
        </configuration>
      </execution>
      <execution>
        <id>qa</id>
        <phase>package</phase>
        <goals>
          <goal>single</goal>
        </goals>
        <configuration>
          <descriptors>
            <descriptor>${project.basedir}/src/main/assembly/qa.xml</descriptor>
          </descriptors>
        </configuration>
      </execution>
      <execution>
        <id>production</id>
        <phase>package</phase>
        <goals>
          <goal>single</goal>
        </goals>
        <configuration>
          <descriptors>
            <descriptor>${project.basedir}/src/main/assembly/production.xml</descriptor>
          </descriptors>
        </configuration>
      </execution>
    </executions>
  </plugin>

结果是只有一个构建,它为每个环境生成单个工件, p>

The result is having just a single build which produces a single artifact for each environment like this:


  1. artifactId-version-test.war

  1. artifactId-version-test.war

artifactId- version-qa.war

artifactId-version-qa.war

artifactId-version-production.war

artifactId-version-production.war

这篇关于向Maven构建的Web应用程序指定运行时配置参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆