Monday 27 March 2017

Creating a simple Maven Project

When I was first introduced to maven, I was introduced as a build tool, an alternative to ant build which I was using for my SoapUI project builds. However, once I dwell deeper into maven and built my interest because of the wide range of features supported by Maven, only then I realized that maven is much more than just a build tool. It is a complete project management tool which allows the developers to have synergy in the project structure. It provides a defined structure to the project which allows developers to easily understand and help in case there is any transition on the project. The new developers who are familiar with maven structure can easily determine the class file, resources etc on the project.

In this post I am going to start with creating a simple maven project and then in later part I will explain how you can create a complex project in maven with multiple modules.

You can create a simple project with below command (here I am assuming that you already have configured JDK and Maven).
mvn archetype:generate -DgroupId=myProject.myCompany.com
                       -DartifactId=myProject
                       -DarchetypeArtifactId=maven-archetype-quickstart
                       -DinteractiveMode=false
 the above command will create a maven project structure where you will have
  • pom.xml : This is the main and single configuration file that contains majority of the information used by maven to build the project. 
  • Directory structure as :
    • src -
      • main - 
        • java - 
          • myProject -
            • myCompany -
              • com -
                • App.java : It is the class file
      • test -
        • java - 
          • myProject -
            • myCompany -
              • com -
                • App.java : It is the JUnit test class file for testing the class created by maven automatically.
To run this maven project file you need to run a simple command as below :
mvn install
Above command will compile your source code, perform test and then create a jar package in the directory as below :
  • target
    • myProject-1.0-SNAPSHOT.jar
    • classes
      • myProject
        • myCompany
          • com
            • App.class
    • maven-archiver
      • pom.properties
    • maven-status
      • maven-compiler-plugin
        • compile
          • default-compile
            • createdFiles.lst
            • inputFiles.lst
        • testCompile 
          • default-testCompile
            • createdFiles.lst
            • inputFiles.lst
    • surefire-reports
      • myProject.myCompany.com.AppTest.txt
      • TEST-myProject.myCompany.com.AppTest.xml
    • test-classes
      • myProject
        • myCompany
          • com
            • AppTest.class

As you can see that maven has compiled and packaged the project into a jar file (if it is web project then a war will be generated). You can run your package with following command from your project directory
java -cp target/myProject-1.0-SNAPSHOT.jar myProject.myCompany.com.App
Hello world!
So, as you can see in the above example that it is very easy to create a maven project. Now we look into the pom.xml file that has been generated by maven




Following element in the pom file together are known as co-ordinates of the projct
  • groupId
  • artifactId
  • packaging
  • version
Creating a complex project with multiple child elements or modules is even easier. Use the same command as above to create the maven master project. Now go to the project directory and remove the "src" directory created inside and edit the "pom.xml" and change the value of element "packaging" to "pom"
<packaging>pom</packaging>
Now your master project is almost ready and you can create the modules inside using the similar command as you used for creating the master project file.
mvn archetype:generate -DgroupId=myProject.myCompany.com
                       -DartifactId=module1
                       -DarchetypeArtifactId=maven-archetype-quickstart
                       -DinteractiveMode=false
mvn archetype:generate -DgroupId=myProject.myCompany.com
                       -DartifactId=module2
                       -DarchetypeArtifactId=maven-archetype-quickstart
                       -DinteractiveMode=false
The above two commands will create two modules or child projects within the master project and you will realize that the pom file inside the master project is updated to have below elements

And the child project will have reference to the master project as below



For a complex project you only need to compile the master project and child projects will be launched on their own.
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] myProject .......................................... SUCCESS [  0.757 s]
[INFO] module1 ............................................ SUCCESS [  5.211 s]
[INFO] module2 ............................................ SUCCESS [  0.748 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.909 s
[INFO] Finished at: 2017-03-26T19:26:54+05:30
[INFO] Final Memory: 15M/37M
[INFO] -----------------------------------------------------------------------

Thursday 23 March 2017

Maven : Compilation of Groovy Class files

There are various ways to build and use your groovy class files, you can use Gradle, or Goovyc Ant or using maven. In maven we have two main possibilities, one with GMaven and the other with Groovy eclipse.

I will elaborate an example with GMaven. GMaven Plugin definition requires the groovy dependency (groovy-all).

Below is an example of a pom file which compiles all the groovy class files located in a specific location.



In this example, my scripts are making connection to an oracle 12g database, hence I had to add dependency for ojdbc and since the DB is in 12G I was facing further errors related to xdb.war. So I downloaded the war file and installed it on maven using command below.

mvn install:install-file -Dfile=xdb6.jar -DgroupId=com.oracle -DartifactId=xmlparserv2 -Dversion=12.1.0.2 -Dpackaging=jar

After installing the war to my maven repository and added the same to dependencies as shown in example above.

Now I can use the above maven pom in my master file and other child pom build can use the classes compiled from it.

Dependency Error : SoapUI 5.2.1

In our project we observed a strange error because of which the maven build was failing. Further analysis led to a conclusion that the problem was specific to the version of Soap-UI plugin we were using in our project.

Here below is the error that we observed while executing the build


There are two possible ways to manage this error, one is to degrade the plugin version for Soap-UI. We saw that this error did not exist with Soap-UI plugin 4.6.? however with 5.?.? we have the problem.

Another way to fix the problem was to add "com.jgoodies:forms:1.0.7"  dependency to your pom file.


Tuesday 7 March 2017

Maven : Integration of tests

In the changing environment, the way tests are executed are also changing. Once we moved to agile mode from a traditional waterfall or v-Cycle mode of development we saw the need of automation. However, there is always a next step to be done, and in this case the obvious next step is to integrate them with development builds. There could be various ways to achive the same among which the most common one is deploying your tests on Jenkins. It provides a common platform where the development builds are done and at the same time the test builds can be done. In this article we will explore the possibility to integrate the various automated tests.

Maven : Integration of SoapUI automated Tests

SoapUI includes a Maven plugin for running SoapUI tests and mocks during Maven build. To use the correct version of SoapUI, you will have to see under the Soap Maven Repository. Maven should know about the soap repository and dependencies related to your project and it can be achieved by updating the maven settings which would be available at "conf" directory under maven binary location.
Edit the settings.xml and update the below elements as needed.
  • Local Repository : Location of the directory where dependency repository would be stored locally for compilation
  • Mirror : Location of maven mirror repository.
  • Repository : maven repositories and other repository where dependent tools and configuration can be found

  • PluginRepository : maven repositories and other repository where dependency plugins can be found
Attached an example settings.xml which is being used for one of the project.



Once the settings are configured your maven can execute soapUI tests.
The second step is to create pom files for the execution of the SoapUI tests. You may have two scenario here, one where you only have one project file and the other where you have multiple project files. For single project file based execution it is easier as you have to create only one pom file which can be located at the same place as the SoapUI project file. For multiple project file however, we need to organize our project to be in the way below :
  • Soap-Project
    • Project-1
      • Soap-Project-file.xml (Soap UI project file)
      • pom.xml (Child pom file configured for this project)
    • Project-2
      • Soap-Project-file.xml (Soap UI project file)
      • pom.xml (Child pom file configured for this project)
    • Project-n
      • Soap-Project-file.xml (Soap UI project file)
      • pom.xml (Child pom file configured for this project)
    • pom.xml (master pom file)
Prefer to use names of the files or directories without spaces or special characters to avoid referencing issues.

The master pom file would look as below :


A typical child pom file would look as below :


You can configure additional details for the project execution by further addition following parameters
  • projectFile : Specified the name of the SoapUI project file to use, default value is ${pom.artifactId}-soapui-project.xml
  • testSuite : Specifies the name of the TestSuite to run
  • testCase : Specifies the name of the TestCase to run
  • endpoint : Overrides the service endpoint to be invoked by any TestRequests
  • host : Overrides the target host:port to be invoked by any TestRequests
  • username : Overrides the username used by any TestRequests run
  • password : Overrides the password used by any TestRequests run
  • domain : Overrides the domain used by any TestRequests run
  • printReport : Controls if a small test report should be printed to the console (true/false)
  • outputFolder : Set which folder results/reports are saved to
  • junitReport : Turns on creation of JUnit-reports, (true/false)
  • exportAll : Controls if all test requests should be exported (default only exports errors), (true/false)
  • settingsFile : Specifies SoapUI settings file to use
  • wssPasswordType : Specifies WSS password type
  • project.password : Specifies password for encrypted project
  • settingsFile.password : Specifies password for encrypted settings file
  • globalProperties : Sets global properties
  • projectProperties : Sets project properties
  • saveAfterRun : Saves project file after run
  • testFailIgnore : Ignore failed tests.
  • reportFormat : Sets formats for created report. Pro only.
  • reportName : Sets which report to create. Pro only.
  • coverage : Sets the output to include Coverage HTML reports. Pro only.
  • environment : Sets the active environment. Pro only.
You will have to replicate the above configuration for all the child projects. Child project names in the pom, the directory name and the name mentioned under the master pom file should be same. Once the pom configuration is done you can test your execution with below command.
 > mvn -U clean install
In case of integration of mock service you will have to do some minor modification in the phase and goal of the execution section. The execution should appear as below :




 You can also define the following properties in order to customize your mock service on run time
  • projectFile : Specified the name of the SoapUI project file to use, default value is ${pom.artifactId}-soapui-project.xml
  • mockService : Specified the MockService to run
  • port : The local port to listen on, overrides the port configured for the MockService
  • path : The local path to listen on, overrides the path configured for the MockService
  • noBlock : Turns off blocking when MockRunner has started
  • settingsFile : Specifies SoapUI settings file to use, will contain path to integrated tool to run
  • project.password : Specifies password for encrypted project file
  • settingsFile.password : Specifies password for encrypted soapui settings file
  • saveAfterRun : Saves project file after run.
For more details following links can be referenced