android-maven-plugin和资源过滤

时间:2021-12-06 14:14:06

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

我是maven的新手,并尝试配置它以使用android-maven-plugin构建我的android项目。我有一个应用程序。资产目录中的属性文件,其中包含不同的应用程序设置。我想从pom中得到这个值。在属性文件中,我定义了一个属性。

myFilteredProperty=${helloFromPOM}

and also define the same property in POM:

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

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

Switched on filtering on assets dir

打开对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

并配置recources插件,如在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.

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

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

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

  • /target/generated-sources/combined-assets/assets/application.properties
  • / /生成的源代码/资产总额/资产/ application.properties目标
  • /target/myapp-0.0.1-SNAPSHOT.apk\application.properties
  • /目标/ 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}.

首先是错误的,它包含${helloFromPOM}而不是我的属性。其次,apk文件内部是正确的。但是当我在设备上安装这个apk时,它显示了我的错误值${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

[错误]产生源时的错误。org.apache.maven.plugin。MojoExecutionException:com. jaywe . maven.mavendo . android.phase01generatources . generatesources. generatesmojo . generatesources. java execuatesources . moexample .java executive.com . org.apache. mavene .生命周期。内部。mojoexecutorl .execute(mojoexecutorl .java .javaorg. apache.maven.maven.maven.doexecute (LifecycleStarter.java . org. apache.maven.maven.maven.doexecute (DefaultMaven.java . apache.maven.maven.mavest . mavest . mavest . mavest . mavest . maven.org.org.org.mact .)。在sun.reflec . nativemethodorimpl .invoke(nativemethodaccessor .invoke) (nativemethodaccess .java . laun.39)在sun. reflec.reflec . delegate . methodaccessorimpl .invoke .invoke .invoke(委托方法访问方法访问方法访问方法访问方法访问方法)。在org.code .codehaus.plexus. classworlds.launcher.launcher.main (Launcher.java:352)网站上,codehaus.classworlds.launcher .main(Launcher.java:47)网站上,sun.reflect.NativeMethodAccessorImpl。由sun.reflec . nativemethodorimpl .invoke(nativemethodaccessorimpl .invoke) (nativemethodaccessorimpll .java:39)在sun.reflec . delegate . methodaccessorimpl .invoke .invoke(delegate) (delegatingmethodaccessor .java: delegate) . executiona.l.a.l.a.l.l.l.l.l.l.l.l.m . methodm.c。无法执行:命令= cmd。\程序文件\Android\ Android -sdk\平台工具\aapt。程序包-m - jd:\项目\应用程序\目标\生成资源\r -m - D:\项目\应用程序\AndroidManifest。\项目\项目\应用程序\目标\过滤资产-自动添加-覆盖扩展D:\项目\应用程序\资产-I“C”\程序文件\Android\ Android\ Android -sdk平台\Android -8\ Android。jar“36”,结果= 1在com.jayway.maven.plugins.android.CommandExecutor$ 1工厂$1. executecommand(征用).java:215在com.jayway.maven.plugins. android.phase1generatesources . generatesources. mojo . generatourcesmojoater28更多[信息]- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -[信息]构建失败[信息]- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -[信息]总时间:4.307秒[信息]了:结婚2012年6月06 10:37:14 MSK[信息]最后的记忆:7米/ 16 m[信息]- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -(错误)未能执行目标com.jayway.maven.plugins.android.generation2:android-maven-plugin:3.2.0:generate-sources(default-generate-sources)项目磁铁:MojoExecutionException:android - 040 - 001:不能执行:命令= cmd。\程序文件\Android\ Android -sdk\平台工具\aapt。exe" package -m -J D:\projects\myapp\target\ generatedsources \r -m D:\projects\myapp\AndroidManifest。\项目\项目\应用程序\目标\过滤资产-自动添加-覆盖扩展D:\项目\应用程序\资产-I“C”\程序文件\Android\ Android\ Android -sdk平台\Android -8\ Android。jar",结果= 1 ->[帮助1][错误][错误]查看错误的完整堆栈跟踪,用-e开关重新运行Maven。[ERROR]使用-X开关重新运行Maven,以启用完全调试日志记录。有关错误和可能的解决方案的更多信息,请阅读以下文章:[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.

当我< resourceDirectory > $ { project.build置评。POM的目录}/ filteredassets ,构建完成的成功,但它不使用过滤的资产。

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.

我也尝试执行这个cmd。\程序文件\Android\ Android -sdk\平台工具\aapt。exe" package -m -J D:\projects\myapp\target\ generatedsources \r -m D:\projects\myapp\AndroidManifest。\项目\项目\应用程序\目标\过滤资产-自动添加-覆盖扩展D:\项目\应用程序\资产-I“C”\程序文件\Android\ Android\ Android -sdk平台\Android -8\ Android。在我的命令行中jar""并得到这个错误:无效的资源目录名:D:\项目myapp\目标\过滤器-资产/应用程序.properties。错误的反斜杠后过滤资产。< resourceDirectory > $ { project.build变化。目录} / filtered-assets < / resourceDirectory > < resourceDirectory > $ { project.build。目录} \ filtered-assets < / resourceDirectory >也没有帮助。

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>

3 个解决方案

#1


8  

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

现在,我更改了 ${project.build。目录} / 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>

#2


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.

如果在pom中指定属性,则不应该在单独的文件中声明它。相反,如果您想在单独的文件中指定它,则不应该在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.

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

#3


0  

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

有时它是由一个不合适的平台版本引起的。尝试在pom文件中设置它。寻找smthg像

<sdk>
    <platform>17</platform>
</sdk>

#1


8  

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

现在,我更改了 ${project.build。目录} / 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>

#2


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.

如果在pom中指定属性,则不应该在单独的文件中声明它。相反,如果您想在单独的文件中指定它,则不应该在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.

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

#3


0  

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

有时它是由一个不合适的平台版本引起的。尝试在pom文件中设置它。寻找smthg像

<sdk>
    <platform>17</platform>
</sdk>