Nemin
Nemin

Reputation: 2027

Executing cmd command using Maven

I want to execute this command:

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

Using Maven. I have to execute this command in the Android SDK/ tools directory. Can anyone help me on how to do this? I think this can be done using batch file but I am not sure how to create a batch file. I need this command to be executed when I type "mvn clean install". Thanks

Upvotes: 2

Views: 8758

Answers (2)

acdcjunior
acdcjunior

Reputation: 135762

Executing commands using Maven

You can use the Maven Exec Plugin bound to the install phase.

In the snippet below, the commant ping with the argument 8.8.8.8 will be executed every time you do a 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>

 
And that's it.

 

 


Update:

I see now what you really having trouble with is executing zipalign, not an arbitrary command. For that, there are two ways.

Using the built-in zipalign of the maven-android-plugin

As of release 2.5.0 of the Android Maven Plugin the zipalign goal is part of the plugin. To activate simply add the goal zipalign as an execution (e.g. to the package phase) and set the skip parameter in the plugin configuration to false:

<zipalign><skip>false</skip></zipalign>

Full <plugin> tag 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>

Sources:

Zipalign using Exec-Maven Plugin

The code below will bind the functionality of zip aligning to the package phase, overwriting previous zip aligned file without asking.

<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>

Please note that this profile has to be added to the pom for the actual APK. You can't add it to a parent pom. The reason for this is that it uses the Maven property that defines the artefact name (project.build.finalName).

Upvotes: 4

GreyBeardedGeek
GreyBeardedGeek

Reputation: 30088

If you're building an Android project with Maven, you should be using the maven-android-plugn, which supports the zipalign tool directly.

Upvotes: 1

Related Questions