好的,我在一个 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 读取属性文件,无论是否可行,结果看起来都是混合的。
最佳答案
在Maven中,这叫做资源过滤,android-maven-plugin支持过滤以下资源类型:
- AndroidManifest.xml,see this answer .
- Assets /,参见 this answer .
- res/,见下文。
示例 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。
关于android - Maven 和 android - 针对不同环境的构建略有不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13867148/