Maven and android - Slightly different builds for

2019-01-19 07:11发布

问题:

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

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.

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

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>

Where config.server_url was defined in prod.properties.

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.

回答1:

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

  • AndroidManifest.xml, see this answer.
  • assets/, see this answer.
  • res/, see below.

Sample res/value/config.xml:

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

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>

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>

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