Monday, September 29, 2014

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]

No comments:

Post a Comment