Home > database >  Maven filtering - replacement of variables
Maven filtering - replacement of variables

Time:09-17

I have a maven project, which generates a jar file as a web project. Based on Maven I include a standalone Tomcat. Inside of the jar file, there is actually the war-file, which contains my application.

This application contains a "version.txt" in src/main/config (or any similar path), that is finally included in the war-file.

This version.txt looks like:

version: ${project.version}

I would like, that maven should replace the variable with the correct version from pom.xml. In my pom.xml I have included:

  <build>
    <resources>
      <resource>
        <directory>src/main/config</directory>
          <filtering>true</filtering>
            <includes>
              <include>**/version.txt</include>
            </includes>
          </resource>
        <resource>
      <resources>
  </build>

So is there any way to include this version.txt and a working replacement in a war-file, which is in a (Tomcat)jar-file?

Addendum:

My File hierarchy looks like:

jar-file
-- ...
--war-file
---- ...
----version.txt

CodePudding user response:

I suggest you use the maven-war-plugin.

https://maven.apache.org/plugins/maven-war-plugin/usage.html

It will expect a certain directory layout, and in the examples it clearly shows how to filter (replace maven variables into the web resources)

https://maven.apache.org/plugins/maven-war-plugin/examples/adding-filtering-webresources.html

If this solution falls short, then a more specific question based on this should should be asked later.

CodePudding user response:

Thanks for the advice. No, there was no maven-war-plugin, but I have included it and based on the instructions, it works.

Short solution:

Added a resourceDirectory on same level like pom.xml and included version.txt

Added in pom.xml and activate filtering:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.3.1</version>
        <configuration>
            <webResources>
                <resource>
                    <filtering>true</filtering>
                    <directory>externalresources</directory>
                </resource>
            </webResources>
        </configuration>
    </plugin>
  • Related