基于Maven配置文件的context.xml中的JNDI配置

3

给定

我刚开始使用Maven做“花哨的东西”,遇到了一个难题。我需要部署到两个不同的服务器,每个服务器都有稍微不同的JDNI资源配置文件在context.xml中定义。

我的文件结构如下: (虽然如果有更好的方法我可以改变这个结构)

src/main/webapp/META-INF/context.xml
src/main/webapp/META-INF/context.devel.xml
src/main/webapp/META-INF/context.prod.xml

根据部署目标,我想使用相应的context.TARGET.xml文件。
问题
我了解到需要设置两个不同的构建配置文件,例如:
<profiles>
  <profile>
      <id>prod</id>
  </profile>
  <profile>
    <id>devel</id>
  </profile>
</profiles> 

但是从这里开始,我对最佳解决方案感到困惑。我知道使用war插件可以排除context.xml,但是从那时起,我不知道该怎么做。

是否有一种方法可以在我的context.xml文件中添加一个变量,让Maven“写入”,而不是拥有两个不同的配置文件。

有什么建议吗?

2个回答

4

以下是一些提示。

  • 你只需要一个 context.xml 文件。
  • 用自定义的maven属性替换 context.xml 中的服务器特定条目。例如:${myServer} 或 ${dbUser}。
  • 在你的配置文件中像这样定义这些属性:
<profiles>
  <profile>
      <id>prod</id>
      <properties>
          <myServer>srv-prod.yourcompany.com</myServer>
          <dbUser>james</dbUser>
      </properties>
  </profile>
  <profile>
    <id>devel</id>
      <properties>
          <myServer>srv-devel.yourcompany.com</myServer>
          <dbUser>richard</dbUser>
      </properties>
  </profile>
</profiles>
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.5</version>
        <configuration>
            <filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>
            <webResources>
                <resource>
                    <directory>src/main/webapp/META-INF</directory>
                    <targetPath>/META-INF</targetPath>
                    <filtering>true</filtering>
                </resource>
           </webResources>
        </configuration>
    </plugin>
 </plugins>
  • 在maven构建中激活适当的配置文件。例如,在命令行上调用mvn -Pprod clean package。或在IDE中激活所需的配置文件。对于devl,请使用-Pdevl

这正是我正在寻找的。 - Jeef

0
你可以使用Maven资源过滤器来明确地包含或排除特定文件从Maven构建生命周期的process-resources阶段。
<profiles>
    <profile>
        <id>prod</id>
        <resources>
            <resource>
                <directory>src/main/resources/META-INF</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/context.prod.xml</include>
                </includes>
            </resource>
        </resources>
    </profile>
    <profile>
        <id>devel</id>
        <resources>
            <resource>
                <directory>src/main/resources/META-INF</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/context.devl.xml</include>
                </includes>
            </resource>
        </resources>
    </profile>
</profiles> 

文档可以在这里找到。


context.prod.xmlcontext.devl.xml是合适的名称吗?关于惯例、最佳实践...你需要额外编写代码来使用proddevl吗? - JimHawkins
@Ulrich 抱歉,我不知道这方面的最佳实践。如果您想在打包之前更改名称,您可以利用Maven Assembly插件。 - ConMan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接