【问题标题】:Why isn't my pom being executed correctly when using Android Studio/IntelliJ?为什么在使用 Android Studio/IntelliJ 时我的 pom 没有正确执行?
【发布时间】:2013-06-22 12:42:24
【问题描述】:

我有一个使用 Maven 构建的 Android 应用程序。 使用 buildnumber-maven-plugin 和 maven-resources-plugin 我将 maven 项目版本和 git commit hash 插入到 AndroidManifest 中。 buildnumber-maven-plugin 的create 目标在validate 阶段运行,maven-resources-plugin 的resources 目标在initialize 阶段运行。

通过命令行(使用mvn install)构建时,一切正常,并且构建号正确显示在生成的清单中。

但是,当通过 Android Studio 或 IntelliJ 构建时,清单中不存在 git commit hash(Maven 属性未替换为实际值),但 maven 项目版本存在。

为什么?



仅供参考:Android Studio 在 Make 之前运行 Maven 阶段 process-resources,因此它应该可以工作。

在命令行上,我使用的是 Maven 3.0.3,因此可能是版本问题(尽管我无法找出 IntelliJ 使用的版本)。

这是我的 POM 的构建元素:

<build>
    <sourceDirectory>src</sourceDirectory>
    <testSourceDirectory>test</testSourceDirectory>

    <resources>
        <resource>
            <directory>${project.basedir}</directory>
            <filtering>true</filtering>
            <targetPath>${project.build.directory}/filtered-manifest</targetPath>
            <includes>
                <include>AndroidManifest.xml</include>
            </includes>
        </resource>
    </resources>

    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>buildnumber-maven-plugin</artifactId>
            <version>${maven.buildnumber.version}</version>
            <configuration>
                <doCheck>false</doCheck>
                <doUpdate>false</doUpdate>
                <shortRevisionLength>6</shortRevisionLength>
                <revisionOnScmFailure>000000</revisionOnScmFailure>
            </configuration>
            <executions>
                <execution>
                    <phase>validate</phase>
                    <goals>
                        <goal>create</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>${maven.resources.version}</version>
            <executions>
                <execution>
                    <phase>initialize</phase>
                    <goals>
                        <goal>resources</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>com.jayway.maven.plugins.android.generation2</groupId>
            <artifactId>android-maven-plugin</artifactId>
            <extensions>true</extensions>
            <configuration>
                <androidManifestFile>${project.build.directory}/filtered-manifest/AndroidManifest.xml</androidManifestFile>
            </configuration>
        </plugin>
    </plugins>
</build>

我的 AndroidManifest 文件中的 versionName 是:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.somecompany"
    android:versionCode="1"
    android:versionName="${project.version}-${buildNumber}" >

从命令行 ${project.version} 和 ${buildNumber} 都正确填写了它们的值,从 IntelliJ ${buildNumber} 不是并且只是显示为“${buildNumber}”:这表明(因为我已设置revisionOnScmFailure) 插件根本无法运行。

我已尝试将 create 目标更改为在 initialize 阶段运行(以防 IntelliJ 跳过 validate),但这没有任何区别。

【问题讨论】:

  • 这个问题有进展吗?
  • 遗憾的是我还没有;作为解决方法,您可以使用 IntelliJ 的 maven“安装”任务而不是“制作”

标签: android maven intellij-idea android-manifest android-studio


【解决方案1】:

我遇到了同样的问题。我所做的是:

  • 转到 Maven 工具窗口
  • 转到使用 buildnumber-maven-plugin 的项目
  • 展开Plugins
  • 查找并扩展 buildnumber 插件
  • 您会在下面找到 buildnumber:create item - 右键单击​​它并选择 Execute After Build

对我来说就像一个魅力。

【讨论】:

    【解决方案2】:

    IntelliJ 拥有自己的内部构建系统,与任何其他 IDE 非常相似,并且能够在没有外部工具帮助的情况下构建项目。 Intellij 还通过解释项目中的 pom.xml 并指导它根据您定义的配置进行构建,从而与 Maven 集成。这适用于大多数编译任务,但是当您引入更复杂的插件(例如 buildnumber-maven-plugin)时就会开始崩溃。不幸的是,IntelliJ 没有内部等价物来处理这个插件,所以 ${buildNumber} 属性永远不会被填充。

    可能的解决方法是:

    1. 不要使用 IntelliJ 的内置系统构建您的项目,使用“Maven 项目”面板,您可以通过转到“查看”>“工具窗口”>“Maven 项目”来显示该面板。这使您可以访问所有标准 Maven 阶段和其他功能。

    2. 在您的 IntelliJ“运行配置”中添加一个名为“buildNumber”的环境变量,并为其指定任何您喜欢的值,例如:buildNumber=DEV。这将使 buildNumber 属性在构建过程中可用并填充该属性,但它不会从您的 SCM 更新。

    我们在多模块 maven 项目中使用第一个解决方法,因为我们也遇到了 buildnumber-maven-plugin 的类似限制。当我们需要在 IntelliJ 中运行集成测试时,我们也会使用解决方案 2,因为我们的代码需要 buildNumber 属性来显示版本信息,只要我们给它任何值就可以了。

    我希望这对您有所帮助,唯一真正的解决方案是让 IntelliJ 的内部构建系统对 buildnumber-maven-plugin 有一些了解,并在构建过程中向环境公开正确的属性。

    【讨论】:

    • 但它不只是导入 POM,它还应该运行 Maven 阶段进程资源,这应该会导致 buildnumber插件正在运行。感谢您的回答。就我而言,在我们的案例中 1. 是不可接受的,因为很少有开发人员会忍受构建时间的增加,但 2. 可能会。我会坚持将此标记为正确,以便为任何其他答案留出一些时间。
    • 过去我发现很难找出 IntelliJ 的编译器在解释 pom.xml 时实际在做什么,那里似乎没有很多信息。我的理解是 IntelliJ 在“进程资源”阶段(或任何阶段)不涉及 maven,但有自己的“进程资源”实现,通过读取 pom.xml 进行配置
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多