gpt4 book ai didi

java - 从 glassfish-resources.xml 访问 pom 属性

转载 作者:行者123 更新时间:2023-12-01 12:47:56 25 4
gpt4 key购买 nike

如何使用 pom 中的属性设置 glassfish-resources.xml 中 jdbc-connection-pool 属性的值。

例如我的pom.xml

...
<profiles>
<profile>
<id>dev</id>
<properties>
<database.dbname>Xpto</database.dbname>
...
</properties>
</profile>
...
</profiles>
...

还有 glassfish-resources.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
<resources>
<custom-resource res-type="java.util.Properties" jndi-name="jndi/iprofile"
factory-class="org.glassfish.resources.custom.factory.PropertiesFactory">
<property name="name" value="${webapp.profile}" />
</custom-resource>
<jdbc-resource enabled="true" jndi-name="jdbc/users" object-type="user" pool-name="MY-POOL">
<description/>
</jdbc-resource>
<jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="com.microsoft.sqlserver.jdbc.SQLServerXADataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="MY-POOL" non-transactional-connections="false" ping="false" pool-resize-quantity="2" pooling="true" res-type="javax.sql.XADataSource" statement-cache-size="0" statement-leak-reclaim="false" statement-leak-timeout-in-seconds="0" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false">
<property name="serverName" value=""/>
<property name="PortNumber" value=""/>
<property name="DatabaseName" value=""/>
<property name="User" value=""/>
<property name="Password" value=""/>
</jdbc-connection-pool>

我想使用 pom.xml 中定义的值 Xpto 设置 DatabaseName 属性。其他配置文件的database.dbname 属性具有不同的值。

最佳答案

您可以使用 maven-replacer-plugin 来解决此类任务。它可用于替换文件中的值。

这是一个例子:

像以前一样定义您的属性:

<properties>
<database.dbname>Xpto</database.dbname>
</properties>

glassfish-resources.xml 中要替换的属性更改为如下所示:

<property name="DatabaseName" value="DATABASENAME"/>

DATABASENAME用于替换。您可以使用任何值,但它在您要修改的文件中必须是唯一的。

maven-war-plugin 的配置更改为如下所示:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<executions>
<execution>
<id>prepare-war</id>
<phase>prepare-package</phase>
<goals>
<goal>exploded</goal>
</goals>
</execution>
<execution>
<id>default-war</id>
<phase>package</phase>
<goals>
<goal>war</goal>
</goals>
<configuration>
<warSourceDirectory>${project.build.directory}/${project.build.finalName}</warSourceDirectory>
</configuration>
</execution>
</executions>
</plugin>

来自maven-replacer-plugin wiki (have a look at this page for details how the maven-replacer-plugin and the maven-war-plugin work together) :

Currently, the replacer plugin needs access to a target resource before it is packaged into an archive. However, standard WAR packaging does not expose web resources (anything under src/main/webapp) for use in other plugins and runs as a single execution. Fortunately, we can invoke war:exploded to copy these resources earlier in the build lifecycle so that they can be used by the maven-replacer-plugin. The standard package usage will then use the modified web resources when creating the WAR artifact.

maven-replacer-plugin 的以下配置添加到 pom.xml 中:

<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.3</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<file>target/${project.build.finalName}/WEB-INF/glassfish-resources.xml</file>
<token>DATABASENAME</token>
<value>${database.dbname}</value>
</configuration>
</plugin>

如您所见,标记 DATABASENAME 被替换为 ${database.dbname} 的值。这是在 prepare-package 阶段完成的,即在将 Web 应用程序内容复制到分解的 WAR 目录之后,但在将该目录打包为 WAR 文件之前。

如果替换不起作用,您可能需要将 maven-war-plugin 降级到版本 2.0.1

如果您想替换多个不同的值,您可以将配置中的 token 和值替换为如下内容:

<replacements>
<replacement>
<token>DATABASENAME</token>
<value>${database.dbname}</value>
</replacement>
</replacements>

另请参阅:

关于java - 从 glassfish-resources.xml 访问 pom 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24506939/

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