android-maven-plugin and resource filtering
Asked Answered
P

3

4

I'm newbie in maven and trying to configure it to build my android project with android-maven-plugin. I have an application.properties file in assets directory which contains different application settings. And i want to obtain this values from my pom. In properties file i define one property as

myFilteredProperty=${helloFromPOM}

and also define the same property in POM:

    <properties>
        <helloFromPOM>MY PROPERTY</helloFromPOM>
    </properties>

Switched on filtering on assets dir

<build>
    ...
    <resources>
        <resource>
            <directory>${project.basedir}/assets</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
    ...
</build>

And configure recources plugin as in samples for android-maven-plugin

        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <executions>
                <execution>
                    <phase>initialize</phase>
                    <goals>
                        <goal>resources</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

But when i'm trying to show Toast in my application with this property i see ${helloFromPOM} instead MY PROPERTY.

In my target directory which generated by maven i see this properties file in two places:

  • /target/generated-sources/combined-assets/assets/application.properties
  • /target/myapp-0.0.1-SNAPSHOT.apk\application.properties

First is wrong, it contains ${helloFromPOM} instead MY PROPERTY. Second, inside the apk file is correct. But when i install this apk on the device it show me wrong value ${helloFromPOM}.

What did i do wrong?

EDIT

Thanks for answer, I tryed it, but get this error:

[ERROR] Error when generating sources. org.apache.maven.plugin.MojoExecutionException: at com.jayway.maven.plugins.android.phase01generatesources.GenerateSourcesMojo.generateR(GenerateSourcesMojo.java:338) at com.jayway.maven.plugins.android.phase01generatesources.GenerateSourcesMojo.execute(GenerateSourcesMojo.java:102) at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59) at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183) at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161) at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320) at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156) at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537) at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196) at org.apache.maven.cli.MavenCli.main(MavenCli.java:141) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290) at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230) at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409) at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352) at org.codehaus.classworlds.Launcher.main(Launcher.java:47) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120) Caused by: com.jayway.maven.plugins.android.ExecutionException: ANDROID-040-001: Could not execute: Command = cmd.exe /X /C ""C:\Program Files\Android\android-sdk\platform-tools\aapt.exe" package -m -J D:\projects\myapp\target\generated-sources\r -M D:\projects\myapp\AndroidManifest.xml -S D:\projects\myapp\target\filtered-assets --auto-add-overlay -A D:\projects\myapp\assets -I "C:\Program Files\Android\android-sdk\platforms\android-8\android.jar"", Result = 1 at com.jayway.maven.plugins.android.CommandExecutor$Factory$1.executeCommand(CommandExecutor.java:215) at com.jayway.maven.plugins.android.phase01generatesources.GenerateSourcesMojo.generateR(GenerateSourcesMojo.java:336) ... 28 more [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 4.307s [INFO] Finished at: Wed Jun 06 10:37:14 MSK 2012 [INFO] Final Memory: 7M/16M [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal com.jayway.maven.plugins.android.generation2:android-maven-plugin:3.2.0:generate-sources (default-generate-sources) on project magent: MojoExecutionException: ANDROID-040-001: Could not execute: Command = cmd.exe /X /C ""C:\Program Files\Android\android-sdk\platform-tools\aapt.exe" package -m -J D:\projects\myapp\target\generated-sources\r -M D:\projects\myapp\AndroidManifest.xml -S D:\projects\myapp\target\filtered-assets --auto-add-overlay -A D:\projects\myapp\assets -I "C:\Program Files\Android\android-sdk\platforms\android-8\android.jar"", Result = 1 -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

When i comment <resourceDirectory>${project.build.directory}/filtered-assets</resourceDirectory> in POM, build finishing successfull, but it doesn't use filtered assests.

Also i try to execute this cmd.exe /X /C ""C:\Program Files\Android\android-sdk\platform-tools\aapt.exe" package -m -J D:\projects\myapp\target\generated-sources\r -M D:\projects\myapp\AndroidManifest.xml -S D:\projects\myapp\target\filtered-assets --auto-add-overlay -A D:\projects\myapp\assets -I "C:\Program Files\Android\android-sdk\platforms\android-8\android.jar"" in my command line and get this error: invalid resource directory name: D:\projects\myapp\target\filtered-assets/application.properties. Wrong slash instead back-slash after filtered-assets. Changing <resourceDirectory>${project.build.directory}/filtered-assets</resourceDirectory> to <resourceDirectory>${project.build.directory}\filtered-assets</resourceDirectory> also doesn't help.

P.S. my plugins section looks like this

   <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <executions>
            <execution>
                <phase>initialize</phase>
                <goals>
                    <goal>resources</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <source>1.6</source>
            <target>1.6</target>
        </configuration>
    </plugin>
    <plugin>
        <groupId>com.jayway.maven.plugins.android.generation2</groupId>
        <artifactId>android-maven-plugin</artifactId>
        <version>3.2.0</version>
        <configuration>
            <sdk>
                <platform>8</platform>
            </sdk>
            <emulator>
                <avd>2.3.3_API-10</avd>
            </emulator>
            <undeployBeforeDeploy>true</undeployBeforeDeploy>
            <resourceDirectory>${project.build.directory}/filtered-assets</resourceDirectory>
        </configuration>
        <extensions>true</extensions>
    </plugin>
Peptonize answered 5/6, 2012 at 15:15 Comment(1)
Thanks, I answer to your comment in question. See EDIT sectionPeptonize
P
8

Now it works, i change <resourceDirectory>${project.build.directory}/filtered-assets</resourceDirectory> to <assetsDirectory>${project.build.directory}/filtered-assets</assetsDirectory>. Results POM:

<properties>
    <myProperty>MY PROPERTY!!!!111</myProperty>
</properties>

    <resources>
        <resource>
            <directory>${project.basedir}/assets</directory>
            <filtering>true</filtering>
            <targetPath>${project.build.directory}/filtered-assets</targetPath>
            <includes>
                <include>**/*.properties</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>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>com.jayway.maven.plugins.android.generation2</groupId>
            <artifactId>android-maven-plugin</artifactId>
            <version>3.2.0</version>
            <configuration>
                <sdk>
                    <platform>8</platform>
                </sdk>
                <emulator>
                    <avd>2.3.3_API-10</avd>
                </emulator>
                <undeployBeforeDeploy>true</undeployBeforeDeploy>
                <assetsDirectory>${project.build.directory}/filtered-assets</assetsDirectory>
            </configuration>
            <extensions>true</extensions>
        </plugin>
Peptonize answered 6/6, 2012 at 7:29 Comment(0)
P
0

If you are specifying the property in your pom you shouldn't have it declared in a separate file as well. Conversely, if you want to specify it in a separate file you shouldn't declare it in your POM.

However, I am not sure exactly what you are trying to do - maven properties are only valid within your pom file and are for use during the build process. You can't access them from within your application. Android/java have no idea they exist and can't see them, so the reason you see the ${helloFromPOM} in your code instead of MY PROPERTY is that android/java is just displaying the text - it doesn't know that it stands for a property and it has no way to parse it into a variable or other value.

Piggott answered 5/6, 2012 at 19:16 Comment(1)
I don't want to use POM properties from my application directly. I want to use maven resource filtering for it. maven.apache.org/plugins/maven-resources-plugin/examples/…Peptonize
S
0

Sometimes it's caused by an inappropriate platform version. Try to set it right in your pom file. Look for smthg like

<sdk>
    <platform>17</platform>
</sdk>
Sandstorm answered 8/4, 2013 at 9:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.