android - Using Maven to build application for different environments (dev, staging, prod) -


suppose have following properties declared in pom.xml file.

<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org     /2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">      // ....      <properties>         <dev.url>http://dev.mysite.com</dev.url>         <staging.url>http://staging.mysite.com</staging.url>         <prod.url>http://prod.mysite.com</prod.url>     </properties>  </project> 

what do, have different run configurations, , depending of flag set, read corresponding property. example when building production, read @ runtime prod.url property, when building staging, read staging.url property.

here's how solved it:
idea store properties in separate resource files. then, using maven resource plugin copy corresponding property file values/environment.xml

in project root directory create following directory structure:

environment/dev/environment.xml environment/prod/environment.xml 

then fill content of resource files appropriate values.

/dev/environment.xml:

<?xml version="1.0" encoding="utf-8"?> <resources>     <string name="url">http://dev.mysite.com</string> </resources> 

/prod/environment.xml:

<?xml version="1.0" encoding="utf-8"?> <resources>     <string name="url">http://prod.mysite.com</string> </resources> 

add maven resource plugin pom.xml specifying phase action should executed, resources copy, , output directory:

//... <plugins>     <plugin>         <artifactid>maven-resources-plugin</artifactid>         <version>2.4.3</version>         <executions>              <execution>                 <id>copy-string-resources</id>                 <phase>validate</phase>                 <goals>                     <goal>copy-resources</goal>                 </goals>                 <configuration>                     <resources>                         <resource>                             <directory>environment/${environment}/</directory>                             <includes>                                <include>environment.xml</include>                                <filtering>true</filtering>                             </includes>                         </resource>                     </resources>                     <overwrite>true</overwrite>                     <outputdirectory>${basedir}/res/values/</outputdirectory>                 </configuration>       </plugin> </plugins> 

from point create 2 run configurations:

mvn validate -denvironment=dev mvn validate -denvironment=prod 

that nothing copy appropriate resources evironment/${environment}/environment.xml, values/environment.xml

depending of run configuration, ${environment} equal dev or prod.

you can find more info on this page.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -