针对不同环境的构建略有不同

针对不同环境的构建略有不同

本文介绍了Maven 和 android - 针对不同环境的构建略有不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我正在一个 android 项目上从 ant 切换到 maven,想知道以下是否容易实现:

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.

原因是我们的应用程序将在几个不同的服务器上运行,这取决于谁获得它,我不希望这是用户选择的东西.相反,它应该被硬编码到应用程序中,这就是它目前的工作方式.

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.

这在 ant 中设置相当容易,我只需从 [env].properties 文件中获取值,然后替换 res/values/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/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>

然后我的蚂蚁脚本会这样做:

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>

在 prod.properties 中定义 config.server_url 的地方.

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-plugin 支持过滤以下资源类型:

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

示例 res/value/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>

用于过滤 res/目录下所有 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>

定义替换值的方法有多种,您可以使用 properties-maven-plugin 在外部属性文件中定义它们.为简单起见,我更喜欢使用 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 clean install -Pxxx构建对应的apk.

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

这篇关于Maven 和 android - 针对不同环境的构建略有不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 11:20