Thursday 9 February 2017

DevOps: 3 ways to debug maven build

DevOps: 3 ways to debug maven build: Maven build debug Maven is automated build project management tool and  automates  the build process by defining the all the dependencies...

3 ways to debug maven build

Maven build debug

Maven is automated build project management tool and automates the build process by defining the all the dependencies in pom.xml.Developers encounters lot of issues while building maven project in development phase as well as release phase.we need to know the ways to debug any issues with maven build.I am listing down the different ways to debug the maven build issues.

There are number of ways to debug an maven project.
I am listing down the required steps for findout the real issues.

1. maven debug mode:-
if you are experience with running maven goals like compile or install, mostly informative message will bedisplayed. if any failure happens, These messages are not helpful to find out the exact cause.
In such case, try to run maven with -X flag to give the clear message of where the error is occured and also plugin configuration and detailed class loading information which is very helpful for the coders to find the real cause of an issue. This is perfect suitable debug option for maven plugin coders.
mvn -X  prints debugging messages
 mvn -e shows error messages
Please remember that this is debugging information for what maven do while building your project. This is not for your project debugging. i.e different concept. i will write one more post about the java project debug tips later posts.

This option will start the maven in debug mode. It will give the complete information messages like what are the different dependencies that are loading to your project and what goals are running.

2.dependencies issues in maven:-

In a real time, for maven project build,we have seen that our project has direct dependencies as well as indirect dependencies.

suppose in your project, you have direct dependency on one module say log4j-1.2.12 and you have other indirect dependency of log4j-1.2.12(coming from spring 2.0.6 which has dependency for your project). This would be the case of your project having two same modules(log4j) with different versions. This would be an class incompatible issue once your project is started in webserver. Then you have to keep the required module in your pom.xml and ignoring other module by specifying exclusion configuration.

To find out the dependency tree for your project, you have to use the below command
mvn dependency:tree
This list out all the dependencies for your project in tree explorer format.all these dependencies are downloaded to your local repository.

3.Debugging tests with maven surefire:-

we will use mvn test for running test cases of your maven project. As you know maven surefire plugin is used to run with your unit test of your project.


you have to provide the maven.surefire.debug property with maven test goal
mvn -Dmaven.surefire.debug test

This will run the tests in separate process.This will listen on port 5005. You can attach this process to eclipse for remote debug.
In the eclipse you have to create and configure the maven debug options as follows.
hostname is localhost
and port  is 5005

This is one of coolest feature provided by maven with eclipse remote debugging.
whenever we started the maven test case execution, you can put break points for stopping the debugger in your test classes for debugging purpose. Once the maven execution is over, eclipse debugger terminate the process.


and other way is to not to run the debugger in fork mode meaning is to execute the tests in the same process, we can use

mvn -DforkMode never test

Maven dependency scopes:

Maven provides four dependency scopes:
  • compile: A compile-scope dependency is available in all phases. This is the default value.
  • provided: A provided dependency is used to compile the application, but will not be deployed. You would use this scope when you expect the JDK or application server to provide the JAR. The servlet APIs are a good example.
  • runtime: Runtime-scope dependencies are not needed for compilation, only for execution, such as JDBC (Java Database Connectivity) drivers.
  • test: Test-scope dependencies are needed only to compile and run tests (JUnit, for example).

Wednesday 8 February 2017

Common Jenkins failures


Skip to end of metadata

Summary

This page lists typical non-code related failures we got on jenkins, tries to explain them and should hence help to fix them.
Maven Dependency plugin
example
[INFO] Searching repository for plugin with prefix: 'dependency'.
[INFO] org.sonatype.plugins: checking for updates from central
[INFO] org.apache.maven.plugins: checking for updates from central
[INFO] org.codehaus.mojo: checking for updates from central
[INFO] artifact org.codehaus.mojo:dependency-maven-plugin: checking for updates from central
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Required goal not found: dependency:analyze in org.codehaus.mojo:dependency-maven-plugin:1.0
There's two dependency plugins - For some reason, Maven (or Hudson's integration thereof) is looking for the old mojo one. In case the new Apache one is (temporarily) not available, mvn will pick the old one but this has no goal analyze.
Solution: redefine your job's goal - instead of dependency:analyze, use the more specific org.apache.maven.plugins:maven-dependency-plugin:analyze.
Re-triggering the build normally fixes that problem.

Missing Artifact

example
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Failed to resolve artifact.
 
Missing:
----------
1) info.magnolia:magnolia-module-standard-templating-kit:jar:2.0.7-SNAPSHOT
 
  Try downloading the file manually from the project website.
 
  Then, install it using the command:
      mvn install:install-file -DgroupId=info.magnolia -DartifactId=magnolia-module-standard-templating-kit -Dversion=2.0.7-20121025.150418-11 -Dpackaging=jar -Dfile=/path/to/file
 
  Alternatively, if you host your own repository you can deploy the file there:
      mvn deploy:deploy-file -DgroupId=info.magnolia -DartifactId=magnolia-module-standard-templating-kit -Dversion=2.0.7-20121025.150418-11 -Dpackaging=jar -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id]
 
  Path to dependency:
    1) info.magnolia:magnolia-module-commenting:jar:1.2.3-SNAPSHOT
    2) info.magnolia:magnolia-module-standard-templating-kit:jar:2.0.7-SNAPSHOT
 
----------
1 required artifact is missing.
So obviously there's an artifact missing, but why?
Potential reasons:
  • there's a typo -> fix it
  • that version of the referenced module had never been build and hence it was never deployed to nexus -> either build that other module or get rid of the dependency to it
  • the artifact has been removed from the cache (happens every few months) -> rebuild that version of that module to get it re-installed

... isn't a file

log fragment
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Failed to create assembly: Error adding file 'info.magnolia:magnolia-module-standard-templating-kit:jar:2.5-SNAPSHOT' to archive: /usr/local/hudson/maven-local-repo/info/magnolia/magnolia-module-standard-templating-kit/2.5-SNAPSHOT/magnolia-module-standard-templating-kit-2.5-SNAPSHOT.jar isn't a file.
  • Looks like this happens, when a module is build but one of its dependencies is built at the same time (so its jar is already 'deleted') -> waiting until the dependency build had finished, then re-triggering the module build fixed it

Clover Version conflict

So if you have something like
log fragment
[CLOVER] FATAL ERROR: Clover could not be initialised because of an unexpected error.
(class java.lang.NoSuchMethodError:com_cenqua_clover.CloverVersionInfo.
An_old_version_of_clover_is_on_your_compilation_classpath___Please_remove___Required_version_is___3_1_7()V)
{code}
in your log, you should try to align parent pom versions of the involved modules.
Here's a recent example of a full log.
Potential reasons:
  • Module "a" is making use of module "b" but that one was instrumented with older clover version because both use different versions of parent pom's and clover versions has changed in between

Invalid Java version


log fragment
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Compilation failure
Failure executing javac, but could not parse the error:
javac: invalid target release: 1.6
Usage: javac <options> <source files>
Reason: 
  • Each hudson job has a jdk to be used - if this is e.g. set to 1.5 but the build would actually require 1.6 you'll see the above error -> get the job properly configured

Tuesday 7 February 2017

Automation of WAR/EAR deployment using Jenkins

Application Deployment has always been on of the most tedious tasks in project development life cycle. This involves steps  like checking out the code from the Code Repository, building it using Maven/Ant and then deploying it to the remote servers. The process is more time/effort taking in case we have multiple servers like Staging,UAT,production.
Jenkins is an open source tool that helps us to automate and centralize the process of deployment.
In this post i am going to explain how to deploy WAR to Apache Tomcat using SVN as code repository and Maven as build WAR tool.
Step 1 : Configure Jenkins
Jenkins comes bundled as WAR file so you need to deploy it to an Application Server. I used Apache Tomcat version 7.0.32. Below are the steps.
1.1. Start the tomcat
1.2. Deploy the jenkins.war Download location for jenkins.war
Note : Some server restrict the size of war file to be uploaded and you get the common error while deploying it from the Tomcat UI Manager. Below is how i resolved it.
a. Open the file $CATELINA_HOME\webapps\manager\WEB-INF web.xml
b. Look out for the below entry, Size 61316110 worked just fine for me
<multipart-config>
<!– 50MB max –>
<max-file-size>61316110</max-file-size>
<max-request-size>61316110</max-request-size>
<file-size-threshold>0</file-size-threshold>
</multipart-config>
c. Restart Tomcat
1.3.  Go to the URL http://locahost:8080/jenkins ( Replace local host with the IP of your server )
1.4.  Add Deploy to container Plugin this can be done using the Plugin Manager feature of Jenkins. This plugin will be used for deployment of the war that we will build using Jenkins.
1.5.  Provide Maven Location to Jenkins – Jenkins interacts with Maven to build code that requires maven. Follow the below steps.
a. Download Maven Link for download
b. Place it on the server and copy its path
c  Go to URL http://localhost:8080/jenkins/configure ( Replace localhost with the IP of your server )
d. Provide name ( Any random name ) and location of your maven.

Step 2 : Configure Tomcat for build deployment
We need to configure the server where we will be deploying our build ( This server can be same or different then the one where Jenkins is running ). This involves creating a user on tomcat and assigning him with manager-script role.
Just add the below lines under the node <tomcat-users></tomcat-users> to tomcat-users.xml file  and restart the server. This file is located in the conf folder of tomcat.
<role rolename=”manager”/>
  <role rolename=”admin”/>
  <user username=”harpreet” password=”harpreet” roles=”standard,manager,admin,manager-gui,manager-script” />
Step 3  –  Create a Job on Jenkins
3.1  Click On “New Job”
3.2 Enter the name for the Job
3.3 Select Build a maven2/3 project

3.4  Look out for section Source Code Management, select Subversion and provide the SVN url of project you will be prompted for your SVN credentials.
3.5   Look out for Build section Provide Maven build parameters e g . clean package.

3.6 : Add a post build action that will deployed the war file to remote tomcat server.  Follow the below steps
a . Click on button “Add post build action”
b. Select the option ” Deploy war/ear to a container”
C . Enter war file name and path
Important :  For every job Jenkins create a folder named as workspace in the jobs folder, this path needs to relative to workspace. Upon successful build war is copied in this folder, and deploy plugin picks it from here. If the war is not in this folder, or you failed to provide the correct path.War will not be deployed to the server.
d Enter the server detail and credential of the user that we created on the tomcat in above steps

3.7 : Save the job and hit “Build Now”,  Check the logs by clicking the ball icons on the left side of the screen.

There will be log traces like you have for Maven, when you build it using eclipse or command line args.
And few jenkins log signifying the deployment like below :
Deploying /home/gagan/.jenkins/jobs/TalentHuntV2/workspace/target/TalentHunt-1.0.war to container Tomcat 7.x Remote
3.8 :  Verifying you did it correct.In case you did everything right, you can test the deployment of your project on the servers.
Step 4 ( Optional ) – Enable Auto Build
You can also configure Jenkins to build your code automatically when anyone commits the code.
4.1 Look out for section “Build Trigger” section.
4.2 Select the event that you want for build trigger. I chose that Jenkins should poll SVN for an updates. If update in code is found it will trigger the build.
For this we just need to select option”Poll SCM” and enter appropriate time, i used pattern */5 * * * * .

Important : For configuration of project that use artifacts from other project please do read my next blog. Building Artifacts Using Jenkins
Jenkins can help you automate a number of other processes/tasks. WAR/EAR development is the one you can start, once you are into Jenkins am sure you will be tempted to explore it further.