gpt4 book ai didi

android-maven-plugin 和资源过滤

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:06:17 25 4
gpt4 key购买 nike

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

myFilteredProperty=${helloFromPOM}

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

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

打开 Assets 目录过滤

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

并按照 samples 配置资源插件对于 android-maven-plugin

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

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

在我由 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] 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

当我评论<resourceDirectory>${project.build.directory}/filtered-assets</resourceDirectory>在 POM 中,构建完成成功,但它不使用过滤 Assets 。

我也尝试执行此 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 .在 filtered-assets 之后错误的斜杠而不是反斜杠。改变 <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>

关于android-maven-plugin 和资源过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10900017/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com