Monday, October 5, 2015

Jenkins Maven Error: Unknown Lifecycle Phase "mvn"


[ERROR] Unknown lifecycle phase "mvn" You must specify a valid lifecycle phase or a goal in the format.

This Jenkins issue is very common, while configuring the maven job in Jenkins. One of my peer faced this issue during her development. Then we investigate and fixed this issue.


Jenkins Configuration :-




Error Log :-

[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.316 s
[INFO] Finished at: 2015-10-05T15:10:46+11:00
[INFO] Final Memory: 20M/491M
[INFO] ------------------------------------------------------------------------
[ERROR] Unknown lifecycle phase "mvn". You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>. Available lifecycle phases are: validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy, pre-clean, clean, post-clean, pre-site, site, post-site, site-deploy. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/LifecyclePhaseNotFoundException
[JENKINS] Archiving /opt/jenkins_dev/workspace/MYAPP_BIZ/pom.xml to au.com.mycompany.group.lar.testing.biz/Myapp/0.0.1-SNAPSHOT/Myapp-0.0.1-SNAPSHOT.pom
channel stopped
Started calculate disk usage of build
Finished Calculation of disk usage of build in 0 seconds
Started calculate disk usage of workspace
Finished Calculation of disk usage of workspace in 0 seconds
Finished: FAILURE


Solution :-

Its a simple mistake, that while configuring the jenkins jobs maven goal, we are simple trying with mvn clean install  , which is a against maven life cycle policy.

So , we fixed this by removing the mvn  . Correct command is clean install. Then it worked.


Sunday, September 27, 2015

JAXB Marshalling: Convert Java Object to XML

JAXB Marshalling - Converts Java Object to XML. 

You need to JAXB library jar , download from here.

Really JAXB makes developers life easy and help to develop stable and reliable coding. Below Example shows how to convert the Java object into XML format.

Book.java

package com.techbyteslearn;

 

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Book {
private String name;
private String author;
private String price;
public String getName() {
return name;
}
@XmlElement
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
@XmlElement
public void setAuthor(String author) {
this.author = author;
}
public String getPrice() {
return price;
}
@XmlElement
public void setPrice(String price) {
this.price = price;
}
}

JAXBObjectToXML.java


package com.techbyteslearn;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class JAXBObjectToXML {
/**
* @param args
*/
public static void main(String[] args) {
Book book=new Book();
book.setAuthor("M Bardhan");
book.setName("Java Cookbook");
book.setPrice("$200");
try {
JAXBContext jaxbContext=JAXBContext.newInstance(Book.class);
Marshaller marshaller=jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
marshaller.marshal(book, System.out);
} catch (JAXBException e) {
System.out.println("Error occured ::"+e.getMessage());
}
}
}

Output XML :-

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<book>
    <author>M Bardhan</author>
    <name>Java Cookbook</name>
    <price>$200</price>
</book>




Monday, September 21, 2015

Tomcat WAR Deployment: Properties File Not Found

This issue I faced while loading the war file on Tomcat. This may be a minor issue, but I faced this issue and spend some time to fix this issue.


Actually , I was working on Tomcat newly and I was not sure what configuration we need to load the properties file. Normally all the properties files are placed in class path.

I placed the properties file inside the Tomcat , but I don't know why this issue was coming.

Issue log :-


25/08/2015 1:58:08 PM org.apache.catalina.core.StandardContext loadOnStartup
SEVERE: Servlet /customer_service_gateway-1.0.2-SNAPSHOT threw load() exception
java.io.FileNotFoundException: class path resource [customer_service_gateway/customer.properties] cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:157)
    at org.springframework.core.io.support.EncodedResource.getInputStream(EncodedResource.java:143)
    at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:98)
    at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:175)
    at org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:156)
    at org.springframework.beans.factory.config.PropertiesFactoryBean.createInstance(PropertiesFactoryBean.java:113)
    at org.springframework.beans.factory.config.PropertiesFactoryBean.createProperties(PropertiesFactoryBean.java:98)
    at org.springframework.beans.factory.config.PropertiesFactoryBean.afterPropertiesSet(PropertiesFactoryBean.java:69)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1541)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1479)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:323)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:107)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1387)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1128)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:657)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:461)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:651)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:599)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:665)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:518)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:459)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    at javax.servlet.GenericServlet.init(GenericServlet.java:160)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5123)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5407)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:976)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1653)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:439)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
    at java.lang.Thread.run(Thread.java:662)

The issue was , tomcat loader was not able to locate the file . So , you need to configure this manually. 

Finally , I fixed this by configure the properties directory in tomcat. You need to explicitly mention the properties location from where your tomcat can read the library and other properties. But, its not always advisable .
You can add the <Tomcat Home Directory> into catalina.properties file. 

Example :-
 shared.loader=/usr/java/apache-tomcat-7.0.41
Note :- Catalina.properties file is inside <Tomcat Home Directory> /conf


Thursday, July 16, 2015

Mocking Is Null After @InjectMocks in Mockito

Mocking is null after injecting the mock, this issue is very common in mockito. I faced this issue when trying to write the junit test case . 

This issue seems you need to load the test class using MockitoAnnotations.initMocks. Because , there is certain steps you need to do / setup the data before executing the test method.

@InjectMocks
    private TokenServiceFormatter tokenServiceFormatter ;

As above I have already injected the respective class, that I want to inject. But, while running its showing the tokenServiceFormatter  (injected object ) is null. So, inside the setup() method you need to load the test class as below.


    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);    
    }

 @Test
public void testMethod(){
//Do your test here
}


Happy Mockinggggggggggggg.

Thursday, June 25, 2015

X Error of Failed Request: BadFont (Invalid Font Parameter) in Unix

X Error of failed request:  BadFont (invalid Font parameter)


When I was trying to start WebSphere Developer Studio , faced the with below error. And this is very common error in UNIX. Because , there are many local and international languages.Its basically not able to find the respective font.

dev@jdg-developer-desktop /opt/IBM/WebSphereStudio/ApplicationDeveloper/v5.1.1 $ ./shortcut_wsappdev
Warning: locale not supported by C library, locale unchanged
X Error of failed request:  BadFont (invalid Font parameter)
  Major opcode of failed request:  55 (X_CreateGC)
  Resource id in failed request:  0x0
  Serial number of failed request:  180
  Current serial number in output stream:  184



You need to sure and make the language installed 

sudo apt-get install language-pack-en-base


Now reconfigure some local languages this is of Ubuntu (try with super user):

sudo dpkg-reconfigure locales


Now you can try your local language :

locale -a


Now you can set your expected language as per your requirement. I tried all and set in my Ubuntu Linux machine. It worked for me.

export LC_ALL="en.utf-8"


export LC_ALL="en_US"
export LANG="en_US"
export LANGUAGE="en_NZ"
export C_CTYPE="en_US"
export LC_NUMERIC=
export LC_TIME=en"en_US"

Find more here about locale.

Saturday, June 20, 2015

How to Decompile Java Class Files on Linux and Windows

How to install graphical java de-compiler (jd-gui) ?

Really this is very basic and very useful when you want to de compile the existing old legacy code . I faced this kind of situation many times and did the same thing. I am sharing this because it may help you .

I am using Linux (Ubuntu Maya). But, you can try with any other Linux distributions even windows OS also. You need to download the 

Download from here.


You  need to download the.tar.gz file  ( jd-gui-0.3.5.linux.i686.tar.gz) for Linux. Then extract the file and go to jd-gui-0.3.5.linux.i686 folder. You can extact via below command else you can try by right+click on the file and extract.

tar -xvf jd-gui-0.3.5.linux.i686.tar.gz

Check there are such below files in that directory.

 
dev@jdg-developer-desktop ~/Downloads/jd-gui-0.3.5.linux.i686 $ ls -ltr
total 1100
drwx------ 3 dev dev    4096 Aug 29  2012 contrib
-rwxrwxr-x 1 dev dev 1111160 Oct 16  2012 jd-gui
-rw-r--r-- 1 dev dev    2462 Oct 16  2012 readme.txt
-rw-r--r-- 1 dev dev     317 Jun  4 14:15 jd-gui.cfg
dev@jdg-developer-desktop ~/Downloads/jd-gui-0.3.5.linux.i686 $ 

Execute the jd-gui and open the application :
dev@jdg-developer-desktop ~/Downloads/jd-gui-0.3.5.linux.i686 $ ./jd-gui  




You can import multiple jar files for de-compile and see the code.

Note - You may get some kind of library missing error , but it depends on you operating system.Whether you are using Fedora,Mint ,Cent Os or Ubuntu. So, you need to install the respective lib and need to update the OS terminal if required.You need to install the below extra libraries if its asking.

 sudo apt-get install libgtk2.0-0:i386 libxxf86vm1:i386 libsm6:i386 lib32stdc++6
I have fixed in my desktop, because of this missing lib I was getting some kind of library missing error. But you can fix this and TRY........Good Luck.

Thursday, May 28, 2015

How to Convert Epoch Time to Human-Readable Format in Unix

How to convert epoch to human readable format in UNIX ?


In this example the epoch time is 1432783870. Use the below command to convert the epoch time to human readable time.Technically epoch time known as UNIX time , also known as POSIX time. 

Example :-

dev@javadevelopersguide-developer-desktop ~/Epoch/ $ date -d @1432783870
Thu May 28 13:31:10 EST 2015
dev@javadevelopersguide-developer-desktop ~/Epoch/ $ date -d @1377360000
Sun Aug 25 02:00:00 EST 2013

Find more here.