使用Android Studio/IntelliJ时,为什么我的pom没有正确执行?

Jos*_*arl 7 android intellij-idea maven android-manifest android-studio

我有一个用Maven构建的Android应用程序.使用buildnumber-maven-plugin和maven-resources-plugin我将maven项目版本和git commit hash插入AndroidManifest.该createbuildnumber - Maven的插件的目标,在运行validate阶段和resourcesMaven的资源,插件的目标,在运行initialize阶段.

通过命令行(with mvn install)构建时,所有工作正常,并且构建号在生成的清单中正确显示.

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

为什么?



仅供参考:Android Studio在Make之前运行Maven阶段流程资源,因此它应该可以运行.

在命令行上我使用的是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>
Run Code Online (Sandbox Code Playgroud)

我的AndroidManifest文件中的versionName是:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.somecompany"
    android:versionCode="1"
    android:versionName="${project.version}-${buildNumber}" >
Run Code Online (Sandbox Code Playgroud)

在命令行中,$ {project.version}和$ {buildNumber}都正确填充了它们的值,来自IntelliJ $ {buildNumber}不是,只是显示为"$ {buildNumber}":这表示(因为我已设置revisionOnScmFailure)该插件根本无法运行.

我已经尝试将create目标改为在initialize阶段中运行(如果IntelliJ正在跳过validate),但这没有任何区别.

Rob*_*unt 8

IntelliJ拥有自己的内部构建系统,就像任何其他IDE一样,并且能够在没有外部工具的帮助下构建项目.Intellij还通过解释项目中的pom.xml并根据您定义的配置构建pom.xml来与Maven集成.这对于大多数编译任务都很有效,但是当你引入更复杂的插件(例如buildnumber-maven-plugin)时,它会开始坍塌.不幸的是,IntelliJ没有内部等效来处理这个插件所以永远不会填充$ {buildNumber}属性.

可能的解决方法是:

  1. 不要使用IntelliJ的内置系统构建项目,使用"Maven Projects"面板,您可以通过转到"View">"Tool Windows">"Maven Projects"来显示该面板.这使您可以访问所有标准Maven阶段和其他功能.

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

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

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