使用ANT更新内部版本号并注入源代码

发布于 2024-09-13 16:26:49 字数 1313 浏览 5 评论 0原文

在我的 build.xml 文件中,我在属性文件中增加构建版本号,如下所示:

<target name="minor">
     <propertyfile file="build_info.properties">
         <entry key="build.minor.number" type="int" operation="+" value="1" pattern="00" />
         <entry key="build.revision.number" type="int" value="0" pattern="00" />
     </propertyfile>
</target>

我也有类似的主要和修订条目。 (来自内部版本号:major.minor.revision

这非常有效。现在我想获取这个递增的内部版本号并将其注入到我的源代码中:

    //Main.as
    public static const VERSION:String = "@(#)00.00.00)@";

通过使用:

<target name="documentVersion">
    <replaceregexp file="${referer}" match="@\(#\).*@" replace="@(#)${build.major.number}.${build.minor.number}.${build.revision.number})@" />
</target>

现在这种方法可以工作了。它确实用过时的版本号替换了版本 but。因此,每当我运行 ANT 脚本时,build_info.properties 就会更新为正确的版本,但我的源代码文件正在使用预先更新的值。

我已经回显以检查在调用替换之前我确实在增加内部版本号,并且我注意到回显:

<echo>${build.minor.number}</echo> 
//After updating it still shows old non updated value here but the new value in the property file.

那么有没有一种方法可以检索属性文件中的更新值,以便我可以使用它注入到我的源代码中?

干杯

In my build.xml file I am incrementing a build version number in a property file like so:

<target name="minor">
     <propertyfile file="build_info.properties">
         <entry key="build.minor.number" type="int" operation="+" value="1" pattern="00" />
         <entry key="build.revision.number" type="int" value="0" pattern="00" />
     </propertyfile>
</target>

I also have similar entries for the major and revision. (from Build numbers: major.minor.revision)

This works great. Now I would like to take this incremented build number and inject it into my source code:

    //Main.as
    public static const VERSION:String = "@(#)00.00.00)@";

By using:

<target name="documentVersion">
    <replaceregexp file="${referer}" match="@\(#\).*@" replace="@(#)${build.major.number}.${build.minor.number}.${build.revision.number})@" />
</target>

Now this sorta works. It does indeed replace the version but with the outdated version number. So whenever I run the ANT script the build_info.properties is updated to the correct version but my source code file is using the pre updated value.

I have echoed to check that indeed I am incrementing the build number before I call the replace and I have noticed that echoing:

<echo>${build.minor.number}</echo> 
//After updating it still shows old non updated value here but the new value in the property file.

So is there a way to retrieve the updated value in the property file so I can use it to inject into my source code?

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淡忘如思 2024-09-20 16:26:49

因此,在花了几个小时无法解决这个问题后,我发布了这个问题,然后在 20 分钟后解决了这个问题。

问题是我在构建文件的顶部有这个:

<property file="build_info.properties"/>

我想这是由于作用域和属性是不可变的,因此我永远无法更新该值。删除该行然后添加以下内容使其完美运行:

<target name="injectVersion">
     <property file="build_info.properties"/>
     <replaceregexp file="${referer}" match="@\(#\).*@" replace="@(#)${build.major.number}.${build.minor.number}.${build.revision.number})@" />
</target>

So after spending hours not being able to solve this, I post this question and then figure it out 20 minutes later.

The problem was I had this at the top of my build file:

<property file="build_info.properties"/>

I guess it was due to scoping and that properties are immutable thus I was never able to update the value. Removing that line and then adding the following got it working perfectly:

<target name="injectVersion">
     <property file="build_info.properties"/>
     <replaceregexp file="${referer}" match="@\(#\).*@" replace="@(#)${build.major.number}.${build.minor.number}.${build.revision.number})@" />
</target>
年华零落成诗 2024-09-20 16:26:49

为什么不直接使用

Why not just use <buildnumber/>?

放我走吧 2024-09-20 16:26:49
 Project used to increment build number in build.properties file

 file parameters:
 version.number=                
 build.number=        

 if changes version.number then build.number starts from 1                                                      
 ====================================================================== -->

<property name="versionFileName" value="build.properties" />
<property file="${versionFileName}" />  
<property name="currentVersion" value="0.1.37"/>

<target name="calculate.version.build">
    <script language="javascript">
        <![CDATA[             
            var currentVersion = project.getProperty("currentVersion");
            var oldVersion = project.getProperty("version.number");
            var buildNumber = project.getProperty("build.number");

            if (!currentVersion.equals(oldVersion)){
                project.setProperty("currentBuild", 1);
            } else {
                var newBuildNumber = ++buildNumber;
                project.setProperty("currentBuild", newBuildNumber);
            }
        ]]> 
    </script>
</target>

<target name="update.version.build" depends="calculate.version.build">
    <propertyfile file="${versionFileName}">
        <entry key="build.number" type="int" operation="=" value="${currentBuild}" />
        <entry key="version.number" type="string" operation="=" value="${currentVersion}" />
    </propertyfile>
    <echo message="New version: ${currentVersion}.${currentBuild}" />
</target>

 Project used to increment build number in build.properties file

 file parameters:
 version.number=                
 build.number=        

 if changes version.number then build.number starts from 1                                                      
 ====================================================================== -->

<property name="versionFileName" value="build.properties" />
<property file="${versionFileName}" />  
<property name="currentVersion" value="0.1.37"/>

<target name="calculate.version.build">
    <script language="javascript">
        <![CDATA[             
            var currentVersion = project.getProperty("currentVersion");
            var oldVersion = project.getProperty("version.number");
            var buildNumber = project.getProperty("build.number");

            if (!currentVersion.equals(oldVersion)){
                project.setProperty("currentBuild", 1);
            } else {
                var newBuildNumber = ++buildNumber;
                project.setProperty("currentBuild", newBuildNumber);
            }
        ]]> 
    </script>
</target>

<target name="update.version.build" depends="calculate.version.build">
    <propertyfile file="${versionFileName}">
        <entry key="build.number" type="int" operation="=" value="${currentBuild}" />
        <entry key="version.number" type="string" operation="=" value="${currentVersion}" />
    </propertyfile>
    <echo message="New version: ${currentVersion}.${currentBuild}" />
</target>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文