executing shell script and ant script with named parameter using maven - maven

I am implementing maven wrapper to ant builds. And the ant command used to build the project is as follows
ant -v -f build.xml -Darch=linux-java7 -Dconfig=/work/build.config -Doutput=/work/bldout/
Now i have to execute the above command through maven . I tried to implement this using "I want to execute shell commands from maven's pom.xml" and "http://sanchitbahal.wordpress.com/2011/09/19/maven-exec-plugin-vs-maven-antrun-plugin-for-running-command-line-tool/"
The sample code i tried inside my pom.xml is as follows :
<plugin>
<artifactId>exec-maven-plugin</artifactId>
<groupId>org.codehaus.mojo</groupId>
<executions>
<execution>
<id>execute-shell</id>
<phase>compile</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>test.sh</executable>
<arguments>
<argument>ARG1</argument>
<argument>ARG2</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
But i am not able to figure out how to pass named parameter such as "-Darch=linux-java7" as arguement to build.xml
Also used maven-antrun plugin to invoke build.xml as follows :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>run-target</id>
<phase>install</phase>
<configuration>
<target>
<ant antfile="build.xml" target="all" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
But here also i am unable to figure out how to pass parameter such as "-Darch=linux-java7" as arguement to build.xml
What i know is that i can put the command in a shell script(in a .sh file) and call the shell script using maven-exec-plugin , but wanted to know if it is possible to do without doing so .

Use antrun, it has less external dependencies (like ant executable on path etc.)
Ant tasks executed by antrun plugin inherit all properties defined inside the properties section of your pom.
So you simple need to include:
<properties>
<arch>linux-java7</arch>
...
</properties>
inside your pom to make it work.

Related

Mule could not resolve placeholder with maven exec

I'm using maven exec plugin to run Mule 3. I have some properties that are correctly resolved when they are in my properties file, however if I try to provide them as a JVM option they do not get resolved/overridden. Below is my maven exec config:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>org.mule.MuleServer</mainClass>
<arguments>
<argument>-config</argument>
<argument>src/main/app/prioritisation-api.xml</argument>
<argument>-classpath</argument>
<argument>src/main/app</argument>
<argument>-Dmule3.http.root=http://localhost:8095</argument>
</arguments>
<classpathScope>compile</classpathScope>
</configuration>
</plugin>
I also tried using -M-Dmule3.http.root=http://localhost:8095 but that did not work.
The exec plugin has two goals java and exec. You are using the java which means:
(Excerpt from the docs):
exec:java Executes the supplied java class in the current VM with the
enclosing project's dependencies as classpath.
I would suggest to use the exec goals instead and start a separate JVM to which you can provided different parameters.

Maven-antrun No ant target defined - SKIPPED

i am trying to copy a file in my maven multi-module project via antrun plugin. the file is in root of parent project:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<inherited>false</inherited>
<executions>
<execution>
<inherited>false</inherited>
<id>copy</id>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target name="copy and rename file">
<copy file="${basedir}/portal-ext.properties" tofile="${liferay.auto.deploy.dir}/../portal-ext.properties" />
</target>
</configuration>
</execution>
</executions>
i run this via mvn antrun:run the problem is i get "No ant target defined - SKIPPED" on parent and on every module. i need it to run only on parent and thought <inherited>false</inherited> would help but i doesn't. But why "No ant target defined"?
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>ant-execute</id>
<configuration>
<target>
<echo message="plugin classpath: " />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
command : mvn antrun:run#ant-execute
antrun:run will only consider the plugin's configuration, not that for a specific execution, so the execution you specify is ignored. As Run a single Maven plugin execution? states, you can give your execution an id of default-cli to have it picked up.
However, the execution you configure should already be taking effect during the regular build lifecycle.
just run it like this: mvn antrun:run#copy

Running adb.exe command from maven 3.0.5

Is it possible to run adb.exe commands from maven. For example I want to run adb shell instrument -e classname#testcasename -w packagename/instrumenation. I need to run this command in maven is it possible?? Do i need to specify it in the pom.xml file or it can be run directly by specifying the command-line argument.
You can use the Maven Exec Plugin to execute cmd commands.
In the snippet below (add it to a pom.xml), the command 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>
In your case, the inner configuration would be something around:
<configuration>
<executable>adb </executable>
<arguments>
<argument>shell</argument>
<argument>instrument</argument>
<argument>-e</argument>
<argument>classname#testcasename</argument>
<argument>-w</argument>
<argument>packagename/instrumenation</argument>
</arguments>
</configuration>
Make sure you bind it to the phase you really need. The example above, as said, is bound to the mvn install - meaning the command will be executed when someone runs that (install) phase.

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
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:
Here you'll find an example using the an older version (still called android-maven-plugin);
ZipalignAPKBuiltByMAven: Describes how to automatically zipalign an APK that has been built by Maven.
Simpligility: Maven Android Plugin with zipalign and improved verification
Another example: A full pom.xml that uses the zipalign.
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).
If you're building an Android project with Maven, you should be using the maven-android-plugn, which supports the zipalign tool directly.

How to bind ant to a maven-plugin custom goal

The way to integrate maven with ant is to simply use the maven-antrun-plugin like
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>package</id>
<phase>package</phase>
<configuration>
<tasks>
<ant antfile="build.xml" target="package" />
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
in your project's pom.xml file
However I want to be able to have that associated build.xml script along with the execution configuration as part of a maven plugin that defines a custom packaging and a lifecycle.
Is there a way to do that?
I wrote an experiment that did this a little while ago. Have a look at the maven-plugin-in-ant branch of jslint4java.

Resources