Wednesday, February 29, 2012

Maven knowledge

How to install Maven on Windows

Apache Maven is not require to install on Windows as a service component, you just need to download the Maven’s zip file, extract it and configure the Windows environment path variable. See following steps :

1. Add JAVA_HOME

2.Download Apache Maven 

Download Apache Maven zip file from this official website, e.g apache-maven-2.2.1-bin.zip

3. Extract It

Extract the downloaded zip file, for instance, “D:\maven“.

4. Add MAVEN_HOME

5. Add PATH

Add the “Maven bin folder” into Windows environment variable, so that you can access Maven command everywhere

6. Verification

Done, to verify it, in command prompt, type “mvn –version” to verify the installation detail.

 Maven Repsository location 

Maven local repository is default to home directory :
  1. Unix/Mac OS X – ~/.m2 on
  2. Windows – C:\Documents and Settings\username\.m2 on Windows .
  3. 1. Maven configuration file
  4. Maven local repository is defined in the Maven’s configuration file, for example, {M2_HOME}\conf\setting.xml.
     Edit it “setting.xml”
    Find the following “localRepository” pattern, and define your new Maven local repository inside the “<localRepository>” element like this :
    <settings>
      <!-- localRepository
       | The path to the local repository maven will use to store artifacts.
       |
       | Default: ~/.m2/repository
      <localRepository>/path/to/local/repo</localRepository>
      -->
     
    <localRepository>D:/maven_repo</localRepository>

    Where is maven central repository ?

    While building a Maven’s project, Maven will check your pom.xml file to download the entire project dependency libraries automatically.
    If Maven can’t find the defined dependency libraries in your Maven local repository, it will try to download it from the default Maven central repository, which is http://repo1.maven.org/maven2/.

    How to download from Maven remote repository?

    In Maven, when you need some libraries that are NOT EXITS the Maven center repository, the process will stopped and output error messages to your Maven console.

    To tell Maven to go to Maven remote repository like java.net, you need to declared a “remote repository” in your Maven’s pom.xml file like this : 

    <repositories>
        <repository>
          <id>java.net</id>
          <url>http://download.java.net/maven/2</url>
        </repository>
      </repositories>

    How to use Maven dependency to download library automatically

    Include “Maven coordinates” into “pom.xml” file, under “<dependencies>” tag

    <dependencies>
        <dependency>
     <groupId>log4j</groupId>
     <artifactId>log4j</artifactId>
     <version>1.2.14</version>
        </dependency>
    </dependencies>

    order :
  5. Search log4j in Maven local repository.
  6. Search log4j in Maven central repository.
  7. Search log4j in Maven remote repository (if define in pom.xml)

    Searching maven coordinates

  8. Let say, you want to know the “logback” Maven’s coordinates , you can search it in Google with search text
  9.  logback site:http://repo1.maven.org/maven2/

     


    How to include library manually into maven local repository?

    There are still many Java libraries that are not support for Maven, or may be you want to create a custom library which is required to include it into your Maven local repository.

    Fortunately, Maven comes with command to let you include your “non-maven-support” library into your Maven local repository easily. 

    For example, “kaptcha” is a third party library which is used to generate “captcha” image to stop spamming, but it did not support Maven. 

    1. Install library – mvn install

    mvn install:install-file -Dfile=c:\kaptcha-2.3.jar -DgroupId=com.google.code 
    -DartifactId=kaptcha -Dversion=2.3 -Dpackaging=jar

    2. Modify pom.xml file

    <dependency>
          <groupId>com.google.code</groupId>
          <artifactId>kaptcha</artifactId>
          <version>2.3</version>
    </dependency>

    How to create a Java project with Maven

    To create a Java project with Maven, you can issue this command :

    mvn archetype:generate -DgroupId={packaging.path} -DartifactId={project-id} 
      -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

    1. mvn archetype:generate

    ex:
    mvn archetype:generate -DgroupId=com.sudheer.core -DartifactId=sudheer-core 
      -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

    2. Java Project Structure

    Maven just generated so called “Maven Standard Directory Layout” for your new Java project, and the directory name is same as the given name in “artifactId”.
    In short, puts your source code in src/main/java, resources file in src/main/resources, unit test file insrc/test/java.

    3. pom.xml

    See the generated pom.xml file, the tag “packaging” is “jar“, when build with Maven, this project will group it into a jar file.
    File : pom.xml
    <project xmlns="http://maven.apache.org/POM/4.0.0" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
      http://maven.apache.org/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.mkyong.core</groupId>
      <artifactId>mkyong-core</artifactId>
      <packaging>jar</packaging>
      <version>1.0-SNAPSHOT</version>
      <name>mkyong-core</name>
      <url>http://maven.apache.org</url>
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
    </project>

    How to convert Maven based Java Project to support Eclipse IDE

    In last tutorial, you created a Java project with Maven, but that project is not able to import into Eclipse IDE, because it is not Eclipse style project.
    Here’s a guide to show you how to convert the Maven generated Java project to Eclipse supported style project.

    1. mvn eclipse:eclipse

    mvn eclipse:eclipse

    2. Verify Java Project

    After that, you will notice two new files are created – “.classpath” and “.project“. Both files are created for Eclipse IDE.
    File : .classpath
    <classpath>
      <classpathentry kind="src" path="src/test/java" 
               output="target/test-classes" including="**/*.java"/>
      <classpathentry kind="src" path="src/main/java" including="**/*.java"/>
      <classpathentry kind="output" path="target/classes"/>
      <classpathentry kind="var" path="M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar" 
        sourcepath="M2_REPO/junit/junit/3.8.1/junit-3.8.1-sources.jar"/>
      <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
    </classpath>
    File : .project
    <projectDescription>
      <name>mkyong-core</name>
      <comment/>
      <projects/>
      <buildSpec>
        <buildCommand>
          <name>org.eclipse.jdt.core.javabuilder</name>
        </buildCommand>
      </buildSpec>
      <natures>
        <nature>org.eclipse.jdt.core.javanature</nature>
      </natures>
    </projectDescription>  

    3. Import into Eclipse IDE

    Now, import it into Eclipse IDE, follow below steps :
    In Eclipse IDE, menu bar , File -> Import… -> General -> Existing Projects into Workspace -> select root directory (select your project folder) -> Done.

    How to create a Web Application Project with Maven

    In last tutorial, you created a Java Project with Maven. But that project is not a web application project, to create a web project with Maven, issue this command :
    mvn archetype:generate -DgroupId={packaging.path} -DartifactId={project-id} 
       -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

    1. mvn archetype:generate

    Navigate to the folder you want to generate your web project, e.g “D:\workspace-new\“. In console, issue this command (inside the eclipse workspace)


    mvn archetype:generate -DgroupId=com.sudheer.core -DartifactId=sudheerweb-core 
       -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

    2. Web Application Project Structure

    A new super clean “web.xml” file is created under “D:\workspace-new\sudheerweb-core\src\main\webapp\WEB-INF” folder.
    File : web.xml
    <!DOCTYPE web-app PUBLIC
     "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
     "http://java.sun.com/dtd/web-app_2_3.dtd" >
     
    <web-app>
      <display-name>Archetype Created Web Application</display-name>
    </web-app>

    3. pom.xml

    Refer to the generated pom.xml file, the tag “packaging” is “war“, when build with Maven, this project will group it into a war file for deployment.
    File : pom.xml
    <project xmlns="http://maven.apache.org/POM/4.0.0" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
      http://maven.apache.org/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.mkyong.core</groupId>
      <artifactId>mkyongweb-core</artifactId>
      <packaging>war</packaging>
      <version>1.0-SNAPSHOT</version>
      <name>mkyongweb-core Maven Webapp</name>
      <url>http://maven.apache.org</url>
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
      <build>
        <finalName>mkyongweb-core</finalName>
      </build>
    </project>


    How to convert Maven based web application to support Eclipse IDE

    1. mvn eclipse:eclipse -Dwtpversion=2.0

    To convert a Maven based Java project to support IDE, you use this command :
    For web application, you need extra parameter to make it support Eclipse’s wtp, instead, you should use this command :
    mvn eclipse:eclipse -Dwtpversion=2.0

    2. Eclipse WTP

    Standard Eclipse’s “.classpath” and “.project” files are created. And you will noticed a new “.setting” folder is created and inside contains “org.eclipse.wst.common.component” and “org.eclipse.wst.common.project.facet.core.xml“, both files for WTP or Faces support in Eclipse.
    File : org.eclipse.wst.common.project.facet.core.xml
    <faceted-project>
      <fixed facet="jst.java"/>
      <fixed facet="jst.web"/>
      <installed facet="jst.web" version="2.4"/>
      <installed facet="jst.java" version="1.4"/>
    </faceted-project>
    Note
    Maven 2.x generated web application with JDK1.4 (see above), which is rather outdated, you may need to upgrade it to latest JDK version.
    File : org.eclipse.wst.common.component
    <project-modules id="moduleCoreId" project-version="2.0">
      <wb-module deploy-name="mkyongweb-core">
        <property name="context-root" value="mkyongweb-core"/>
        <wb-resource deploy-path="/" source-path="src/main/webapp"/>
        <property name="java-output-path" value="/target/classes"/>
        <wb-resource deploy-path="/WEB-INF/classes" source-path="src/main/resources"/>
      </wb-module>
    </project-modules>
     

    3. Import into Eclipse IDE

    Now, you have everything what a Eclipse web application want, so, you can start import your Maven based web application into Eclipse IDE.
    Steps :
    In Eclipse IDE, menu bar , File -> Import… -> General -> Existing Projects into Workspace -> select root directory (select your project folder) -> Done.

    How to create a project with Maven template

     

    1. Maven – mvn archetype:generate

    Navigate to folder that you want Maven to generate the project, e.g “E:\workspace\test“, and issue this command “mvn archetype:generate”. Maven will list out exiting 40+ Maven template projects for you to choose.

    How to deploy Maven based war file to Tomcat

    1. Tomcat Authentication

    First, add an user with administrator access right for Tomcat. To add Tomcat user, edit this file – “%TOMCAT_PATH%/conf/tomcat-users.xml“.
    File : tomcat-users.xml
    <?xml version='1.0' encoding='utf-8'?>
    <tomcat-users>
      <role rolename="manager"/>
      <role rolename="admin"/>
      <user username="admin" password="password" roles="admin,manager"/>
    </tomcat-users>

    2. Maven Authentication

    In Maven side, you need to add the same user authentication information in “%MAVEN_PATH%/conf/settings.xml“.
    File : settings.xml
    //...
    <server>
     <id>TomcatServer</id>
     <username>admin</username>
     <password>password</password>
    </server>
    //...

    3. Maven-Tomcat-Plugin

    Declare “Maven-Tomcat plugin” and related Tomcat server detail in your pom.xml file.
    <plugin>
     <groupId>org.codehaus.mojo</groupId>
     <artifactId>tomcat-maven-plugin</artifactId>
     <configuration>
      <url>http://127.0.0.1:8080/manager</url>
      <server>TomcatServer</server>
      <path>/mkyongWebApp</path>
     </configuration>
    </plugin>
    See a full pom.xml file.
    <project xmlns="http://maven.apache.org/POM/4.0.0" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
     http://maven.apache.org/maven-v4_0_0.xsd">
     <modelVersion>4.0.0</modelVersion>
     <groupId>com.mkyong</groupId>
     <artifactId>mkyongweb-core</artifactId>
     <packaging>war</packaging>
     <version>1.0</version>
     <name>mkyongweb-core Maven Webapp</name>
     <url>http://maven.apache.org</url>
     <build>
      <plugins>
       <!-- Maven Tomcat Plugin -->
       <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>tomcat-maven-plugin</artifactId>
        <configuration>
         <url>http://127.0.0.1:8080/manager</url>
         <server>TomcatServer</server>
         <path>/mkyongWebApp</path>
        </configuration>
       </plugin>
       <!-- Maven compiler plugin -->
       <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
         <source>1.6</source>
         <target>1.6</target>
        </configuration>
       </plugin>
      </plugins>
     </build>
    </project>
     

    4. Deploy to Tomcat

    Issue “mvn tomcat:deploy” to package your project in a WAR file, and deploy it to Tomcat server. To verify it, just access to the Tomcat’s manager page and make sure “/mkyongWebApp” path is existed.
     

     

     

     
     

     

     

     

     
     

     

     

     

     

     

     

     

     
     

 




 

No comments:

Post a Comment