maven - read version number from property file

前端 未结 4 357
自闭症患者
自闭症患者 2020-12-19 00:56

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 int

相关标签:
4条回答
  • 2020-12-19 01:16

    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.

    0 讨论(0)
  • 2020-12-19 01:34

    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.

    0 讨论(0)
  • 2020-12-19 01:39

    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.

    0 讨论(0)
  • 2020-12-19 01:40

    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> <======== IT IS!!!!!
                  </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.

    0 讨论(0)
提交回复
热议问题