android-maven-plugin和资源过滤

时间:2012-06-05 15:15:20

标签: android maven android-maven-plugin maven-resources-plugin

我是maven中的新手,并尝试将其配置为使用android-maven-plugin构建我的android项目。 我在assets目录中有一个application.properties文件,其中包含不同的应用程序设置。我想从我的pom中获取这些价值。在属性文件中,我将一个属性定义为

myFilteredProperty=${helloFromPOM}

并在POM中定义相同的属性:

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

切换资产目录上的过滤

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

并为{-3}}配置recources插件,用于android-maven-plugin

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

但是,当我尝试使用此属性在我的应用程序中显示Toast时,我会看到$ {helloFromPOM}而不是MY PROPERTY。

在我的maven生成的目标目录中,我在两个地方看到了这个属性文件:

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

首先是错误的,它包含$ {helloFromPOM}而不是MY PROPERTY。其次,apk文件内部是正确的。但是当我在设备上安装这个apk时,它会向我显示错误的值$ {helloFromPOM}。

我做错了什么?

修改

感谢您的回答,我试了一下,但得到了这个错误:

  

[ERROR]生成源时出错。   org.apache.maven.plugin.MojoExecutionException:at   com.jayway.maven.plugins.android.phase01generatesources.GenerateSourcesMojo.generateR(GenerateSourcesMojo.java:338)     在   com.jayway.maven.plugins.android.phase01generatesources.GenerateSourcesMojo.execute(GenerateSourcesMojo.java:102)     在   org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)     在   org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)     在   org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)     在   org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)     在   org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)     在   org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)     在   org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)     在   org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)     在org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)at at   org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)at   org.apache.maven.cli.MavenCli.execute(mavenCli.java:537)at at   org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)at at   org.apache.maven.cli.MavenCli.main(MavenCli.java:141)at at   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)     在java.lang.reflect.Method.invoke(Method.java:597)at   org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)     在   org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)     在   org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)     在   org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)     在org.codehaus.classworlds.Launcher.main(Launcher.java:47)at   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)     在java.lang.reflect.Method.invoke(Method.java:597)at   com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)   引起:com.jayway.maven.plugins.android.ExecutionException:   ANDROID-040-001:无法执行:Command = cmd.exe / X / C.   &#34;&#34; C:\ Program Files \ Android \ android-sdk \ platform-tools \ aapt.exe&#34;   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&#34; C:\ Program   Files \ Android \ android-sdk \ platforms \ android-8 \ android.jar&#34;&#34;,Result =   1点   com.jayway.maven.plugins.android.CommandExecutor $厂$ 1.executeCommand(CommandExecutor.java:215)     在   com.jayway.maven.plugins.android.phase01generatesources.GenerateSourcesMojo.generateR(GenerateSourcesMojo.java:336)     ... 28更多[INFO]   -------------------------------------------------- ---------------------- [INFO] BUILD FAILURE [INFO]   -------------------------------------------------- ---------------------- [INFO]总时间:4.307s [INFO]完成于:6月6日星期三10:37:14 MSK   2012 [INFO]最终记忆:7M / 16M [INFO]   -------------------------------------------------- ---------------------- [错误]无法执行目标   com.jayway.maven.plugins.android.generation2:Android的Maven的插件:3.2.0:产生来源   项目magent上的(default-generate-sources):MojoExecutionException:   ANDROID-040-001:无法执行:Command = cmd.exe / X / C.   &#34;&#34; C:\ Program Files \ Android \ android-sdk \ platform-tools \ aapt.exe&#34;   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&#34; C:\ Program   Files \ Android \ android-sdk \ platforms \ android-8 \ android.jar&#34;&#34;,Result =   1 - &gt; [Help 1] [ERROR] [ERROR]查看完整的堆栈跟踪   错误,用-e开关重新运行Maven。 [错误]使用重新运行Maven   -X开关启用完整的调试日志记录。 [错误] [错误]了解更多   有关错误和可能的解决方案的信息,请阅读   以下文章:[错误] [帮助1]   samples

当我在POM中评论<resourceDirectory>${project.build.directory}/filtered-assets</resourceDirectory>时,构建成功完成,但它没有使用过滤后的资产。

此外,我尝试在命令行中执行此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""并收到此错误:invalid resource directory name: D:\projects\myapp\target\filtered-assets/application.properties。在过滤资产之后,错误的斜杠反而斜线反斜杠。将<resourceDirectory>${project.build.directory}/filtered-assets</resourceDirectory>更改为<resourceDirectory>${project.build.directory}\filtered-assets</resourceDirectory>也无济于事。

P.S。我的插件部分看起来像这样

   <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>

3 个答案:

答案 0 :(得分:8)

现在可行,我将<resourceDirectory>${project.build.directory}/filtered-assets</resourceDirectory>更改为<assetsDirectory>${project.build.directory}/filtered-assets</assetsDirectory>。结果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>

答案 1 :(得分:0)

如果要在pom中指定属性,则不应在单独的文件中声明它。相反,如果要在单独的文件中指定它,则不应在POM中声明它。

但是,我不确定您要做什么 - maven属性仅在您的pom文件中有效,并且在构建过程中使用。您无法从应用程序中访问它们。 Android / java不知道它们存在并且看不到它们,所以你在代码中看到${helloFromPOM}而不是MY PROPERTY的原因是android / java只显示文本 - 它没有'我知道它代表一个属性,它无法将其解析为变量或其他值。

答案 2 :(得分:0)

有时它是由不合适的平台版本引起的。 尝试在你的pom文件中设置它。 寻找类似于

的smthg
<sdk>
    <platform>17</platform>
</sdk>