2011-03-29 51 views
5

我正在使用以下格式的版本控制应用程序的Ant构建过程:major.minor.buildcount。所以目前的应用程序大约在2.1.52,我们在2.1版本,并有35个版本。询问用户是|否输入

我现在添加一个蚂蚁目标,询问用户他们是否想推进主要版本和/或次要版本。

当我在命令行中运行我的目标,我想遵循如下:

@@ ant version 
Versioning application... 
Would you like to advance the major version to 3? (Y|n) 
@@ n 
Not Advancing major version 
Would you like to advance the minor version to 2? (y|N) 
@@ y 
Advancing minor version 

与@@是用户输入,我想借此前缀的线条。我的主版本和次版本存储在build.properties文件中。

这里是我到目前为止的代码

<?xml version="1.0"?> 
<project name="StudentMS" default="zip" basedir="."> 
    <propertyfile file="./.ant/build.properties"> 
     <entry key="version.buildnumber" type="int" default="0" operation="+" pattern="00" /> 
    </propertyfile> 

    <property file="./.ant/build.properties" /> 
    <property name="sourceDir" location="/Users/dave/Workspace/ColdFusion/StudentMs" /> 
    <property name="buildDir" location="${sourceDir}/builds" /> 

    <target name="version" description="Adds a major and minor version to the build."> 
     <input message="Advance major version? ${version.major}" addproperty="updatemajor" validargs="y,n" defaultvalue="n" /> 
     <propertyfile file="./.ant/build.properties"> 
      <entry key="version.major" type="int" default="0" operation="+" pattern="00" /> 
     </propertyfile> 

     <input message="Advance minor version? ${version.minor}" addproperty="updateminor" validargs="y,n" defaultvalue="y" /> 
     <propertyfile file="./.ant/build.properties"> 
      <entry key="version.minor" type="int" default="0" operation="+" pattern="00" /> 
     </propertyfile> 
    </target> 
</project> 

我的build.properties

#Tue, 29 Mar 2011 11:46:30 -0400 
version.buildnumber=35 
version.major=2 
version.minor=1 

我仍然很新的蚂蚁,所以我很抱歉,我不能发表更多先进的代码。所以我需要做的第一件事是在我的属性文件编辑中添加一些条件。

+1

请提供关于“有条件的种类”的更多细节。 也许AntForms(http://antforms.sourceforge.net/)就是你要找的东西,在某些类型的逻辑f.e中,在ant脚本中基于表单的交互。参数是必需的和可选的,如果并且除非widget的条件存在..等。推荐用于基于form的与ant的交互! – Rebse 2011-03-29 19:47:36

+0

基本上如果用户回答yes(y)然后更新属性文件,否则不要做任何事情。我真的更喜欢不使用Antforms,因为那样会消除我从对该项目非常重要的命令行执行构建的能力。 – 2011-03-29 20:46:13

+0

Ant Flaka(http://code.google.com/p/flaka/)是一个新的Ant插件,它提供了一种创新的表达式语言。除此之外,Flaka还提供条件式和重复式控制结构,例如,除非,当,选择,切换..因此,对于您的条件要求,您可以使用= 您的嵌套任务.. for if | else you use the flaka choose | when | otherwise construct 有关更多详细信息,请参阅全面的Flaka文档= http://flaka.googlecode.com/files/flaka.pdf – Rebse 2011-03-30 19:33:18

回答

7

你想要的东西可以通过结合条件和antcall任务并添加一些额外的目标来实现。

我觉得这样的事情应该工作:

<property file="./.ant/build.properties" /> 
<property name="sourceDir" location="/Users/dave/Workspace/ColdFusion/StudentMs" /> 
<property name="buildDir" location="${sourceDir}/builds" /> 

<target name="version" description="Adds a major and minor version to the build."> 
    <input message="Advance major version? ${version.major}" addproperty="updatemajor" validargs="y,n" defaultvalue="n" /> 

    <condition property="executeMajor"> 
     <and> 
      <isset property="updatemajor" /> 
      <equals arg1="${updatemajor}" arg2="y" /> 
     </and> 
    </condition> 

    <antcall target="update_major" /> 

    <input message="Advance minor version? ${version.minor}" addproperty="updateminor" validargs="y,n" defaultvalue="y" /> 

    <condition property="executeMinor"> 
     <and> 
      <isset property="updateminor" /> 
      <equals arg1="${updateminor}" arg2="y" /> 
     </and> 
    </condition> 

    <antcall target="update_minor" /> 

</target> 

<target name="update_major" if="executeMajor"> 
    <!-- Code to update major here --> 
</target> 

<target name="update_minor" if="executeMinor"> 
    <!-- Code to update minor here --> 
</target> 

基本上,它是仅有的情况下executeMajor和executeMinor性质的updatemajor/updateminor被设置为“y”。然后,只要设置了executeMajor/Minor变量,ant就会运行更新目标,否则它将跳过它们。

+0

你可以在[Condition](http://ant.apache.org/manual/Tasks/condition.html)[AntCall](http://ant.apache.org/manual/Tasks)中查看不同的使用过的ant任务/antcall.html) – jbalsas 2011-03-29 21:09:28

0

替代方案将支持用户输入和无人参与构建。

您可以在命令行上定义ant属性。所以,当你要提前一个版本,你可以做这样的事情:

 ant -Dbuild.version.advanceMinor=true 

这种做法也将使您避免大多数的构建额外的步骤。

+0

但是,解决了我需要做一个条件式的问题。我仍然会在我的构建文件中需要说'如果advanceMinor == true这样做;否则不要这样做,这是我不知道该怎么做的。 – 2011-03-29 20:47:40

+0

哦!您可以将“if”属性添加到目标,只有在定义属性时才会导致它们发生。请参阅http://ant.apache.org/manual/targets.html#targets。此外,还有一个“条件”任务可以根据逻辑运算符和条件设置属性 - 请参阅http://ant.apache.org/manual/Tasks/condition.html。 – 2011-03-30 17:28:35