14

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).

Raedwald
  • 46,613
  • 43
  • 151
  • 237
alex
  • 477
  • 2
  • 7
  • 17
  • It relates to an Android build packaged by Maven, but it may be imperly tagged as Android as well – alex Dec 17 '11 at 00:13

4 Answers4

7

Read following answers:

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.

gavenkoa
  • 45,285
  • 19
  • 251
  • 303
  • this doesn't work if you need to refer to the property in the pom file... dont waste your time reading those other posts if you need that – JonnyRaa Jan 05 '15 at 15:22
  • @JonnyLeeds Are you sure? Check my subproject: http://sourceforge.net/u/gavenkoa/exp/ci/default/tree/maven/properties/pom.xml for testing this. Run it by `mvn compile` and you see values of properties from external files. But I agree that `properties-maven-plugin` have issues and Maven build system very limited in extensibility. – gavenkoa Jan 05 '15 at 16:46
  • I was referring to the posts you link to - I can't use this because it's expecting a properties file rather than just a plain text file. Is it the use of the plugin that makes this work. It definitely doesnt when doing something like: `project.properties.put("versionNumber", new File("version").getText())` – JonnyRaa Jan 05 '15 at 17:04
  • @JonnyLeeds OK, I see what you try to achieve. You can build property file from simple file content by Ant task on `initialize`. I hope the order of `` like them occur in `pom.xml` so after `maven-antrun-plugin` you be able to use `properties-maven-plugin`. See https://ant.apache.org/manual/Tasks/concat.html and `header` section to write `PROP = FILE-CONTENT` – gavenkoa Jan 05 '15 at 17:31
7

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.

alex
  • 477
  • 2
  • 7
  • 17
  • You should use the release plugin instead, cause the default working cycle is to have 1.1.1-SNAPSHOT during developemt and the release will have 1.1.1 as version. This can be achieved by using the release plugin which does this automatically. If you don't like to use the release plugin you can use the version-maven-plugin to change the versions in your pom. – khmarbaise Jan 06 '12 at 15:52
  • 1
    I`m trying to read my build version number from a text file, not sure how the plugins can help. – alex Jan 11 '12 at 17:02
0

I found I could work around this problem in maven by using the stuff about reading from the file suggested above, which does work for 'filtering' (putting stuff into a properties template file), so my application knows what version it is despite maven's confusion.

The reason I wanted it in the pom file is to put the version number into a couple of minified javascript file names so they wouldn't get cached across version updates... strictly speaking this doesnt require the actual version number so I used ${maven.build.timestamp} instead which outputs the date + time of the build in the format yyyymmdd-hhmm.

I think if I needed something more complex I'd start stripping stuff out into msbuild or something similar instead... Maven doesnt make things easy and after a year of battling it I dont really feel like its getting any better.

JonnyRaa
  • 7,559
  • 6
  • 45
  • 49
0

As you already got pom.xml, you don't need another extra properties file for define such a simple property, use POM's properties tag and override the default build finalName:

<properties>
  <final.build.version>1.1.1</final.build.version>
</properties>
<build>
  ... ...
  <finalName>${project.artifactId}-${final.build.version}</finalName>
  ... ...
</build>

If you use maven sign and zipalign your final apk at release stage, and want to override the final name here, use:

<build>
  <plugins>
    <plugin>
      <groupId>com.jayway.maven.plugins.android.generation2</groupId>
      <artifactId>android-maven-plugin</artifactId>
      <extensions>true</extensions>
      <inherited>true</inherited>
      <configuration>
        <undeployBeforeDeploy>true</undeployBeforeDeploy>
        <sign>
          <debug>false</debug>
        </sign>
        <zipalign>
          <verbose>true</verbose>
          <inputApk>${project.build.directory}/${project.artifactId}-${final.build.version}.apk</inputApk>
          <outputApk>${project.build.directory}/${project.artifactId}-${final.build.version}-signed-aligned.apk</outputApk>
        </zipalign>
        </configuration>
          <executions>
            <execution>
              <id>alignApk</id>
              <phase>package</phase>
              <goals>
                <goal>zipalign</goal>
              </goals>
            </execution>
          </executions>
      </plugin>
   </plugins>
</build>

EDIT:
If you have to use properties file, use properties-maven-plugin, check out similar SO question here.

Hope this help.

Community
  • 1
  • 1
yorkw
  • 40,926
  • 10
  • 117
  • 130
  • 2
    I do need that file for other purposes. The property in it is reused by other scripts. It is commited manually by developers. The final version number uses it and adds some other quallifiers, such as the timestamp. – alex Dec 17 '11 at 00:17
  • This is indeed how I read the value, but it seems that although I can read it, I cannot substitute the value defined in .pom – alex Dec 17 '11 at 00:28
  • @alex, I don't think this gonna work within IDE like Eclipse, however, as long as your build you app from command line (i.e. mvn clean install), you should be fine, double check out its [official site](http://mojo.codehaus.org/properties-maven-plugin/) for user guide. – yorkw Dec 17 '11 at 00:50
  • My build is commited with Git and Jenkins + Maven take care of the rest. Additional steps and scripts can be configured with Jenkins. – alex Dec 17 '11 at 02:11