2012-12-13 33 views
4

好吧我從螞蟻切換到Maven在Android項目,並想知道如果它下面會很容易實現:Maven和Android的 - 略有不同的是建立針對不同的環境

目前,我有一個自定義的build.xml腳本其中有幾個創建發佈版本的目標。它們中的每一個都用於構建應用程序以針對不同的服務器URL運行,其中服務器可以是我們在其他國家/地區的開發,生產,登臺或部署服務器。

原因是我們的應用程序將運行在幾個不同的服務器上,具體取決於誰獲得它,我不希望這是用戶選擇的東西。相反,它應該被硬編碼到應用程序中,這是它目前的工作方式。

這在ant中很容易設置,我只是從[env] .properties文件中取值,然後替換res/values/config.xml中的server_url字符串。例如:

ant release-prod 

拉會讀取一個名爲prod.properties的文件,它定義了server_url是什麼。我config.xml文件存儲在配置/ config.xml中像這樣:

<?xml version="1.0" encoding="UTF-8"?> 
<resources> 
    <string name="config_server_url">@[email protected]</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讀取屬性文件,看起來好像結果是混合的,無論這是否工作。

+0

不要這樣做! (不要切換)等一會兒,然後直接從Ant切換到Gradle! – Blundell

+0

爲什麼? Gradle看起來很酷,但是在gradle/android的作品中有一些主要的東西嗎?最近我被這麼多技術所壓倒,我害怕嘗試任何新的技術。但我喜歡這個主意。看起來maven非常適合處理依賴關係,但作爲一個通用的構建工具,它看起來並不理想。 –

+0

哦,我看,看起來像gradle將會是新的構建系統。它看起來像gradle反正使用maven倉庫,所以或許現在做出改變不是一個問題,只要它沒有太多的工作來完成我想要的東西。我認爲最大的障礙是爲我當前的項目設置所有的存儲庫資料/依賴項。 –

回答

5

在Maven中,這就是所謂的資源篩選,Android的Maven的插件支持過濾以下資源類型:

樣品RES /值/ config.xml中:用於過濾所有xml文件下RES /目錄

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

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

相關問題