我是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 文件中读取了一个配置文件配置变量,该文件只显示了我想要使用的文件集( test < / em>, cert 或 prod )。
这样,在为已知环境构建应用程序时,我不必担心检查每个可能的框架配置参数。
所以有两个问题:
这是否可以通过Maven POM以非hackish方式实现?是否有一篇关于应用程序构建阶段的Maven文档,书籍或参考文献?
是否有更合理的方法将我的应用程序构建配置文件定位到特定的执行环境(测试,产品等)?我想这个可能更容易辩论,但如果有一个更简单,更优雅的方式来解决这个问题,我全都听见了:P。
感谢您提供的任何帮助。干杯!
答案 0 :(得分:1)
我建议将测试环境的配置放入src / test / resources中,这可以由Maven自动进行测试。生产配置应位于src / main / resources中,这意味着生产配置将在打包和发布期间自动使用。 像cert这样的其他配置目标意味着您需要为不同的环境再次构建应用程序。
您是否通过为不同环境生成工件的CI环境构建?是的,基于配置文件的解决方案不是一个好的选择,只需根据为每个环境单独构建应用程序的需要。最好只构建一次具有代表不同环境的工件。 让我们从这样的事情开始:
.
|-- 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-assembly-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>
结果是只有一个构建,为每个环境生成一个工件,如下所示:
的artifactId版本-test.war
artifactId-version-qa.war
的artifactId版本-production.war
答案 1 :(得分:0)
基本概念是在单个POM
文件中创建多个构建配置,然后使用
mvn -P <profile-name> <targets>
您还可以通过定义<activation/>
规则让maven决定使用哪个配置文件。