Tuesday, September 30, 2014

GUI SVN and Diff client for Linux/Unix


  1. I choose RapidSVN + Meld for Ubuntu:
    $ sudo apt-get install rapidsvn
    $ sudo apt-get install meld
  2. Start rapidsvn and setting it's diff tool:
    $ rapidsvn -> View -> Preferences... -> Programs -> Diff Tool -> Browse:"/usr/bin/meld", Program arguments: "%1 %2" -> OK.
  3. Download SVN trunk:
    Repositary -> Checkout -> ...
  4. Reference:
    http://en.wikipedia.org/wiki/Comparison_of_Subversion_clients
    http://en.wikipedia.org/wiki/RapidSVN
    http://en.wikipedia.org/wiki/Comparison_of_file_comparison_tools
    http://en.wikipedia.org/wiki/Meld_(software)

Monday, September 29, 2014

svn tutorial

Add library dependency for Maven

This article is about how to add library dependency for Maven, using log4j and commons-logging as example.
  1. Create basic project with maven:
     mvn archetype:generate \
     -DarchetypeArtifactId=maven-archetype-quickstart \
     -DinteractiveMode=false \
     -DgroupId=tw.com.codedata \
     -DartifactId=helloworld
    
     [INFO] Scanning for projects...
     [INFO]                                                                         
     [INFO] ------------------------------------------------------------------------
     [INFO] Building Maven Stub Project (No POM) 1
     [INFO] ------------------------------------------------------------------------
     [INFO] 
     [INFO] >>> maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom >>>
     [INFO] 
     [INFO] <<< maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom <<<
     [INFO] 
     [INFO] --- maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom ---
     [INFO] Generating project in Batch mode
     [INFO] ----------------------------------------------------------------------------
     [INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0
     [INFO] ----------------------------------------------------------------------------
     [INFO] Parameter: groupId, Value: tw.com.codedata
     [INFO] Parameter: packageName, Value: tw.com.codedata
     [INFO] Parameter: package, Value: tw.com.codedata
     [INFO] Parameter: artifactId, Value: helloworld
     [INFO] Parameter: basedir, Value: /home/linst/ws/study/maven
     [INFO] Parameter: version, Value: 1.0-SNAPSHOT
     [INFO] project created from Old (1.x) Archetype in dir: /home/linst/ws/study/maven/helloworld
     [INFO] ------------------------------------------------------------------------
     [INFO] BUILD SUCCESS
     [INFO] ------------------------------------------------------------------------
     [INFO] Total time: 1:46.068s
     [INFO] Finished at: Mon Sep 29 18:06:53 CST 2014
     [INFO] Final Memory: 10M/239M
     [INFO] ------------------------------------------------------------------------
    
    $ tree .
    .
    └── helloworld
        ├── pom.xml
        └── src
            ├── main
            │   └── java
            │       └── tw
            │           └── com
            │               └── codedata
            │                   └── App.java
            └── test
                └── java
                    └── tw
                        └── com
                            └── codedata
                                └── AppTest.java
    
    12 directories, 3 files
    
     
     
       4.0.0
       tw.com.codedata
       helloworld
       jar
       1.0-SNAPSHOT
       helloworld
       http://maven.apache.org
       
         
           junit
           junit
           3.8.1
           test
         
       
     
    
  2. Create file ./helloworld/src/main/resources/log4j.properties:
     $ cd helloworld/src/main/
     $ mkdir resources
     $ vim resources/log4j.properties
    
     # Set root logger level to DEBUG and have appenders A1, A2.
     log4j.rootLogger=DEBUG, A1, A2
      
     # A1 is set to be a ConsoleAppender
     log4j.appender.A1=org.apache.log4j.ConsoleAppender
     log4j.appender.A1.layout=org.apache.log4j.PatternLayout
     log4j.appender.A1.layout.ConversionPattern=[%d{yyyy/MM/dd HH:mm:ss,SSS}][%p][%C-%L] %m%n
      
     # A2 is set to be a DailyRollingFileAppender to path "./log/Log4j.log"
     log4j.appender.A2=org.apache.log4j.DailyRollingFileAppender
     log4j.appender.A2.layout=org.apache.log4j.PatternLayout
     log4j.appender.A2.layout.ConversionPattern=[%d{yyyy/MM/dd HH:mm:ss,SSS}][%p][%C-%L] %m%n
     log4j.appender.A2.File=./log/Log4j.log
    
  3. Update ./helloworld/src/main/java/tw/com/codedata/App.java to HelloWorld.java with below content:
     
     package tw.com.codedata;
      
     import org.apache.commons.logging.Log;
     import org.apache.commons.logging.LogFactory;
      
     public class HelloWorld {
      
         static Log logger = LogFactory.getLog(HelloWorld.class);
      
         public static void main(String[] args) {
      logger.info("Hello World");
         }
      
     }
    
  4. Try to build, you will get errors because no required logging libraries:
     $ cd helloworld/
     $ mvn package
     [INFO] Scanning for projects...
     [INFO]                                                                         
     [INFO] ------------------------------------------------------------------------
     [INFO] Building helloworld 1.0-SNAPSHOT
     [INFO] ------------------------------------------------------------------------
     [INFO] 
     [INFO] --- maven-resources-plugin:2.3:resources (default-resources) @ helloworld ---
     [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
     [INFO] Copying 1 resource
     [INFO] 
     [INFO] --- maven-compiler-plugin:2.0.2:compile (default-compile) @ helloworld ---
     [INFO] Compiling 1 source file to /home/linst/ws/study/maven/helloworld/target/classes
     [INFO] ------------------------------------------------------------------------
     [INFO] BUILD FAILURE
     [INFO] ------------------------------------------------------------------------
     [INFO] Total time: 0.792s
     [INFO] Finished at: Mon Sep 29 18:27:38 CST 2014
     [INFO] Final Memory: 8M/239M
     [INFO] ------------------------------------------------------------------------
     [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.0.2:compile (default-compile) on project helloworld: Compilation failure: Compilation failure:
     [ERROR] /home/linst/ws/study/maven/helloworld/src/main/java/tw/com/codedata/HelloWorld.java:[3,33] package org.apache.commons.logging does not exist
     [ERROR] 
     [ERROR] /home/linst/ws/study/maven/helloworld/src/main/java/tw/com/codedata/HelloWorld.java:[4,33] package org.apache.commons.logging does not exist
     [ERROR] 
     [ERROR] /home/linst/ws/study/maven/helloworld/src/main/java/tw/com/codedata/HelloWorld.java:[8,11] cannot find symbol
     [ERROR] symbol  : class Log
     [ERROR] location: class tw.com.codedata.HelloWorld
     [ERROR] 
     [ERROR] /home/linst/ws/study/maven/helloworld/src/main/java/tw/com/codedata/HelloWorld.java:[8,24] cannot find symbol
     [ERROR] symbol  : variable LogFactory
     [ERROR] location: class tw.com.codedata.HelloWorld
     [ERROR] -> [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/MojoFailureException
    
  5. Update pom.xml to add two dependency:
        ... 
        
          junit
          junit
          3.8.1
          test
        
        
          commons-logging
          commons-logging
          1.1.1
        
        ... 
    
  6. Build again:
     $ mvn package
     [INFO] Scanning for projects...
     [INFO]                                                                         
     [INFO] ------------------------------------------------------------------------
     [INFO] Building helloworld 1.0-SNAPSHOT
     [INFO] ------------------------------------------------------------------------
     Downloading: http://nexus-ams.tomtomgroup.com:8081/nexus/content/groups/public/log4j/log4j/1.2.16/log4j-1.2.16.pom
     Downloaded: http://nexus-ams.tomtomgroup.com:8081/nexus/content/groups/public/log4j/log4j/1.2.16/log4j-1.2.16.pom (20 KB at 11.4 KB/sec)
     Downloading: http://nexus-ams.tomtomgroup.com:8081/nexus/content/groups/public/log4j/log4j/1.2.16/log4j-1.2.16.jar
     Downloaded: http://nexus-ams.tomtomgroup.com:8081/nexus/content/groups/public/log4j/log4j/1.2.16/log4j-1.2.16.jar (471 KB at 111.6 KB/sec)
     [INFO] 
     [INFO] --- maven-resources-plugin:2.3:resources (default-resources) @ helloworld ---
     [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
     [INFO] Copying 1 resource
     [INFO] 
     [INFO] --- maven-compiler-plugin:2.0.2:compile (default-compile) @ helloworld ---
     [INFO] Compiling 1 source file to /home/linst/ws/study/maven/helloworld/target/classes
     [INFO] 
     [INFO] --- maven-resources-plugin:2.3:testResources (default-testResources) @ helloworld ---
     [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
     [INFO] skip non existing resourceDirectory /home/linst/ws/study/maven/helloworld/src/test/resources
     [INFO] 
     [INFO] --- maven-compiler-plugin:2.0.2:testCompile (default-testCompile) @ helloworld ---
     [INFO] Compiling 1 source file to /home/linst/ws/study/maven/helloworld/target/test-classes
     [INFO] 
     [INFO] --- maven-surefire-plugin:2.10:test (default-test) @ helloworld ---
     [INFO] Surefire report directory: /home/linst/ws/study/maven/helloworld/target/surefire-reports
    
     -------------------------------------------------------
      T E S T S
     -------------------------------------------------------
     Running tw.com.codedata.AppTest
     Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.009 sec
    
     Results :
    
     Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
    
     [INFO] 
     [INFO] --- maven-jar-plugin:2.2:jar (default-jar) @ helloworld ---
     [INFO] Building jar: /home/linst/ws/study/maven/helloworld/target/helloworld-1.0-SNAPSHOT.jar
     [INFO] ------------------------------------------------------------------------
     [INFO] BUILD SUCCESS
     [INFO] ------------------------------------------------------------------------
     [INFO] Total time: 7.618s
     [INFO] Finished at: Mon Sep 29 18:33:32 CST 2014
     [INFO] Final Memory: 13M/239M
     [INFO] ------------------------------------------------------------------------
    
    .
    ├── pom.xml
    ├── src
    │   ├── main
    │   │   ├── java
    │   │   │   └── tw
    │   │   │       └── com
    │   │   │           └── codedata
    │   │   │               ├── App.java~
    │   │   │               └── HelloWorld.java
    │   │   └── resources
    │   │       └── log4j.properties
    │   └── test
    │       └── java
    │           └── tw
    │               └── com
    │                   └── codedata
    │                       └── AppTest.java
    └── target
        ├── classes
        │   ├── log4j.properties
        │   └── tw
        │       └── com
        │           └── codedata
        │               └── HelloWorld.class
        ├── helloworld-1.0-SNAPSHOT.jar
        ├── maven-archiver
        │   └── pom.properties
        ├── surefire
        ├── surefire-reports
        │   ├── TEST-tw.com.codedata.AppTest.xml
        │   └── tw.com.codedata.AppTest.txt
        └── test-classes
            └── tw
                └── com
                    └── codedata
                        └── AppTest.class
    
    24 directories, 12 files
    
  7. Run:
     $ mvn exec:java -Dexec.mainClass="tw.com.codedata.HelloWorld"
     [INFO] Scanning for projects...
     [INFO]                                                                         
     [INFO] ------------------------------------------------------------------------
     [INFO] Building helloworld 1.0-SNAPSHOT
     [INFO] ------------------------------------------------------------------------
     [INFO] 
     [INFO] --- exec-maven-plugin:1.3.2:java (default-cli) @ helloworld ---
     [WARNING] Warning: killAfter is now deprecated. Do you need it ? Please comment on MEXEC-6.
     [2014/09/29 18:38:01,362][INFO][tw.com.codedata.HelloWorld-11] Hello World
     [INFO] ------------------------------------------------------------------------
     [INFO] BUILD SUCCESS
     [INFO] ------------------------------------------------------------------------
     [INFO] Total time: 0.814s
     [INFO] Finished at: Mon Sep 29 18:38:01 CST 2014
     [INFO] Final Memory: 8M/239M
     [INFO] ------------------------------------------------------------------------
    
  8. Reference:
    http://www.codedata.com.tw/java/understanding-gradle-2-maven/ [Chinese]

Install Maven Integration for Eclipse


  1. Website:
    https://www.eclipse.org/m2e/
  2. Install to Eclipse:
    Goto:
    https://www.eclipse.org/m2e/download/

    Copy link:
    Latest m2e release (recommended)
    http://download.eclipse.org/technology/m2e/releases

    Eclipse -> Help -> Install New Software -> Add ->
    Name: m2eclipse
    Location: http://download.eclipse.org/technology/m2e/releases
    -> OK -> Select "Maven Integration for Eclipse" -> Next.
  3. Reference:
    http://mkn939.blogspot.tw/2013/03/eclipde-maven-plugin-m2eclipse-step-by.html [Chinese]

Apache Maven first project for "archetypeArtifactId=maven-archetype-webapp"


  1. Create webapp project:
     $ mvn \
     archetype:generate \
     -DarchetypeArtifactId=maven-archetype-webapp \
     -DinteractiveMode=false \
     -DgroupId=com.myMaven.app \
     -DartifactId=HelloJavaWeb
    
     [INFO] Scanning for projects...
     [INFO]                                                                         
     [INFO] ------------------------------------------------------------------------
     [INFO] Building Maven Stub Project (No POM) 1
     [INFO] ------------------------------------------------------------------------
     [INFO] 
     [INFO] >>> maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom >>>
     [INFO] 
     [INFO] <<< maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom <<<
     [INFO] 
     [INFO] --- maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom ---
     [INFO] Generating project in Batch mode
     (Downloading packages...)
     [INFO] ----------------------------------------------------------------------------
     [INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-webapp:1.0
     [INFO] ----------------------------------------------------------------------------
     [INFO] Parameter: groupId, Value: com.myMaven.app
     [INFO] Parameter: packageName, Value: com.myMaven.app
     [INFO] Parameter: package, Value: com.myMaven.app
     [INFO] Parameter: artifactId, Value: HelloJavaWeb
     [INFO] Parameter: basedir, Value: /home/linst/ws/study/maven
     [INFO] Parameter: version, Value: 1.0-SNAPSHOT
     [INFO] project created from Old (1.x) Archetype in dir: /home/linst/ws/study/maven/HelloJavaWeb
     [INFO] ------------------------------------------------------------------------
     [INFO] BUILD SUCCESS
     [INFO] ------------------------------------------------------------------------
     [INFO] Total time: 1:31.939s
     [INFO] Finished at: Mon Sep 29 15:28:36 CST 2014
     [INFO] Final Memory: 11M/302M
     [INFO] ------------------------------------------------------------------------
    
    .
    └── HelloJavaWeb
        ├── pom.xml
        └── src
            └── main
                ├── resources
                └── webapp
                    ├── index.jsp
                    └── WEB-INF
                        └── web.xml
    
    6 directories, 3 files
    

  2. pom.xml:
     
     
       4.0.0
       com.myMaven.app
       HelloJavaWeb
       war
       1.0-SNAPSHOT
       HelloJavaWeb Maven Webapp
       http://maven.apache.org
       
         
           junit
           junit
           3.8.1
           test
         
       
       
         HelloJavaWeb
       
     
    
  3. Compile and packaging:
     $ cd HelloJavaWeb/
     $ mvn package
     [INFO] Scanning for projects...
     [INFO]                                                                         
     [INFO] ------------------------------------------------------------------------
     [INFO] Building HelloJavaWeb Maven Webapp 1.0-SNAPSHOT
     [INFO] ------------------------------------------------------------------------
     [INFO] 
     [INFO] --- maven-resources-plugin:2.3:resources (default-resources) @ HelloJavaWeb ---
     [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
     [INFO] Copying 0 resource
     [INFO] 
     [INFO] --- maven-compiler-plugin:2.0.2:compile (default-compile) @ HelloJavaWeb ---
     [INFO] No sources to compile
     [INFO] 
     [INFO] --- maven-resources-plugin:2.3:testResources (default-testResources) @ HelloJavaWeb ---
     [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
     [INFO] skip non existing resourceDirectory /home/linst/ws/study/maven/HelloJavaWeb/src/test/resources
     [INFO] 
     [INFO] --- maven-compiler-plugin:2.0.2:testCompile (default-testCompile) @ HelloJavaWeb ---
     [INFO] No sources to compile
     [INFO] 
     [INFO] --- maven-surefire-plugin:2.10:test (default-test) @ HelloJavaWeb ---
     (Downloading packages...)
     [INFO] No tests to run.
     [INFO] Surefire report directory: /home/linst/ws/study/maven/HelloJavaWeb/target/surefire-reports
     (Downloading packages...)
    
     -------------------------------------------------------
      T E S T S
     -------------------------------------------------------
    
     Results :
    
     Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
    
     [INFO] 
     [INFO] --- maven-war-plugin:2.1.1:war (default-war) @ HelloJavaWeb ---
     [INFO] Packaging webapp
     [INFO] Assembling webapp [HelloJavaWeb] in [/home/linst/ws/study/maven/HelloJavaWeb/target/HelloJavaWeb]
     [INFO] Processing war project
     [INFO] Copying webapp resources [/home/linst/ws/study/maven/HelloJavaWeb/src/main/webapp]
     [INFO] Webapp assembled in [15 msecs]
     [INFO] Building war: /home/linst/ws/study/maven/HelloJavaWeb/target/HelloJavaWeb.war
     [INFO] WEB-INF/web.xml already added, skipping
     [INFO] ------------------------------------------------------------------------
     [INFO] BUILD SUCCESS
     [INFO] ------------------------------------------------------------------------
     [INFO] Total time: 10.573s
     [INFO] Finished at: Mon Sep 29 15:36:19 CST 2014
     [INFO] Final Memory: 7M/239M
     [INFO] ------------------------------------------------------------------------
    
    .
    ├── pom.xml
    ├── src
    │   └── main
    │       ├── resources
    │       └── webapp
    │           ├── index.jsp
    │           └── WEB-INF
    │               └── web.xml
    └── target
        ├── classes
        ├── HelloJavaWeb
        │   ├── index.jsp
        │   ├── META-INF
        │   └── WEB-INF
        │       ├── classes
        │       └── web.xml
        ├── HelloJavaWeb.war
        ├── maven-archiver
        │   └── pom.properties
        └── surefire
    
    13 directories, 7 files
    
  4. You can see the generated "HelloJavaWeb.war" which can be deploy to Tomcat web server.
  5. Adding jar files, ex: modify pom.xml to add mysql dependency:
     
     
       4.0.0
       com.myMaven.app
       HelloJavaWeb
       war
       1.0-SNAPSHOT
       HelloJavaWeb Maven Webapp
       http://maven.apache.org
       
         
           junit
           junit
           3.8.1
           test
         
         
           mysql
           mysql-connector-java
           5.1.24
         
       
       
         HelloJavaWeb
       
     
    
  6. Compile and packaging:
    $ mvn package
    
    .
    ├── pom.xml
    ├── src
    │   └── main
    │       ├── resources
    │       └── webapp
    │           ├── index.jsp
    │           └── WEB-INF
    │               └── web.xml
    └── target
        ├── classes
        ├── HelloJavaWeb
        │   ├── index.jsp
        │   ├── META-INF
        │   └── WEB-INF
        │       ├── classes
        │       ├── lib
        │       │   └── mysql-connector-java-5.1.24.jar
        │       └── web.xml
        ├── HelloJavaWeb.war
        ├── maven-archiver
        │   └── pom.properties
        └── surefire
    
    14 directories, 8 files
    
  7. You can see the generated "mysql-connector-java-5.1.24.jar" and it will also be packaged in "HelloJavaWeb.war".
  8. References:
    http://mkn939.blogspot.tw/2013/03/maven-hellomaven-maven-step-by-step.html [Chinese]

Sunday, September 28, 2014

Apache Maven first project for "archetypeArtifactId=maven-archetype-quickstart"


  1. Apache Maven:
    "Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information."
  2. Ubuntu install:
     
     $ sudo apt-get install maven
    
  3. Where is it and version:
     
     $ which mvn
     /usr/bin/mvn
    
     $ mvn -v
     Apache Maven 3.0.4
     Maven home: /usr/share/maven
     Java version: 1.6.0_31, vendor: Sun Microsystems Inc.
     Java home: /usr/lib/jvm/java-6-openjdk-amd64/jre
     Default locale: en_US, platform encoding: UTF-8
     OS name: "linux", version: "3.2.0-65-generic", arch: "amd64", family: "unix"
    
  4. Create first project:
     
     $ mvn \
     archetype:generate \
     -DarchetypeArtifactId=maven-archetype-quickstart \
     -DinteractiveMode=false \
     -DgroupId=com.myMaven.app \
     -DartifactId=HelloMaven
    
     [INFO] Scanning for projects...
     (Downloading packages...)
     [INFO]                                                                         
     [INFO] ------------------------------------------------------------------------
     [INFO] Building Maven Stub Project (No POM) 1
     [INFO] ------------------------------------------------------------------------
     [INFO] 
     [INFO] >>> maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom >>>
     [INFO] 
     [INFO] <<< maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom <<<
     [INFO] 
     [INFO] --- maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom ---
     (Downloading packages...)
     [INFO] Generating project in Batch mode
     (Downloading packages...)
     [INFO] ----------------------------------------------------------------------------
     [INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0
     [INFO] ----------------------------------------------------------------------------
     [INFO] Parameter: groupId, Value: com.myMaven.app
     [INFO] Parameter: packageName, Value: com.myMaven.app
     [INFO] Parameter: package, Value: com.myMaven.app
     [INFO] Parameter: artifactId, Value: HelloMaven
     [INFO] Parameter: basedir, Value: /home/linst/ws/study/maven
     [INFO] Parameter: version, Value: 1.0-SNAPSHOT
     [INFO] project created from Old (1.x) Archetype in dir: /home/linst/ws/study/maven/HelloMaven
     [INFO] ------------------------------------------------------------------------
     [INFO] BUILD SUCCESS
     [INFO] ------------------------------------------------------------------------
     [INFO] Total time: 3:05.835s
     [INFO] Finished at: Mon Sep 29 11:28:32 CST 2014
     [INFO] Final Memory: 9M/239M
     [INFO] ------------------------------------------------------------------------
    
  5. About groupId, artifactId and archetypeArtifactId:
     
     groupId: This element indicates the unique identifier of the organization or group that created the project.
     artifactId: This element indicates the unique base name of the primary artifact being generated by this project.
     archetypeArtifactId: The archetype's artifactId.
    

    http://maven.apache.org/archetype/maven-archetype-plugin/generate-mojo.html#archetypeCatalog
  6. The generated tree and files and pom.xml:
     
     .
     └── HelloMaven
         ├── pom.xml
         └── src
             ├── main
             │   └── java
             │       └── com
             │           └── myMaven
             │               └── app
             │                   └── App.java
             └── test
                 └── java
                     └── com
                         └── myMaven
                             └── app
                                 └── AppTest.java
    
     12 directories, 3 files
    
     
     
       4.0.0
       com.myMaven.app
       HelloMaven
       jar
       1.0-SNAPSHOT
       HelloMaven
       http://maven.apache.org
       
         
           junit
           junit
           3.8.1
           test
         
       
     
    
  7. Compile:
     
     $ cd HelloMaven/
     $ mvn compile
     [INFO] Scanning for projects...
     [INFO]                                                                         
     [INFO] ------------------------------------------------------------------------
     [INFO] Building HelloMaven 1.0-SNAPSHOT
     [INFO] ------------------------------------------------------------------------
     (Downloading packages...)
     [INFO] 
     [INFO] --- maven-resources-plugin:2.3:resources (default-resources) @ HelloMaven ---
     [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
     [INFO] skip non existing resourceDirectory /home/linst/ws/study/maven/HelloMaven/src/main/resources
     [INFO] 
     [INFO] --- maven-compiler-plugin:2.0.2:compile (default-compile) @ HelloMaven ---
     (Downloading packages...)
     [INFO] Compiling 1 source file to /home/linst/ws/study/maven/HelloMaven/target/classes
     [INFO] ------------------------------------------------------------------------
     [INFO] BUILD SUCCESS
     [INFO] ------------------------------------------------------------------------
     [INFO] Total time: 13.615s
     [INFO] Finished at: Mon Sep 29 15:02:17 CST 2014
     [INFO] Final Memory: 8M/239M
     [INFO] ------------------------------------------------------------------------
    
    .
    ├── pom.xml
    ├── src
    │   ├── main
    │   │   └── java
    │   │       └── com
    │   │           └── myMaven
    │   │               └── app
    │   │                   └── App.java
    │   └── test
    │       └── java
    │           └── com
    │               └── myMaven
    │                   └── app
    │                       └── AppTest.java
    └── target
        └── classes
            └── com
                └── myMaven
                    └── app
                        └── App.class
    
    16 directories, 4 files
    
  8. Run:
     
     $ mvn exec:java -Dexec.mainClass=com.myMaven.app.App
     [INFO] Scanning for projects...
     (Downloading packages...)
     [INFO]                                                                         
     [INFO] ------------------------------------------------------------------------
     [INFO] Building HelloMaven 1.0-SNAPSHOT
     [INFO] ------------------------------------------------------------------------
     [INFO] 
     [INFO] --- exec-maven-plugin:1.3.2:java (default-cli) @ HelloMaven ---
     (Downloading packages...)
     [WARNING] Warning: killAfter is now deprecated. Do you need it ? Please comment on MEXEC-6.
     Hello World!@@!
     [INFO] ------------------------------------------------------------------------
     [INFO] BUILD SUCCESS
     [INFO] ------------------------------------------------------------------------
     [INFO] Total time: 50.464s
     [INFO] Finished at: Mon Sep 29 15:15:47 CST 2014
     [INFO] Final Memory: 7M/239M
     [INFO] ------------------------------------------------------------------------
    
  9. References:
    http://maven.apache.org/index.html
    http://maven.apache.org/guides/getting-started/index.html
    http://maven.apache.org/pom.html
    http://www.codedata.com.tw/java/understanding-gradle-2-maven/ [Chinese]
    http://mkn939.blogspot.tw/2013/03/maven-hellomaven-maven-step-by-step.html [Chinese]

Friday, September 26, 2014

Apache logging services, Apache log4j, Apache commons logging

Apache logging services


"The Apache Logging Services Project creates and maintains open-source software related to the logging of application behavior and released at no charge to the public."


Components:

Apache Log4j 2

Log4j 2 is an upgrade to Log4j that provides significant improvements over its predecessor, Log4j 1.x.

Apache log4php

A versatile logging framework for PHP. Originally a port of Apache log4j to PHP, it has grown to include various PHP specific features.

Apache log4net

A port of the excellent Apache log4j framework to the Microsoft .NET runtime.

Apache chainsaw

A GUI based log viewer. Chainsaw is a companion application to log4j written by members of the log4j development community.

Apache log4j

The original Apache logging framework for Java.

Apache log4cxx

Apache log4cxx is a logging framework for C++ patterned after log4j.

Apache log4j extras

Extras for log4j 1.x, like companions, receivers and more.


How to use Apache log4j 1.x: 

  1. Website:
    https://logging.apache.org/log4j/1.2/

    "With log4j it is possible to enable logging at runtime without modifying the application binary."
    "The target of the log output can be a file, an OutputStream, a java.io.Writer, a remote log4j server, a remote Unix Syslog daemon, or many other output targets."
  2. Read before start:
    https://logging.apache.org/log4j/1.2/manual.html
  3. Download:
    https://logging.apache.org/log4j/1.2/download.html
    http://ftp.tc.edu.tw/pub/Apache/logging/log4j/1.2.17/log4j-1.2.17.zip
    http://ftp.mirror.tw/pub/apache/logging/log4j/1.2.17/log4j-1.2.17.tar.gz
    Linux: "log4j-1.2.17.tar.gz"
    Windows: "log4j-1.2.17.zip"
  4. Import to Eclipse:
    Eclipse -> New Java Project "log4jTest01" -> Next -> Adding Log4j JAR file by "Libraries → Add External JARs" -> Select "log4j-1.2.17.zip" or "log4j-1.2.17.tar.gz" -> Finish.
  5. Create log4j.properties file in project src folder:
      
      # Set root logger level to DEBUG and have appenders A1, A2.
      log4j.rootLogger=DEBUG, A1, A2
    
      # A1 is set to be a ConsoleAppender
      log4j.appender.A1=org.apache.log4j.ConsoleAppender
      log4j.appender.A1.layout=org.apache.log4j.PatternLayout
      log4j.appender.A1.layout.ConversionPattern=[%d{yyyy/MM/dd HH:mm:ss,SSS}][%p][%C-%L] %m%n
    
      # A2 is set to be a DailyRollingFileAppender to path "./log/Log4j.log"
      log4j.appender.A2=org.apache.log4j.DailyRollingFileAppender
      log4j.appender.A2.layout=org.apache.log4j.PatternLayout
      log4j.appender.A2.layout.ConversionPattern=[%d{yyyy/MM/dd HH:mm:ss,SSS}][%p][%C-%L] %m%n
      log4j.appender.A2.File=./log/Log4j.log
    
      # Other usage: Enable below line to print only messages of level WARN or above in the package "log4jTest.log4jTest01".
      # log4j.logger.log4jTest.log4jTest01=WARN
    
  6. Create java package "log4jTest" and add source log4jTest01.java with class log4jTest01:
  7.   package log4jTest;
    
      import org.apache.log4j.Level;
      import org.apache.log4j.Logger;  
    
      public class log4jTest01 {
        private static Logger loggerlog4jTest01 = Logger.getLogger(log4jTest01.class);  
    
        public static void main(String[] args) {  
          // Example 1: (TRACE < DEBUG < INFO < WARN < ERROR < FATAL)
          loggerlog4jTest01.info("-------------------------------------------------");
          loggerlog4jTest01.info("Example 1: loggerlog4jTest01 will using Level setting in log4j.properties file");
          loggerlog4jTest01.trace("This is trace message.");
          loggerlog4jTest01.debug("This is debug message.");
          loggerlog4jTest01.info("This is info message.");
          loggerlog4jTest01.warn("This is warn message.");
          loggerlog4jTest01.error("This is error message.");
          loggerlog4jTest01.info("-------------------------------------------------");
    
          // Example 2: (TRACE < DEBUG < INFO < WARN < ERROR < FATAL)
          // Test two logger "com.foo" and "com.foo.Bar"
          loggerlog4jTest01.info("-------------------------------------------------");
          loggerlog4jTest01.info("Example 2: barlogger will inherit debug level from foologger");
          // get a logger instance named "com.foo"
          Logger  foologger = Logger.getLogger("com.foo");
    
          // Now set its level. Normally you do not need to set the
          // level of a logger programmatically. This is usually done
          // in configuration files.
          foologger.setLevel(Level.INFO);
          foologger.info("foologger setLevel to Level.INFO");
    
          Logger barlogger = Logger.getLogger("com.foo.Bar");
    
          // This request is enabled, because WARN >= INFO.
          foologger.warn("foologger Low fuel level.");
    
          // This request is disabled, because DEBUG < INFO.
          foologger.debug("foologger Starting search for nearest gas station.");
    
          // The logger instance barlogger, named "com.foo.Bar",
          // will inherit its level from the logger named
          // "com.foo" Thus, the following request is enabled
          // because INFO >= INFO.
          barlogger.info("barlogger Located nearest gas station.");
    
          // This request is disabled, because DEBUG < INFO.
          barlogger.debug("barlogger Exiting gas station search");
          loggerlog4jTest01.info("-------------------------------------------------");
        }  
      }
    
  8. Build and run, you will see below output in Eclipse Console window:
      [2014/09/28 17:56:35,583][INFO][log4jTest.log4jTest01-11] -------------------------------------------------
      [2014/09/28 17:56:35,588][INFO][log4jTest.log4jTest01-12] Example 1: loggerlog4jTest01 will using Level setting in log4j.properties file
      [2014/09/28 17:56:35,589][DEBUG][log4jTest.log4jTest01-14] This is debug message.
      [2014/09/28 17:56:35,590][INFO][log4jTest.log4jTest01-15] This is info message.
      [2014/09/28 17:56:35,590][WARN][log4jTest.log4jTest01-16] This is warn message.
      [2014/09/28 17:56:35,591][ERROR][log4jTest.log4jTest01-17] This is error message.
      [2014/09/28 17:56:35,591][INFO][log4jTest.log4jTest01-18] -------------------------------------------------
      [2014/09/28 17:56:35,592][INFO][log4jTest.log4jTest01-22] -------------------------------------------------
      [2014/09/28 17:56:35,595][INFO][log4jTest.log4jTest01-23] Example 2: barlogger will inherit debug level from foologger
      [2014/09/28 17:56:35,596][INFO][log4jTest.log4jTest01-31] foologger setLevel to Level.INFO
      [2014/09/28 17:56:35,597][WARN][log4jTest.log4jTest01-36] foologger Low fuel level.
      [2014/09/28 17:56:35,597][INFO][log4jTest.log4jTest01-45] barlogger Located nearest gas station.
      [2014/09/28 17:56:35,598][INFO][log4jTest.log4jTest01-49] -------------------------------------------------
    

Apache commons logging


"When writing a library it is very useful to log information. However there are many logging implementations out there, and a library cannot impose the use of a particular one on the overall application that the library is a part of."

"The Logging package is an ultra-thin bridge between different logging implementations. A library that uses the commons-logging API can be used with any logging implementation at runtime. Commons-logging comes with support for a number of popular logging implementations, and writing adapters for others is a reasonably simple task."

"Applications (rather than libraries) may also choose to use commons-logging."

How to use Apache commons logging: 


  1. Download:
    http://commons.apache.org/proper/commons-logging/download_logging.cgi
    http://ftp.twaren.net/Unix/Web/apache//commons/logging/binaries/commons-logging-1.2-bin.zip
  2. Import to both "log4j-1.2.17.jar" and "commons-logging-1.2.jar" to Eclipse.
  3. Create log4j.properties file(same as previous one) in project src folder.
  4. HelloWorld.java:
      package demo.JclTest;
    
      import org.apache.commons.logging.Log;
      import org.apache.commons.logging.LogFactory;
       
      public class HelloWorld {
        static Log logger = LogFactory.getLog(HelloWorld.class);
    
        public static void main(String[] args) {
          logger.info("Hello World");
          
          // Example 1: (TRACE < DEBUG < INFO < WARN < ERROR < FATAL)
          logger.info("-------------------------------------------------");
          logger.info("Example 1: logger will using Level setting in log4j.properties file");
          logger.trace("This is trace message.");
          logger.debug("This is debug message.");
          logger.info("This is info message.");
          logger.warn("This is warn message.");
          logger.error("This is error message.");
          logger.info("-------------------------------------------------");
        }
      }
    
  5. Build and run:
      [2014/09/28 18:29:11,645][INFO][demo.JclTest.HelloWorld-10] Hello World
      [2014/09/28 18:29:11,650][INFO][demo.JclTest.HelloWorld-13] -------------------------------------------------
      [2014/09/28 18:29:11,650][INFO][demo.JclTest.HelloWorld-14] Example 1: logger will using Level setting in log4j.properties file
      [2014/09/28 18:29:11,651][DEBUG][demo.JclTest.HelloWorld-16] This is debug message.
      [2014/09/28 18:29:11,652][INFO][demo.JclTest.HelloWorld-17] This is info message.
      [2014/09/28 18:29:11,652][WARN][demo.JclTest.HelloWorld-18] This is warn message.
      [2014/09/28 18:29:11,653][ERROR][demo.JclTest.HelloWorld-19] This is error message.
      [2014/09/28 18:29:11,653][INFO][demo.JclTest.HelloWorld-20] -------------------------------------------------
    



Monday, September 22, 2014

About *.md files

What are those README.md, CONTRIBUTING.md files in github?

"Markdown is a plain text formatting syntax[5] designed so that it optionally can be converted to HTML using a tool by the same name. Markdown is popularly used to format readme files, for writing messages in online discussion forums or in text editors for the quick creation of rich text documents."

http://en.wikipedia.org/wiki/Markdown
http://en.wikipedia.org/wiki/Markup_language
http://en.wikipedia.org/wiki/Comparison_of_document_markup_languages
http://markdown.tw/ [Chinese]

About .gitignore file

Ignoring files:
https://help.github.com/articles/ignoring-files

A collection of useful .gitignore templates:
https://github.com/github/gitignore

Some common .gitignore configurations:
https://gist.github.com/octocat/9257657


SyntaxHighlighter for Blogger

SyntaxHighlighter

Official website: http://alexgorbatchev.com/SyntaxHighlighter/
"SyntaxHighlighter is a fully functional self-contained code syntax highlighter developed in JavaScript."

Download or using hosted version

From http://alexgorbatchev.com/SyntaxHighlighter/download/, two ways to use SyntaxHighlighter:
  1. Download whole package(ex: "syntaxhighlighter_3.0.83.zip") and upload to your host server.
  2. Use a free hosted version according to http://alexgorbatchev.com/SyntaxHighlighter/hosting.html. (You can donate to help SyntaxHighlighter author pay for these extra bandwidth :))
Here I choose using free hosted version.

Installation

Two way to install:
  1. You can add all brushes manually to your post: http://alexgorbatchev.com/SyntaxHighlighter/manual/installation.html
  2. You can also use dynamic brush loader: http://alexgorbatchev.com/SyntaxHighlighter/manual/api/autoloader.html

Here I choose manually way.

Available brushes

http://alexgorbatchev.com/SyntaxHighlighter/manual/brushes/

Configuration for Blogger

http://alexgorbatchev.com/SyntaxHighlighter/manual/configuration/

My steps for Blogger

1. Where are those scripts and css files:
  • syntaxhighlighter_3.0.83\scripts\*.js
  • syntaxhighlighter_3.0.83\styles\*.css
2. Go to your Template -> Edit HTML.
3. Add below content before </head> tag ends:

    
    
    
    
    

    
    

    
    

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    
    
    
4. Save the template.
5. Here’s examples:

  • <pre /> method:
      
        /**
        * SyntaxHighlighter
        */
        function foo()
        {
          if (counter <= 10)
            return;
          // it works!
        }
      
    Will render as:
        /**
        * SyntaxHighlighter
        */
        function foo()
        {
          if (counter <= 10)
            return;
          // it works!
        }
      
  • <script /> method:
      
    
    Will render as:

References

http://geektalkin.blogspot.in/2009/11/embed-code-syntax-highlighting-in-blog.html
http://wesingkasu.blogspot.in/2013/05/blogger-syntaxhighlighter.html [Chinese]
http://pydoing.blogspot.in/2010/11/syntaxhighlighter-blogger.html [Chinese]

Friday, September 19, 2014

Debug Android executables using gdb and gdbserver

About 3 monthes ago I created a POC which is a cross-built executable running on a rooting Android device, but it crashed unexpectedly with only "Segmentation fault" messages. Because there is no GDB available in my Google Nexus 7, so after some studies, I found that I can cross debugging with GDB and GDBServer in NDK folder:
$ find ndk9c/ -name "gdbserver" -type f
ndk9c/prebuilt/android-mips/gdbserver/gdbserver
ndk9c/prebuilt/android-arm/gdbserver/gdbserver
ndk9c/prebuilt/android-x86/gdbserver/gdbserver
$ find ndk9c/ -name "arm*gdb" -type f
ndk9c/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gdb
ndk9c/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gdb

Here are my steps:

On Host

$ adb push ndk9c/prebuilt/android-arm/gdbserver/gdbserver /data/working
$ adb forward tcp:1234 tcp:1234

On Device

$ cd /data/working
$ ./gdbserver localhost:1234 MyProgram
Process ./MyProgram created; pid = 729
Listening on port 1234

On Host

$ ./arm-linux-androideabi-gdb ./MyProgram
GNU gdb (GDB) 7.3.1-gg2
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "--host=x86_64-linux-gnu --target=arm-linux-android".
For bug reporting instructions, please see:
...
Reading symbols from /home/steven/working/bin/MyProgram...done.

(gdb) target remote 169.254.255.1:1234
Remote debugging using 169.254.255.1:1234
warning: Unable to find dynamic linker breakpoint function.
GDB will retry eventurally.  Meanwhile, it is likely
that GDB is unable to debug shared library initializers
or resolve pending breakpoints after dlopen().
0xb0001000 in ?? ()

(gdb)

On Device

After Host machine "target remote 169.254.255.1:1234", Device gdbserver will show below message:
...
Listening on port 1234
Remote debugging from host 169.254.255.2

On Host

Finally you can start the executable on Host machine and waiting for the crashed, then debug it:
(gdb) b UnicodeConversion.cpp:373
(gdb) c
Continuing.
...
Program received signal SIGSEGV, Segmentation fault.
...
(gdb) bt
...
(gdb)

References

http://www.kandroid.org/online-pdk/guide/debugging_gdb.html
http://appleapplecat.pixnet.net/blog/post/32464205-gdbserver-on-android [Chinese]

Chinese sentence tokenization using SCWS

Last year, I was asked to add a Chinese Tokenizer feature for our search engine. I studied some already exists libraries and finally found SCWS(Simple Chinese Word Segmentation) which is a open source library and command line tool created by hightman and can be download from github.

Basic concepts


About SCWS

https://github.com/hightman/scws

Build libscws and its command line tools using CMake

1. Download scws from github, and checkout tag 1.2.2:

$ git clone https://github.com/hightman/scws.git ~/ws/scws/
$ git tag
1.2.0
1.2.1
1.2.2
$ git checkout 1.2.2

2. Build libscws and command line tools:

Prepare Environment:
$ sudo apt-get install cmake
$ mkdir /home/leer/ws/myscws
$ cd /home/leer/ws/myscws
$ cp -rf ~/ws/scws/libscws/ .
$ cp -rf ~/ws/scws/cli/ .

Create CMake files:
$ cd /home/leer/ws/myscws
$ vim CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(myscws)
set(myscws_ROOT ${CMAKE_CURRENT_LIST_DIR} CACHE STRING "myscws root directory")
add_subdirectory(
 ${CMAKE_CURRENT_SOURCE_DIR}/libscws
 ${CMAKE_CURRENT_BINARY_DIR}/libscws)
add_subdirectory(
 ${CMAKE_CURRENT_SOURCE_DIR}/cli
 ${CMAKE_CURRENT_BINARY_DIR}/cli)


$ vim /home/leer/ws/libscws/CMakeLists.txt
project(scws)
set(LIBSCWS_ROOT ${PROJECT_SOURCE_DIR} CACHE STRING "LIBSCWS_ROOT")
set(LIBSCWS_API_INCLUDES ${LIBSCWS_ROOT} CACHE STRING "LIBSCWS_API_INCLUDES")
include_directories(${LIBSCWS_API_INCLUDES})
file(GLOB LIBSCWS_HEADER_FILES ${PROJECT_SOURCE_DIR}/*.h)
file(GLOB LIBSCWS_SOURCE_FILES ${PROJECT_SOURCE_DIR}/*.c)
add_library(${PROJECT_NAME} STATIC ${LIBSCWS_SOURCE_FILES} ${LIBSCWS_HEADER_FILES})
target_link_libraries(${PROJECT_NAME} m)


$ vim /home/leer/ws/cli/CMakeLists.txt
project(scws_cli)
set(SCWS_CMD_ROOT ${PROJECT_SOURCE_DIR} CACHE STRING "SCWS_CMD_ROOT")
include_directories(${LIBSCWS_API_INCLUDES})
file(GLOB SCWS_CLI_CMD_SOURCE_FILES ${PROJECT_SOURCE_DIR}/scws_cmd.c)
add_executable(${PROJECT_NAME}_cmd ${SCWS_CLI_CMD_SOURCE_FILES})
target_link_libraries(${PROJECT_NAME}_cmd scws)
file(GLOB SCWS_CLI_GEN_DICT_SOURCE_FILES ${PROJECT_SOURCE_DIR}/gen_dict.c)
add_executable(${PROJECT_NAME}_gen_dict ${SCWS_CLI_GEN_DICT_SOURCE_FILES})
target_link_libraries(${PROJECT_NAME}_gen_dict scws)

Add some patch to SCWS:
$ vim /home/leer/ws/myscws/libscws/version.h:
/* version.h.in. input file for configure */
#ifndef SCWS_VERSION
#define SCWS_VERSION "1.2.2"
#endif
#define SCWS_BUGREPORT "http://www.xunsearch.com/scws"
#define PACKAGE_VERSION "UNKNOW"

$ vim /home/leer/ws/myscws/cli/gen_dict.c
#include "scws.h"

Create build scripts:
$ vim build.sh
#!/bin/bash
mkdir -p Build && cd Build && rm -rf && cmake .. && make && cd -

Start building:
$ chmod 777 build.sh
$ ./build.sh

The library and executables:
$ find . -name "libscws*" -type f
./Build/libscws/libscws.a
$ find . -name "scws_cli*" -type f
./Build/cli/scws_cli_cmd
./Build/cli/scws_cli_gen_dict


3. How to use these command line tools:

TODO.

Using SCWS C APIs

TODO.