Android的 - 使用ant创建构建配置,更改配置值 [英] Android - use ant to create build configurations that change configuration values
问题描述
我要的是一种具有设置是依赖于构建配置。举一个具体的例子,我的Android应用程序连接到Web服务。在发展中,我希望服务的URL从一个可配置的值拉到研究。在测试中,我想拉一个不同的值,在生产,另一种价值。
所以,在code我有这样的事情:
公共类的Htt prequestHelper
{
私有静态字符串GetServiceUrl(ServiceAction动作)
{
返回的serviceUrl + action.toString();
}
}
默认情况下(在调试的时候/通过Eclipse中运行),我想该网址是的http://本地主机:1234
在测试我想 https://test.mydomain.com
在制作我想 https://mydomain.com
我是新来的Eclipse和蚂蚁,它一直以来我用java很长一段时间。我该如何去设置此功能?我应该在build.xml是什么样子?我明白,当我想建立测试/生产版本我将需要使用命令行。没关系。但我不知道如何得到这个的serviceUrl自动设定依赖于构建。我甚至不知道最好的地方,把这个信息(资源,属性文件?)。我真的想避免设置它,建筑,将其设置,建筑等。
作为答案上面提到说,你必须把这些URL像dev.properties,test.properties,prod.properties等。<属性文件/ P>
现在只是你需要做的事情是让你的身材够聪明的选择取决于环境中的属性文件。
这可以通过传递参数ANT做,是这样的:
$蚂蚁 - 文件MyBuild.xml -DcurrentEnv =开发(用于开发环境)
$蚂蚁 - 文件MyBuild.xml -DcurrentEnv =测试(测试)
$蚂蚁 - 文件MyBuild.xml -DcurrentEnv = PROD(用于生产)
在你的构建脚本,这是你可以包括你的属性文件:
&lt;目标名称=jarMe&GT;
&LT;罐子destfile =sample.jarBASEDIR =SRC包括=$ {currentEnv}的.properties/&GT;
&LT; /目标和GT;
有了这个地方,任何名义您提供在构建时,与该名称属性文件将有所回升。
What I want is a way to have settings that are dependent on build configuration. To give a specific example, my android application connects to a web service. In development, I want the service url to be pulled in from a configurable value. In Test, I want a different value pulled in. In production, yet another value.
So, in code I have something like this:
public class HttpRequestHelper
{
private static String GetServiceUrl(ServiceAction action)
{
return serviceUrl + action.toString();
}
}
By default (when debugging/running through eclipse), I want that url to be http://localhost:1234
In Test I want https://test.mydomain.com
In Production I want https://mydomain.com
I am new to eclipse and ant and it has been a long time since I used java. How do I go about setting this up? What should the build.xml look like? I understand that when I want to build the test/prod versions I will need to use the command line. That's okay. But I don't know how to get this serviceUrl auto-set dependent on the build. I'm not even sure the best place to put this information (a resource, a properties file?). I really want to avoid setting it, building, setting it, building, etc.
As answers mentioned above says, you have to place the URLs in a property file like dev.properties, test.properties, prod.properties etc..
Now only thing that you need to do is making your build intelligent enough to choose a property file depending upon environment.
That can be done by passing a parameter to ANT, something like:
$ ant -file MyBuild.xml -DcurrentEnv=dev (For Development environment)
$ ant -file MyBuild.xml -DcurrentEnv=test (For Test)
$ ant -file MyBuild.xml -DcurrentEnv=prod (For Production)
Inside your build script, this is how you can include your property file:
<target name="jarMe">
<jar destfile="sample.jar" basedir="src" includes="${currentEnv}.properties"/>
</target>
With this in place, whatever name you supply at the time of build, property file with that name will be picked up.
这篇关于Android的 - 使用ant创建构建配置,更改配置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!