0

Edited to clarify that I'm asking for a solution for Eclipse WTP with Tomcat.

I have since got this to work for WAR artifacts by just using the variable ${project.version} in web.xml in conjunction with maven-war-plugin filtering as follows.

<webResources>
        <resource>
            <filtering>true</filtering>
            <directory>src/main/webapp</directory>
            <includes>
                <include>**/web.xml</include>
            </includes>
        </resource>
    </webResources>
    <warSourceDirectory>src/main/webapp</warSourceDirectory>
    <webXml>src/main/webapp/WEB-INF/web.xml</webXml>

</edit>


I'd like to use the value of ${project.version} in web.xml in an Eclipse WTP-run Tomcat as follows.

<servlet-mapping>
    <servlet-name>jersey</servlet-name>
    <url-pattern>/api/${version}/*</url-pattern>
</servlet-mapping>

I've tried filtering as follows, but this doesn't work, I still get a 404. I know that the filtering mechanism is usually applied to access Maven variables in the source code rather than web.xml, but I though this might work for my case too...

POM excerpt

<build>
<filters>  
    <filter>${basedir}/src/main/filters/${filter.name}.properties</filter>  
</filters>
<plugins>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.2.0</version>
    <configuration>
      <webResources>
        <resource>
          <directory>${basedir}/src/main/webapp</directory>
          <filtering>true</filtering>
          <includes>  
            <include>WEB-INF/web.xml</include>  
          </includes> 
        </resource>
      </webResources>
    </configuration>
  </plugin>
...
  <properties>
    <filter.name>version</filter.name>
  </properties>      

/src/main/filters/version.properties

version=${project.version}

web.xml excerpt

<servlet-mapping>
    <servlet-name>jersey</servlet-name>
    <url-pattern>/api/v${version}/*</url-pattern>
</servlet-mapping>

I know that this mechanism is meant to be used for getting properties in the Java source (e.g., https://stackoverflow.com/a/15356374/731040, https://developer.jboss.org/wiki/HowToConfigureJavaEEApplicationToApplyDifferentSettingsinWebxmlEtcForVariousEnvironmentsByMaven), but thought this might work out-of-the-box for variable injection into web.xml as well. Alas, it doesn't

s.d
  • 4,017
  • 5
  • 35
  • 65
  • I believe, this link will help you. https://stackoverflow.com/questions/12099008/how-to-include-values-from-properties-file-into-web-xml – Arvind Katte Nov 22 '17 at 15:24
  • I can't help you with a solution, but you weren't wrong in thinking you can filter any text files, not just source code. – Gorazd Rebolj Nov 22 '17 at 15:25
  • What happens if you use `${project.version}` in web.xml? – Aleksandr M Nov 22 '17 at 20:31
  • @AleksandrM: Works fine if I build the *war*, does not work for use in Eclipse WTP. I'll edit the question to reflect that. Thanks! – s.d Nov 23 '17 at 08:51

0 Answers0