gpt4 book ai didi

java - 为 Maven 构建的 Web 应用程序指定运行时配置参数

转载 作者:太空宇宙 更新时间:2023-11-04 08:11:47 25 4
gpt4 key购买 nike

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

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

例如:

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

... ad nauseum

然后,在我的 ant 构建脚本中,我只是从 project.properties 文件中读取一个配置文件配置变量,该变量仅指示我想要使用的文件集(testcertprod)。

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

有两个问题:

  1. 这是否可以通过 Maven POM 以非 hackish 的方式实现?是否有 Maven 文档、书籍或引用资料涉及应用程序构建的此阶段?

  2. 是否有更明智的方法将我的应用程序构建配置文件针对特定的执行环境(测试、生产等)?我想这个问题可能更容易引起争论,但如果有一种更简单、更优雅的方法来处理这个问题,我洗耳恭听:P。

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

最佳答案

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

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

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

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

<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-assemble-plugin 配置:

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

结果是只有一个构建,它为每个环境生成一个 Artifact ,如下所示:

  1. artifactId-version-test.war

  2. artifactId-version-qa.war

  3. artifactId-版本-生产.war

关于java - 为 Maven 构建的 Web 应用程序指定运行时配置参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11065273/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com