Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

maven - read version number from property file

I`m trying to read my build version number from a text file, and then assign it to the generated package name: myRelease-1.1.1.apk where the build number is manually set into a property file version.number=1.1.1 How can I overload ${version.number} as set in pom.xml by the one in the property file?

Edit: more details about the project . I use git to commit the property file and then Jenkins takes over and builds with Maven. I would like to end up with a build “myBuild-9.9.9.apk”. Right now I have “myBuild-1.1.2.apk” In extras/version.properties

project.version=9.9.9  

In pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <parent>
            .....
            <version>1.1.2</version>
      </parent>

      <groupId>ca.lapresse.android</groupId>
      <artifactId>lapresse-hockey-app</artifactId>
      <packaging>apk</packaging>
      <name>La Presse Hockey - App</name>
      <version>1.1.2</version>

…… It seems that ${project.artifactId} takes the version number from <version></version> and it becomes 1.1.2. This should be reflected in ${project.version}, which I’m trying to overload from the property file.

<plugin>
<groupId>org.codehaus.mojo</groupId>
      <artifactId>properties-maven-plugin</artifactId>
      <version>1.0-alpha-2</version>
      <executions>
            <execution>
                  <phase>process-resources</phase>
                  <goals>
                        <goal>read-project-properties</goal>
                  </goals>
                  <configuration>
                        <files>
                              <file>${project.basedir}/extras/version.properties</file>
                        </files>
                  </configuration>
            </execution>
      </executions>
</plugin>

What am I doing wrong and what I am not doing at all? My understanding of Maven is very rudimentary (I come from an Ant background).

like image 424
alex Avatar asked Dec 16 '11 23:12

alex


People also ask

How do you read values from properties file in POM xml?

maven properties plugin is used to read the properties from an external file. In pom. xml, create a property under the properties tag for the property file location. On running the mvn validate command.

How Maven read properties file in Java?

First, we need to define the properties file called file. properties as shown below under src/main/files. Let's define the class called FileProperties and load this properties file with the FileInputStream. This method throws the exception called FileNotFoundException if the file is not found.

What is Maven property file?

Maven supports a hierarchy of different properties to allow specifying defaults and overriding them at the appropriate level. The properties files in Maven are processed in the following order: Built-in properties are processed. ${basedir}/project. properties ( basedir is replaced by the directory where the project.


2 Answers

Read following answers:

  • User and project specific settings in Maven
  • How to read an external properties file in Maven
  • Reading properties file from Maven POM file
  • Read a file into a Maven property

or just:

<project>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>properties-maven-plugin</artifactId>
        <version>1.0</version>
        <executions>
          <execution>
            <phase>initialize</phase>
            <goals>
              <goal>read-project-properties</goal>
            </goals>
            <configuration>
              <files>
                <file>dev.properties</file> <!-- THIS!!! -->
              </files>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

UPDATE Working proof of concept is a subproject at http://sourceforge.net/u/gavenkoa/exp/ci/default/tree/maven/properties/pom.xml

Run build as mvn compile, check pom.xml and console output.

like image 181
gavenkoa Avatar answered Sep 20 '22 16:09

gavenkoa


Just to answer my own question: it turns out that Maven needs the property to be set before anything can happen in the script. As such, it is set in stone and not modifiable from a file. I ended up writing an Ant task that modifies the pom.xml and changes the version in the file, before Maven script is triggered. Ugly and non-trivial, but it works.

like image 44
alex Avatar answered Sep 20 '22 16:09

alex