我调用了一个 Web 服务,不幸的是它的命名空间根据服务器域名进行更改。这使得源代码管理合并变得困难,并导致许多构建错误。 maven 可以用来替换 java 文件中此类变体变量的占位符吗?这将帮助我设置托管 Web 服务的服务器的域名,而不是修改包含命名空间的所有文件。
最佳答案
是的,您可以使用 Maven 来完成。
(1) 在本地maven存储库中创建setting.xml文件。如下所述。 http://maven.apache.org/settings.html
在其中,您可以为每个服务器创建具有不同属性的不同配置文件,如下所示
<profile>
<id>local</id>
<properties>
<hibernate.connection.url>jdbc:mysql://localhost:3306/my_db</hibernate.connection.url>
<hibernate.connection.username>db_user_name</hibernate.connection.username>
<hibernate.connection.password>db_password</hibernate.connection.password>
</properties>
</profile>
<profile>
<id>dev</id>
<properties>
<hibernate.connection.url>jdbc:mysql://dev_url:3306/my_db</hibernate.connection.url>
<hibernate.connection.username>dev_db_user_name</hibernate.connection.username>
<hibernate.connection.password>dev_db_password</hibernate.connection.password>
</properties>
</profile>
<profile>
<id>production</id>
<properties>
<hibernate.connection.url>jdbc:mysql://prod_url:3306/my_db</hibernate.connection.url>
<hibernate.connection.username>prod_db_user_name</hibernate.connection.username>
<hibernate.connection.password>prod_db_password</hibernate.connection.password>
</properties>
</profile>
注意:您可以根据您的要求创建任意数量的自定义属性。
(2) 在项目工作区的属性文件内。使用占位符而不是硬编码值。就像 db_connection_url = ${hibernate.connection.url}
假设属性文件存在于位置:src/main/resources/。
(3) Pom.xml 文件将“过滤”设置为 true,如下所示
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
(4) 项目构建:使用-P选项,指定创建项目构建文件时需要使用的配置文件。例如
- 构建本地服务器时使用命令:mvn -P local clean package
- 构建本地服务器时使用命令:mvn -P dev clean package
- 为本地服务器构建时使用命令:mvn -P production clean package
Maven 将使用适当的配置文件并用定义属性替换所有占位符。
注意:您也可以在 pom.xml 中定义标签,而不是创建setting.xml 文件
关于java - 使用maven修改Java文件占位符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27728397/