8

This is an easy filter approach to write the project version into a file.

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

This is the project structure (left out the unintresting parts)

├───src
│   └───main
│       ├───java
│       │   └─── [...]
│       └───webapp
│           ├───META-INF
│           └───WEB-INF
│               ├───cfg
│               └───portal.version
└─── pom.xml

The content of portal.version

${project.version}

This should be replaced with the artifact version of the pom.xml, but unfortunately nothing happens. Whats wrong? Thank you in advance

Christopher Klewes
  • 11,181
  • 18
  • 74
  • 102
  • I would give you more than one point just for the question. Navigating through the maven documentation is a hell, and usually results in a long and complex xml snippet. This is clear, quick and immediate. – Dacav Nov 19 '13 at 09:03

3 Answers3

4

When you specify resource element, the result filtered content is copied into a target/classes folder. To filter web app resources you could configure maven-war-plugin.

Though to get the version, in most cases it is better to read a standard Maven property file in your application, e.g. META-INF\maven\<groupId>\<artifactId>\pom.properties

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
Eugene Kuleshov
  • 31,461
  • 5
  • 66
  • 67
  • 1
    Thank you, but I don't want to bind maven that closely to my application. – Christopher Klewes Sep 26 '10 at 09:21
  • This property file is not available in the classpath when you run your application in your IDE - reading works only in the deployed runtime context outside your IDE. maven/.../pom.properties is only build into the jar or war artifact. – IPP Nerd Aug 05 '20 at 09:30
2

To filter web resources, you can use the filtering capabilities of the war plugin.

brabster
  • 42,504
  • 27
  • 146
  • 186
0

This is solved in this post and also there's an additional step, you have to include this under maven-resources-plugin:

<delimiters>
    <delimiter>${*}</delimiter>
</delimiters>

At least that worked for me and I had the exact same issue as you.

Joselo
  • 127
  • 1
  • 10