使用Maven执行cmd命令

时间:2013-05-22 00:15:26

标签: maven batch-file

我想执行此命令:

zipalign [-f] [-v] <alignment> infile.apk outfile.apk

使用Maven。我必须在Android SDK / tools目录中执行此命令。任何人都可以帮我解决这个问题吗?我认为这可以使用批处理文件完成,但我不知道如何创建批处理文件。我输入“mvn clean install”时需要执行此命令。感谢

2 个答案:

答案 0 :(得分:3)

使用Maven

执行命令

您可以使用绑定到install阶段的Maven Exec Plugin

在下面的代码段中,每次执行ping时都会执行带有参数8.8.8.8的commant mvn install

<project>
...
<build>
    <plugins>
        <plugin>
            <artifactId>exec-maven-plugin</artifactId>
            <groupId>org.codehaus.mojo</groupId>
            <version>1.2.1</version>
            <executions>
                <execution>
                    <id>My Command Runner</id>
                    <phase>install</phase>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                    <configuration>
                        <executable>ping</executable>
                        <arguments>
                            <argument>8.8.8.8</argument>
                        </arguments>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
...
</project>


就是这样。


更新

我现在看到你真正遇到的问题是执行 zipalign ,而不是任意命令。为此,有两种方式。

使用maven-android-plugin

的内置zipalign

从Android Maven插件的2.5.0版本开始,zipalign目标是该插件的一部分。要激活,只需将目标zipalign添加为执行(例如,添加到package阶段),并将skip plugin中的configuration参数设置为{{1 }}:

false

完整<zipalign><skip>false</skip></zipalign> 代码示例:

<plugin>

来源:

  • Here you'll find an example使用旧版本(仍然称为<plugin> <groupId>com.jayway.maven.plugins.android.generation2</groupId> <artifactId>maven-android-plugin</artifactId> <inherited>true</inherited> <configuration> <sign> <debug>false</debug> </sign> <zipalign> <verbose>true</verbose> <inputApk>${project.build.directory}/${project.artifactId}.apk</inputApk> <outputApk>${project.build.directory}/${project.artifactId}-signed-aligned.apk </outputApk> </zipalign> </configuration> <executions> <execution> <id>alignApk</id> <phase>package</phase> <goals> <goal>zipalign</goal> </goals> </execution> </executions> </plugin> );
  • ZipalignAPKBuiltByMAven:介绍如何自动压缩由Maven构建的APK。
  • Simpligility:带有zipalign和改进验证的Maven Android插件
  • 另一个例子:A full pom.xml that uses the zipalign

使用Exec-Maven插件Zipalign

下面的代码将zip对齐功能绑定到android-maven-plugin阶段,覆盖以前的zip对齐文件而不会询问。

package

请注意,此配置文件必须添加到实际APK的pom中。您无法将其添加到父pom。原因是它使用Maven属性来定义人工制品名称(<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.1.1</version> <executions> <execution> <id>zipalign</id> <goals> <goal>exec</goal> </goals> <phase>package</phase> <configuration> <executable>${ANDROID_HOME}/tools/zipalign</executable> <arguments> <argument>-f</argument> <argument>4</argument> <argument>target/${project.build.finalName}.apk</argument> <argument>target/${project.build.finalName}-zipped.apk</argument> </arguments> </configuration> </execution> </executions> </plugin> )。

答案 1 :(得分:1)

如果您正在使用Maven构建Android项目,则应该使用maven-android-plugn,它直接支持zipalign工具。