Maven: Understanding Lifecycle, Phase and Plugin Goal by code

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

Lifecycle

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;

Phases

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: http://maven.apache.org/plugins/maven-compiler-plugin/usage.html

maven javadoc: http://maven.apache.org/plugins/maven-javadoc-plugin/usage.html

Java: Generate Random String

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

Solution:
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));
    }

output:


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

1. Random String Generator:

package com.liguoliang.java.utils;

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)
            tmp.append(ch);
        for (char ch = 'a'; ch <= 'z'; ++ch)
            tmp.append(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:

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>

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>
       <version>1.7.7</version>
    </dependency>

2. Setup log4j configuration

src/main/resources/log4j.xml (log4j.properties):

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/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"/>
     </layout>
   </appender>

  <root>
     <priority value ="debug" />
     <appender-ref ref="console" />
   </root>
</log4j:configuration>

or log4j.properties

log4j.rootLogger=DEBUG, STDOUT
log4j.logger.deng=INFO
log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
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) {
		LOGGER.info("slf4j test info");
	}
}

output:

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 https://developers.google.com/protocol-buffers/docs/javatutorial

you may find all source code from github: https://github.com/guoliang-dev/google-protocol-buffers-java-helloworld

image

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

AddressBookProtos.java is the generated Java class;

TestCase:

package com.liguoliang.java.protobuf;

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

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

    @Test
     public void testWriteInstanceToFile() throws Exception {
         String path = "./src/test/resources/address.txt";
         AddressBook.Builder addressBookBuilder = AddressBook.newBuilder();
         addressBookBuilder.addPerson(buildNewPerson());
         AddressBook addressBook = addressBookBuilder.build();
         FileOutputStream outputStream = new FileOutputStream(path);
         addressBook.writeTo(outputStream);
         outputStream.close();
         System.out.println("AddressBook write to :\n " + path);
         URL url = Resources.getResource("./address.txt");
         String contentInFile = Resources.toString(url, Charsets.UTF_8);
         assertTrue(contentInFile.contains(name));
     }
     @Test
     public void testParseInstanceFromFile() throws FileNotFoundException, IOException {
         AddressBook.Builder addressBookBuilderRead = AddressBook.newBuilder();
         addressBookBuilderRead.mergeFrom(new FileInputStream("./src/test/resources/address-test.txt"));
         AddressBook addressBookRead = addressBookBuilderRead.build();
         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();
         personBuilder.setId(1);
         personBuilder.setName(name);
         Person person = personBuilder.build();
         return person;
     }

}

Newer Posts <-> Older Posts



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