0

I have a file that I'd like to show the version on top. For example, if my POM.xml is on version 1.1-SNAPSHOT, I like this file also include 1.1-SNAPSHOT on top of it (or somewhere close to top). When I release, I want the version change to 1.1 (i.e. the same as POM.xml). I could write a bash script to do that before and after each Maven build, but I wonder if Maven can do that.

I see buildnumber Maven plugin may do something similar (eg. Puts the build number in manifest of a jar file). But can Maven put the "version" into a "specific file" that I specify?

1 Answers1

0

You need to use the maven-resources-plugin for filtering. Instead of putting into the file, consider the following, which makes it available to your code.

You need to put a .properties file into src/main/resources, and then enable filtering.

Here's an example for some tests:

In the pom:

    <build>
    <testResources>
        <testResource>
            <directory>src/test/resources</directory>
            <filtering>true</filtering>
        </testResource>
    </testResources>

In src/test/resources, a file test-config.properties:

project.version=${project.version}

And then the usual Java code to open a file from the classpath and feed it into the 'properties' class.

 public static void beforeClass() throws Exception {
    URL configPropUrl = Resources.getResource(AbstractIT.class, "test-config.properties");
    Properties props = new Properties();
    try (InputStream propStream = configPropUrl.openStream()) {
        props.load(propStream);
    }

    basedir = props.getProperty("basedir");
    projectVersion = props.getProperty("project.version");

}
bmargulies
  • 97,814
  • 39
  • 186
  • 310
  • Can you elaborate on " the usual Java code to open a file from the classpath and feed it into the 'properties' class"? How to do this exactly? By the way, this means doing some scripting (as opposed to have Maven handle it), right? Also, my file can't go inside src/main/resource, because it's a parent project, and its own level it doesn't have srv/main, src/test structure (It's a pom packaing project, and underneath it has 2 webapps. The webapps do have the src/main structure, but the parent project doesn't have that). Can this be achieved in a file that is just on the root of the project? – newrealtest0000 Nov 11 '15 at 19:54
  • It means you put executable code into place to read it, it isn't 'edited into your source'. – bmargulies Nov 11 '15 at 19:55
  • Thanks, that should work pretty good for a property file. Would it work also for a non-property file which is not located under resources/ directory? I'd need to put the version number in a bash script file. This file may change on each release, so we need to ideally put a version in the file, so we know which version is deployed. – newrealtest0000 Nov 11 '15 at 22:07
  • Yes if you read up on how to use more detailed config of the maven-resources-plugin. You can read from anywhere, write to anywhere, and then use the build-helper to add the target to the build. – bmargulies Nov 12 '15 at 00:11