The Maven Resources Plugin allows you to perform Maven variable overrides on files (Resources) of the project.
This plugin is part of the standard Maven execution plan in the phases process-resources
and process-test-resources
, so what you need to do is instruct the plugin to replace the variables during these phases.
Following the example of the documentation, you simply need to add the following configuration:
<project>
...
<name>My Resources Plugin Practice Project</name>
...
<build>
...
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
...
</resources>
...
</build>
...
</project>
Then any file in the specified directory will be processed by Maven. You could then create a txt
or properties
containing the version:
versao=${project.version}
This way, Maven is in charge of placing the correct version when packaging the project.
If there are other files in the project it may be best to prevent them from being unnecessarily or improperly processed. You can specify multiple tags <resource>
containing tags <include>
and <exclude>
to specify which files you want to process and which should remain intact.
Example:
<project>
...
<build>
...
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<excludes>
<exclude>**/*.properties</exclude>
</excludes>
</resource>
...
</resources>
...
</build>
...
</project>
Thanks Luiz, I will do some tests. That
Package
Java also serves for versioning?– Renan Gomes
@Renan The Java versioning specification is interesting and contains important information if you are implementing a library or module that is used with various components and dependencies of different versions. For example, you may have a jar with the API interfaces in the version
1.0
and an API implementation jar in version1.0.10
, therefore the manifesto jar would contain different versions of specification and implementation. However, for simpler cases this information is irrelevant.– utluiz
The Maven Archiver Plugin can generate this information for you. So you could read this information while running by uploading the file
META-INF/MANIFEST.MF
of jar.– utluiz