Wednesday, August 19, 2009

Executing maven test with the ant run plugin--A Real experiance.

Few days back i got a unexpected result with using the maven ant run task with a patch i submitted for the unit tests for the SMS Transport(which is my GSoC project).So i thought it would be useful if i blog my experiance and how i solved that issue.

So i'll start with my xml segment in the pom.

<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-antrun-plugin</artifactid>
<executions>
<execution>
<id>build-repo</id>
<phase>test-compile</phase>
<configuration>
<tasks>
<mkdir dir="target/test-resources/samples/conf">
<mkdir dir="target/test-resources/samples/repository/modules">
<mkdir dir="target/test-resources/samples/repository/services">
<mkdir dir="target/test-resources/samples/repository/services/SampleService/org/apache/axis2/transport/sms">
<mkdir dir="target/test-resources/samples/repository/services/SampleService/META-INF">
<copy file="${settings.localRepository}/org/apache/axis2/addressing/${axis2.version}/addressing-${axis2.version}.mar" tofile="target/test-resources/samples/repository/modules/addressing.mar">
<copy file="target/test-classes/org/apache/axis2/transport/sms/SimpleInOutMessageReceiver.class" tofile="target/test-resources/samples/repository/services/SampleService/org/apache/axis2/transport/sms/SimpleInOutMessageReceiver.class">
<copy file="conf/axis2.xml" tofile="target/test-resources/samples/conf/axis2.xml">
<copy file="repository/services/SampleService/META-INF/MANIFEST.MF" tofile="target/test-resources/samples/repository/services/SampleService/META-INF/MANIFEST.MF">
<copy file="repository/services/SampleService/META-INF/services.xml" tofile="target/test-resources/samples/repository/services/SampleService/META-INF/services.xml">
</copy>
</copy>
<goals>
<goal>run</goal>
</goals>
</copy>
</copy>
</copy>

</mkdir></mkdir></mkdir></mkdir></mkdir></tasks></configuration></execution></executions></plugin>
As you see there is only some mkdir and copy tasks listed in the test-compile phase

So When I run the tests test where executed successfully(mvn clean install / mvn test)
And also when i skip the tests with mvn clean install -Dtest=false the build was successful.

The problem comes when i try to skip the test with mvn clean install -Dmaven.test.skip=true
when i do that follwing error appiers

[ERROR] BUILD ERROR
[INFO] ------------------------------
------------------------------------------
[INFO] An Ant BuildException has occured: Warning: Could not find file /home/charith/projects/transport/modules/sms/target/test-classes/org/apache/axis2/transport/sms/SimpleInOutMessageReceiver.class to copy.

So If you look in to the ant run plugin the copy tasks are listed in the "test-compile" phase so its seems correct to assume that those tasks will not execute when we skip the tests.And it seems that assumption is correct when we use the -Dtest=false.But that assumption fails with we run with -Dmaven.test.skip=true

The reason for this error is the .class file that the copy task trying to copy created after compiling the test sources.but when we use -Dmaven.test.skip=true to skip the tests it will execute the tesks that are listed in the test-compile phase .So it reports the Error.

to overcome that case i had to add the system variable check to the task.Now it will check for the maven.test.skip variable before executing the test.So if it is true it will not execute the tasks in the test-compile phase.Following this the modified pom which resolved the issue.




<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-antrun-plugin</artifactid>
<executions>
<execution>
<id>build-repo</id>
<phase>test-compile</phase>
<configuration>
<tasks unless="maven.test.skip">
<mkdir dir="target/test-resources/samples/conf">
<mkdir dir="target/test-resources/samples/repository/modules">
<mkdir dir="target/test-resources/samples/repository/services">
<mkdir dir="target/test-resources/samples/repository/services/SampleService/org/apache/axis2/transport/sms">
<mkdir dir="target/test-resources/samples/repository/services/SampleService/META-INF">
<copy file="${settings.localRepository}/org/apache/axis2/addressing/${axis2.version}/addressing-${axis2.version}.mar" tofile="target/test-resources/samples/repository/modules/addressing.mar">
<copy file="target/test-classes/org/apache/axis2/transport/sms/SimpleInOutMessageReceiver.class" tofile="target/test-resources/samples/repository/services/SampleService/org/apache/axis2/transport/sms/SimpleInOutMessageReceiver.class">
<copy file="conf/axis2.xml" tofile="target/test-resources/samples/conf/axis2.xml">
<copy file="repository/services/SampleService/META-INF/MANIFEST.MF" tofile="target/test-resources/samples/repository/services/SampleService/META-INF/MANIFEST.MF">
<copy file="repository/services/SampleService/META-INF/services.xml" tofile="target/test-resources/samples/repository/services/SampleService/META-INF/services.xml">
</copy>
</copy>
<goals>
<goal>run</goal>
</goals>
</copy>
</copy>
</copy>

</mkdir></mkdir></mkdir></mkdir></mkdir></tasks></configuration></execution></executions></plugin>
As you see the only change is tasks unless="maven.test.skip" in the task element which check !(maven.test.skip)

the follwing xml segment is logically equal to

if(!(maven.test.skip)) {
//execute task
}


<tasks unless="maven.test.skip">
After this system variable check I was successful with running the tests with -Dtest=false , -Dmaven.test.skip=true.

No comments: