Maven和机器人 - 稍有不同建立不同的环境 [英] Maven and android - Slightly different builds for different environments

查看:137
本文介绍了Maven和机器人 - 稍有不同建立不同的环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧我从蚂蚁切换到Maven在Android项目,并想知道如果它下面会很容易实现:

Ok I am switching from ant to maven on an android project and wondering if it the following would be easy to implement:

目前我有一个自定义的build.xml脚本,其中包含用于创建发布版本的几个目标。他们每个人都被用于构建应用程序来对不同的服务器的URL,而服务器可以是开发,生产,升级,甚至部署服务器,我们在其他国家运行。

Currently I have a custom build.xml script which has several targets for creating release builds. Each of them is used to build the application to run against a different server URL, where the server can be a dev, production, staging, or even deployment servers we have in other countries.

原因是,我们的应用程序将运行对几种不同的服务器取决于谁得到它,我不希望用户选择的是什么东西。相反,它应该很难codeD到应用程序,它是如何工作的目前。

The reason is that our application will run against several different servers depending who gets it, and I don't want that to be something the user chooses. Instead it should be hardcoded into the application, which is how it works currently.

这是很容易设置的蚂蚁,我只取值从[ENV]属性文件,然后替换在RES /价值/ config.xml中SERVER_URL字符串。例如:

This is fairly easy to setup in ant, I just take the values from the [env].properties file, and then replace out server_url string in res/values/config.xml. For instance:

ant release-prod

会拉读取名为prod.properties一个文件,该文件定义了什么是SERVER_URL了。我存储在配置/ config.xml中的config.xml文件是这样:

Would pull read a file named prod.properties which defines what the server_url was. I store the config.xml file in config/config.xml as so:

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <string name="config_server_url">@CONFIG.SERVER_URL@</string>
</resources>

然后我的ant脚本做到这一点:

Then my ant script does this:

<copy file="config/config.xml" todir="res/values" overwrite="true" encoding="utf-8">
    <filterset>
           <filter token="CONFIG.SERVER_URL" value="${config.server_url}" />
    </filterset>
</copy>

在哪里config.server_url于prod.properties定义。

Where config.server_url was defined in prod.properties.

我不知道我怎么可能做到与Maven类似的东西?有任何想法吗。我抬起头怎么读与Maven的属性文件,它看上去像结果是混合是否将工作或没有。

I'm wondering how I could accomplish something similar with Maven? Any ideas. I looked up how to read property files with maven and it looked like the results were mixed whether that would work or not.

推荐答案

在Maven的,这就是所谓的资源筛选,Android的Maven的插件支持过滤以下资源类型:

In Maven, this is called resource filtering, android-maven-plugin support filtering the following resource types:

  • 在AndroidManifest.xml中,<一个href="http://stackoverflow.com/questions/10803088/how-do-i-change-my-androidmanifest-as-its-being-packaged-by-maven/10808077#10808077">see这个答案。
  • 在资产/,请参阅<一href="http://stackoverflow.com/questions/10900017/android-maven-plugin-and-resource-filtering/10909923#10909923">this回答。
  • RES /,见下文。
  • AndroidManifest.xml, see this answer.
  • assets/, see this answer.
  • res/, see below.

样品水库/价值/ config.xml文件:

Sample res/value/config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<resources>
  <string name="config_server_url">${config.server.url}</string>
</resources>

用于过滤下的资源的所有XML文件

样品POM配置/目录:

Sample pom configuration for filtering all xml file under res/ directory:

<build>
  <resources>
    <resource>
      <directory>${project.basedir}/res</directory>
      <filtering>true</filtering>
      <targetPath>${project.build.directory}/filtered-res</targetPath>
      <includes>
        <include>**/*.xml</include>
      </includes>
    </resource>
  </resources>
  <plugins>
    <plugin>
      <artifactId>maven-resources-plugin</artifactId>
      <executions>
        <execution>
          <phase>initialize</phase>
          <goals>
            <goal>resources</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
    <plugin>
      <groupId>com.jayway.maven.plugins.android.generation2</groupId>
      <artifactId>android-maven-plugin</artifactId>
      <extensions>true</extensions>
      <configuration>
        <sdk>
          <platform>10</platform>
        </sdk>
        <undeployBeforeDeploy>true</undeployBeforeDeploy>
        <resourceDirectory>${project.build.directory}/filtered-res</resourceDirectory>
      </configuration>
    </plugin>
  </plugins>
</build>

有几种方法来定义取代的价值,您可以在外部属性定义他们的属性,Maven的插件文件。为简单起见,我preFER使用Maven配置文件和pom.xml中定义它们,就像这样:

There are several ways to define the substituted value, you can define them in an external properties file with properties-maven-plugin. For simplicity, I prefer to use Maven profiles and define them in pom.xml, like so:

<profiles>
  <profile>
    <id>dev</id>
    <properties>
      <config.server.url>dev.company.com</config.server.url>
    </properties>
  </profile>
  <profile>
    <id>Test</id>
    <properties>
      <config.server.url>test.company.com</config.server.url>
    </properties>
  </profile>
  <profile>
    <id>Prod</id>
    <properties>
      <config.server.url>prod.company.com</config.server.url>
    </properties>
  </profile>
</profiles>

然后使用 MVN全新安装-Pxxx 来建立相应的APK。

Then use mvn clean install -Pxxx to build corresponding apk.

这篇关于Maven和机器人 - 稍有不同建立不同的环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆