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

时间:2014-06-27 16:12:47

标签: maven jboss maven-ear-plugin

我正在使用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,但这很难看......)

感谢任何提示!

1 个答案:

答案 0 :(得分:1)

好吧,因为没有人知道更好的答案,这就是我如何将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>
相关问题