我在项目中将数据连接移动到beta和生产数据库以进行测试。显然,将alpha数据库凭据存储在源存储库中是可以的,但是将beta和生产数据库凭据存储在源存储库中就会遭到枪决。
我知道maven可以有{userdir}/build。属性文件。我想使用这个文件将db凭据保存在源存储库之外。但是我似乎不能让maven弄清楚,对于文件x.cfg.xml,它必须替换值。
在hibernate。cfg。xml文件中有这一行
<property name="hibernate.connection.url">@ssoBetaUrl@</property>
现在我如何让maven用{userdir}/build中的值替换该变量?属性文件吗?
编辑 -------------我一直在玩属性-maven-plugin插件,但我似乎无法得到它的火。我把它放在父元素
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<id>read-properties</id>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
</execution>
</executions>
</plugin>
但是当它构建时,它不会触发。如果我阅读http://maven.apache.org/maven-1.x/reference/properties.html正确,它应该在~/build中找到构建属性文件。属性文件夹,从那里开始,但我不确定
我认为你处理这件事的方式是错误的。与其让构建过程将适当的连接细节放入JAR文件中,不如让程序在启动时查找配置文件。
通常,我的基于hibernate的应用程序将在%user.home&/.appname/config.properties
下查找一个文件,并从那里加载DB凭据和其他部署特定的数据。如果文件丢失,可以将默认版本包含在JAR中并复制到此位置(在初始启动时,这样您就不必将文件复制粘贴到新系统中),然后使用适当的设置对其进行编辑。
这样,您可以使用相同的构建为测试和生产服务器生成JAR(或WAR)文件,差异将在配置文件中(可能已经部署)。这也使得拥有多个生产部署成为可能,每个生产部署都与不同的数据库通信,而在构建过程中没有任何复杂性。
你可以使用两个插件
- properties-maven-plugin
-
代用品
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>properties-maven-plugin</artifactId> <version>1.0-alpha-1</version> <executions> <execution> <phase>initialize</phase> <goals> <goal>read-project-properties</goal> </goals> <configuration> <files> <file>{userdir}/build.properties</file> </files> </configuration> </execution> </executions> </plugin> <plugin> <groupId>com.google.code.maven-replacer-plugin</groupId> <artifactId>replacer</artifactId> <version>1.5.2</version> <executions> <execution> <phase>prepare-package</phase> <goals> <goal>replace</goal> </goals> </execution> </executions> <configuration> <includes> <include>target/**/*.*</include> </includes> <replacements> <replacement> <token>@ssoBetaUrl@</token> <value>http://[anyURL]</value> </replacement> </replacements> </configuration> </plugin>