Maven Filtering

2019-05-26 20:06发布

问题:

I am using Maven (3.x) to build an Android application within Hudson. I use filtering/profiles to do text substition of the strings.xml file as part of the build.

So within the strings.xml, I will have entries such as:

<string name="app_name">${app_name}</string>

Within the pom.xml file, I have profile entries:

 <profiles>
    <profile>
        <id>local</id>
        <properties>
            <app_env>local</app_env>
       <app_name>Acme-loc</app_name>
        </properties>
    </profile>  
    <profile>
        <id>dev</id>
        <properties>
            <app_env>dev</app_env>
       <app_name>Acme-dev</app_name>
        </properties>
    </profile>      
    ....

When I look inside the *.apk, the strings.xml file is substituted correctly. However, when installed in device, I see ${app_name} which leads me to believe that the substitution is happening after the compile of app. Can I specify which step of the build the substitution happens?

回答1:

In a standard maven build, resource filtering happens at process-resources, which comes before compile, which comes before package. It's hard to say more without more details about how you have resource filtering set up and how your package is built.

Edit: It looks like you're probably generating the R.java file using the android plugin's generate-sources goal, right? That's most likely bound to the generate-sources phase. Your strings.xml doesn't get filtered until the process-resources phase, which comes later in the build. You need to make sure that the android goal runs after the resource filtering happens and that it points at the output strings.xml file, not the source file.



回答2:

There is an example of how to apply Maven resource filtering to Android resource files in the Maven-Android-Plugin samples, and also a brief discussion about it.

Specifically have a look at the Morseflash app pom.xml in the samples.

The key points are:-

  • run resource filtering during the initialize phase, putting the output into /target/filtered-res
  • configure the maven-android-plugin to then pickup it's resources from the filtered resources directory e.g.:

<resourceDirectory>${project.build.directory}/filtered-res</resourceDirectory>