Maven: Understanding Lifecycle, Phase and Plugin Goal by code

Categories: Development NotesJava February 11th, 2015 23:24


There are same built-in build lifecycles: default, clean and site. The default lifecycle handles your project deployment, the clean lifecycle handles project cleaning, while the site lifecycle handles the creation of your project’s site documentation.

the default lifecycle includes many phases, e.g: validate, initialize…compile, test, package, integration-test, verify, install and deploy.  check Maven 3 components.xml;

If you run one phase, e.g. install, it will execute not only that phase (install), but also every phase prior to the install phase;


A Build Phase is Made Up of Plugin Goals;

for maven 3.1, compile phase is using: org.apache.maven.plugins:maven-compiler-plugin:3.1:compile;  get more details from default-bindings.xml

Plugin Goal

A plugin may have one or more goals wherein each goal represents a capability of that plugin.

plugins can contain information that indicates which lifecycle phase to bind a goal to. Note that adding the plugin on its own is not enough information – you must also specify the goals you want to run as part of your build. A plugin goal may be bound to zero or more build phases. A goal not bound to any build phase could be executed outside of the build lifecycle by direct invocation.

pom configuration examples:

maven compile:

maven javadoc:

Java: Generate Random String

Categories: Java; Tagged with: ; @ December 22nd, 2014 23:04

1). generate random string using java.util.Random; or
2). Use Apache commons lang;

   public void testRandomString() {
        System.out.println("RandomStringGenerator.getRandomString(5): " + RandomStringGenerator.getRandomString(5));

        System.out.println("RandomStringUtils.random(5): " + RandomStringUtils.random(5));
        System.out.println("RandomStringUtils.randomAlphabetic(5): " + RandomStringUtils.randomAlphabetic(5));
        System.out.println("RandomStringUtils.randomNumeric(5): " + RandomStringUtils.randomNumeric(5));


RandomStringGenerator.getRandomString(5): r2irv
RandomStringUtils.random(5): 㩩ﻰ뒛ţ䍋
RandomStringUtils.randomAlphabetic(5): Nhnfe
RandomStringUtils.randomNumeric(5): 51685

1. Random String Generator:


import java.util.Random;

 * Created by Guoliang, Li on 12/22/2014.
public class RandomStringGenerator {

    private static char[] repo;
    static {
        StringBuilder tmp = new StringBuilder();
        for (char ch = '0'; ch <= '9'; ++ch)
        for (char ch = 'a'; ch <= 'z'; ++ch)
        repo = tmp.toString().toCharArray();

    public static String getRandomString(int length) {
        char[] buf = new char[length];
        Random random = new Random();

        if (length < 1) {
            throw new IllegalArgumentException("length < 1: " + length);

        for (int i = 0; i < buf.length; i++) {
            buf[i] = repo[random.nextInt(repo.length)];
        return new String(buf);

Apache commons lang:


IntelliJ 14 Jetty integration

Categories: Development NotesJava November 30th, 2014 23:02

Bug with Jetty 9.2.5?

Tried Jetty jetty-distribution-9.2.5.v20141112 but got this Error when configuring the jetty server:
”start.d\http.ini not found”;

cannot find jetty.base when starting the sever:

WARNING: Missing Required File: ${jetty.base}\lib\alpn\alpn-boot-8.1.0.v20141016.jar

Use Jetty 8?

works fine with Jetty 8.

Java: Config slf4j and log4j in Maven Project

Categories: Java November 10th, 2014 23:29

1. Config Maven dependences

    <dependency>         <groupId>org.slf4j</groupId>         <artifactId>slf4j-api</artifactId>         <version>1.7.7</version>     </dependency>
      <dependency>        <groupId>org.slf4j</groupId>        <artifactId>slf4j-log4j12</artifactId>

2. Setup log4j configuration

src/main/resources/log4j.xml (

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="">
   <appender name="console" class="org.apache.log4j.ConsoleAppender">
     <param name="Target" value="System.out"/>
     <layout class="org.apache.log4j.PatternLayout">
       <param name="ConversionPattern" value="%d %-5p %c{1} - %m%n"/>

     <priority value ="debug" />
     <appender-ref ref="console" />


log4j.rootLogger=DEBUG, STDOUT
log4j.appender.STDOUT.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

3. Initialize Logger and use it

package com.liguoliang.log;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LogTest {

	static Logger LOGGER = LoggerFactory.getLogger(LogTest.class);
	public static void main(String[] args) {"slf4j test info");


2014-11-10 23:07:39,624 INFO  LogTest - slf4j test info

Google Protocol Buffers Helloworld(Java)

Categories: Java; Tagged with: ; @ July 3rd, 2014 0:15

This is a Protocol Buffers hello world project following

you may find all source code from github:


Code Generation

The message definition: addressbook.proto;

D:\dev\tools\protoc-2.5.0-win32>protoc.exe -I=D:\dev\..\src\main\resosurce\ --java_out=D:\dev..\src\main\java  D:\dev\…\resosurce\addressbook.proto is the generated Java class;



public class ProtocolBuffersTest {
     private static final String name = "google";

     public void testBuildInstance() throws Exception {
         Person person = buildNewPerson();
         assertEquals(name, person.getName());

     public void testWriteInstanceToFile() throws Exception {
         String path = "./src/test/resources/address.txt";
         AddressBook.Builder addressBookBuilder = AddressBook.newBuilder();
         AddressBook addressBook =;
         FileOutputStream outputStream = new FileOutputStream(path);
         System.out.println("AddressBook write to :\n " + path);
         URL url = Resources.getResource("./address.txt");
         String contentInFile = Resources.toString(url, Charsets.UTF_8);
     public void testParseInstanceFromFile() throws FileNotFoundException, IOException {
         AddressBook.Builder addressBookBuilderRead = AddressBook.newBuilder();
         addressBookBuilderRead.mergeFrom(new FileInputStream("./src/test/resources/address-test.txt"));
         AddressBook addressBookRead =;
         System.out.println("AddressBook loaded: \n" + addressBookRead.toString());
         assertEquals(1, addressBookRead.getPersonCount());
         Person person = addressBookRead.getPerson(0);
         assertEquals(name, person.getName());
     private Person buildNewPerson() {
         Person.Builder personBuilder = Person.newBuilder();
         Person person =;
         return person;


Newer Posts <-> Older Posts

// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.