我是maven的新手,正试图将其配置为使用android maven插件构建我的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>
并配置资源插件,如android maven插件的示例中所示
<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
First是错误的,它包含${helloFromPOM}而不是MY PROPERTY。其次,apk文件内部是正确的。但当我在设备上安装这个apk时,它显示了错误的值${helloFromPOM}。
我做错了什么?
编辑
谢谢你的回答,我试过了,但得到了这个错误:
[ERROR]生成源时出错。org.apache.maven.plugin.MojoExecutionException:位于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.hifecycle.internal.MojoExecution.execute(MojoExecutitor.java:209)在org.apache.maven.hifecycle.internal.MojoExecution.execute(MojoExecutitor.java:153)在org.apache.maven.hifecycle.internal.MojoExecution.execute(MojoExecutitor.java:145)在org.apache.maven.hifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModelBuilder.java:84)在org.apache.maven.hifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModelBuilder.java:59)在org.apache.maven.hifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleSStarter.java:183)在org.apache.maven.hifecycle.internal.LifecycleStarter.execute(LifecycleSstarter.java:161)在org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)org.apache.maven.cli.MavenCli.main(MavenCli.java:141)位于的sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)位于java.lang.reflect.Method.ioke(Method.java:597)org.codehaus.cluster.classworlds.launcher.launcher.launchEnhanced(launcher.java:290)在org.codehaus.cluster.classworlds.launcher.launcher.launch(launcher.java:230)在org.codehaus.cluster.classworlds.launcher.launcher.mainWithExitCode(launcher.java:409)在org.codehaus.cluster.classworlds.launcher.launcher.main(launcher.java:352)网址:org.codehaus.classworlds.Launcher.main(Launcher.java:47)位于的sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)位于java.lang.reflect.Method.ioke(Method.java:597)com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)原因:com.jayway.maven.plugins.android.ExecutionException:ANDROID-040-001:无法执行:命令=cmd.exe/X/C"C:\Program Files\Android\Android-sdk\platform tools\aapt.exe"package-m-J D:\projects\myapp\target\生成的源\r-mD: \projects\myapp\AndroidManifest.xml-SD: \projects\myapp\target\已筛选资产--自动添加覆盖-AD: \projects\myapp\assets-I"C:\ProgramFiles\Android\Android-sdk\platforms\Android-8\Android.jar",结果=1在com.jayway.maven.plugins.android.CommandExecutor$Factory$1.executeCommand(CommandExecutor.java:215)在com.jayway.maven.plugins.android.phase01generatesources.GenerateSourcesMojo.generateR(GenerateSourcesMojo.java:336)…28更多[信息]------------------------------------------------------------------------[INFO]生成失败[INFO]------------------------------------------------------------------------[信息]总时间:4.307s[信息]完成时间:6月6日星期三上午10:37:142012【信息】最终存储器:7M/16M【信息】------------------------------------------------------------------------[错误]无法执行目标com.jayway.maven.plugins.android.generation2:android-maven-plugin:3.2.0:生成源项目映像上的(默认生成源):MojoExecutionException:ANDROID-040-001:无法执行:命令=cmd.exe/X/C"C:\Program Files\Android\Android-sdk\platform tools\aapt.exe"package-m-J D:\projects\myapp\target\生成的源\r-mD: \projects\myapp\AndroidManifest.xml-SD: \projects\myapp\target\已筛选资产--自动添加覆盖-AD: \projects\myapp\assets-I"C:\ProgramFiles\Android\Android-sdk\platforms\Android-8\Android.jar",结果=1->[Help 1][ERROR][ERROR]要查看错误,请使用-e开关重新运行Maven。[错误]使用重新运行Maven-X开关以启用完全调试日志记录。[ERROR][ERROR]了解更多信息有关错误和可能的解决方案的信息,请阅读以下文章:[ERROR][Help 1]http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
当我在POM中注释<resourceDirectory>${project.build.directory}/filtered-assets</resourceDirectory>
时,构建完成成功,但它不使用筛选的assests。
此外,我尝试在命令行中执行这个cmd.exe /X /C ""C:Program FilesAndroidandroid-sdkplatform-toolsaapt.exe" package -m -J D:projectsmyapptargetgenerated-sourcesr -M D:projectsmyappAndroidManifest.xml -S D:projectsmyapptargetfiltered-assets --auto-add-overlay -A D:projectsmyappassets -I "C:Program FilesAndroidandroid-sdkplatformsandroid-8android.jar""
,并得到这个错误:invalid resource directory name: D:projectsmyapptargetfiltered-assets/application.properties
。错误的斜线,而不是反斜线后过滤的资产。将<resourceDirectory>${project.build.directory}/filtered-assets</resourceDirectory>
更改为<resourceDirectory>${project.build.directory}filtered-assets</resourceDirectory>
也没有帮助。
附言:我的插件部分看起来像这个
<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>
现在它工作了,我将<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>
如果在pom中指定属性,则不应该在单独的文件中声明它。相反,如果您想在单独的文件中指定它,则不应该在POM中声明它。
然而,我不确定您到底想做什么——maven属性只在pom文件中有效,并且在构建过程中使用。您无法从应用程序中访问它们。Android/java不知道它们的存在,也看不到它们,所以你在代码中看到${helloFromPOM}
而不是MY PROPERTY
的原因是Android/java只是在显示文本——它不知道它代表一个属性,也无法将其解析为变量或其他值。
有时是由不合适的平台版本引起的。试着在你的pom文件中设置正确。寻找类似的smthg
<sdk>
<platform>17</platform>
</sdk>