如何强制Maven EAR插件在application.xml中使用上下文根变量?



我使用Maven EAR插件来生成EAR的application.xml,其中包含一个WAR。

我希望在运行时确定该WAR的contextRoot(这得益于JBoss AS 7),因此application.xml应包含如下内容:

<module>
  <web>
    <web-uri>my.war</web-uri>
    <context-root>${my.context.root}</context-root>
  </web>
</module>

这是通过在JBoss AS 7中设置系统属性my.context.root并配置JBoss来替换XML描述符文件中的变量来实现的:

<system-properties>
    <property name="my.context.root" value="/foo"/>
</system-properties>
<subsystem xmlns="urn:jboss:domain:ee:1.1">
  <spec-descriptor-property-replacement>true</spec-descriptor-property-replacement>
  <jboss-descriptor-property-replacement>true</jboss-descriptor-property-replacement>
</subsystem>

如果我在EAR中编辑生成的application.xml,它就可以工作了。

但是,我无法让Maven将${my.context.root}写入application.xml中的上下文根目录。

我先试了这个(因为没有过滤,它应该工作):

<configuration>
  <modules>
    <webModule>
      <groupId>my.group</groupId>
      <artifactId>my-war</artifactId>
      <contextRoot>${my.context.root}</contextRoot>
    </webModule>
  </modules>
</configuration>

显然,即使filtering默认为false, Maven仍然认为它应该使用它作为Maven属性。结果是EAR插件只输入WAR的名称:

<module>
  <web>
    <web-uri>my-war.war</web-uri>
    <context-root>/my-war</context-root>
  </web>
</module>

所以我尝试转义:

<configuration>
  <modules>
    <webModule>
      <groupId>my.group</groupId>
      <artifactId>my-war</artifactId>
      <contextRoot>${my.context.root}</contextRoot>
    </webModule>
  </modules>
</configuration>

然后按字面理解:

<module>
  <web>
    <web-uri>my-war.war</web-uri>
    <context-root>${my.context.root}</context-root>
  </web>
</module>

如何让Maven做我想做的?(当然,我可以尝试通过使用Maven替换器插件破解application.xml,但这是丑陋的…)

谢谢你的提示!

好吧,既然没有人知道更好的答案,下面是我如何破解application.xml的:

<plugin>
  <groupId>com.google.code.maven-replacer-plugin</groupId>
  <artifactId>replacer</artifactId>
  <executions>
    <execution>
      <id>replace-escaped-context-root</id>
      <phase>process-resources</phase>
      <goals>
        <goal>replace</goal>
      </goals>
      <configuration>
        <file>${project.build.directory}/${project.build.finalName}/META-INF/application.xml</file>
        <regex>false</regex>
        <token>${</token>
        <value>${</value>
      </configuration>
    </execution>
  </executions>
</plugin>
<plugin>
  <artifactId>maven-ear-plugin</artifactId>
  <configuration>
    <modules>
      <webModule>
        <groupId>my.group</groupId>
        <artifactId>my-war</artifactId>
        <contextRoot>${my.context.root}</contextRoot>
      </webModule>
    </modules>
  </configuration>
</plugin>

最新更新