【问题标题】:Maven and android - Slightly different builds for different environmentsMaven 和 android - 针对不同环境的稍微不同的构建
【发布时间】:2012-12-01 18:34:23
【问题描述】:

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

目前我有一个自定义 build.xml 脚本,它有几个用于创建发布版本的目标。它们中的每一个都用于构建应用程序以针对不同的服务器 URL 运行,其中服务器可以是我们在其他国家/地区拥有的开发、生产、登台甚至部署服务器。

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

这在 ant 中很容易设置,我只需从 [env].properties 文件中获取值,然后替换 res/values/config.xml 中的 server_url 字符串。例如:

ant release-prod

将读取一个名为 prod.properties 的文件,该文件定义了 server_url 是什么。我将 config.xml 文件存储在 config/config.xml 中:

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

然后我的 ant 脚本会这样做:

<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 中定义的。

我想知道如何使用 Maven 完成类似的事情?有任何想法吗。我查看了如何使用 maven 读取属性文件,看起来结果好坏参半。

【问题讨论】:

  • 别这样! (不要切换)再等一会儿,直接从 Ant 切换到 Gradle!
  • 为什么? Gradle 看起来很酷,但是 gradle/android 的作品中有什么重要的东西吗?我最近对所有这些技术感到不知所措,我害怕尝试任何新事物。但我喜欢这个主意。看起来 maven 非常适合处理依赖项,但作为通用构建工具,它似乎不太理想。
  • 哦,我明白了,看起来 gradle 将成为新的构建系统。看起来 gradle 无论如何都使用 maven 存储库,所以现在进行更改可能不是问题,只要它不需要太多的工作来完成我想要的。我认为最大的障碍是为当前项目设置所有存储库内容/依赖项。
  • 但是 Gradle 也可以运行你的 ant 脚本 ;-)
  • @Blundell,让Gradle像Maven一样成熟恐怕短期内不会发生(全功能支持gradle-android-plugin和IDE集成)。您可能对this discussion 感兴趣。

标签: android maven ant


【解决方案1】:

在 Maven 中,这称为资源过滤,android-maven-plugin 支持过滤以下资源类型:

示例 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配置:

<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 中定义它们,如下所示:

<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。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    • 2021-03-19
    • 1970-01-01
    • 2018-10-26
    • 2021-03-19
    相关资源
    最近更新 更多