Tuesday, April 10, 2012

Maven Starter Guide

  • Enviroment
SET M2_HOME=%DEVAPPS_HOME%/apache-maven-3.0.3
SET PATH=%JAVA_HOME%/bin;%M2_HOME%/bin;
SET MAVEN_CLASSPATH=%M2_HOME%/lib/maven-core-3.0.3.jar;%M2_HOME%/lib/maven-model-3.0.3.jar;%M2_HOME%/lib/maven-plugin-api-3.0.3.jar;%M2_HOME%/lib/maven-repository-metadata-3.0.3.jar;%M2_HOME%/lib/maven-artifact-3.0.3.jar;

  • Project Creation
c:\> mvn archetype:generate
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 186:
Choose org.apache.maven.archetypes:maven-archetype-quickstart version:
1: 1.0-alpha-1
2: 1.0-alpha-2
3: 1.0-alpha-3
4: 1.0-alpha-4
5: 1.0
6: 1.1
Define value for property 'groupId' (package name): : org.freecode.demo
Define value for property 'artifactId' (project name): :Demo1
Define value for property 'version':  1.0-SNAPSHOT: :1.0
Define value for property 'package':  org.freecode.demo: :
Confirm properties configuration:
groupId: org.freecode.demo
artifactId: Demo1
version: 1.0
package: org.freecode.demo
 Y: :



    a Project file of Maven (pom.xml) is generated.

      <groupId>org.freecode.demo</groupId>
      <artifactId>Demo1</artifactId>
      <version>1.0</version>
      <packaging>jar</packaging>
      <name>Demo1</name>
      <url>http://maven.apache.org</url>
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <junit.version>4.11</junit.version>
      </properties>
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>${junit.version}</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
    </project>
    
  • Structure of Maven Project
  • Demo1
    |-- pom.xml
    |-- src
    |   |-- main
    |   |   |-- assembly
    |   |   |-- config
    |   |   |-- filters
    |   |   |-- java
    |   |   |   `-- org
    |   |   |       `-- freecode
    |   |   |           `-- demo
    |   |   |               `-- App.java
    |   |   |-- resources
    |   |   `-- webapp
    |   |       |-- index.jsp
    |   |       `-- WEB-INF
    |   |           |-- web.xml
    |   |           `-- classes
    |   |               `-- org
    |   |                   `-- freecode
    |   |                       `-- demo
    |   |                           `-- App.class   
    |   `-- test
    |   |   `-- java
    |   |       `-- org
    |   |           `-- freecode
    |   |               `-- demo
    |   |                   `-- AppTest.java
    |   `-- site
    |
    `-- target


  • Manually Add Dependencies
C:\> call mvn install:install-file -Dfile=ojdbc5.jar -DgroupId=com.oracle -DartifactId=ojdbc -Dversion=5.0 -Dpackaging=jar
Update pom.xml by adding the custom dependencies
e.g.
<dependencies> …
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc</artifactId>
        <version>5.0</version>
    </dependency>
</dependencies>

  • Project Compile, Build, Package
to clean a project, use:
  c:\Demo1> mvn clean
to package a project:
  c:\Demo1> mvn package

  • Build Plugin - Compiler
<build>
    <finalName>war file name</finalName>
    <resources>…</resources>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
             <configuration>
                 <source>1.7</source>
                 <target>1.7</target>
             </configuration>
        </plugin>
        … …
    </plugins>
</build>

  • Build Plugin - WAR Package (for JBoss/Wildfly)
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <webResources>
                    <resource>
                        <filtering>true</filtering>
                        <directory>src/main/webapp/WEB-INF</directory>
                        <includes>    <include>jboss-web.xml</include>   </includes>
                        <targetPath>/WEB-INF</targetPath>
                    </resource>
                </webResources>
                <outputDirectory>c:/jbossas-8.1.0.Final/standalone/deployments</outputDirectory>
            </configuration>
        </plugin>
    </plugins>
</build>

  • Profiles per Environment
    <profiles>
        <profile>
        <id>dev</id>
        <properties>
            <application.environment>Development</application.environment>
            <environment.id>dev</environment.id>
            <context.path.name>/dev</context.path.name>
            <jta.data.source.poweron>PoweronDEV</jta.data.source.poweron>
            … …
        </properties>
    </profile>

    run command: C:\> mvn clean package –P dev
  • Project Deployment (Glassfish)
to deploy a project to Glassfish server, you need to add Glassfish plugin and some dependencies into pom.xml.

to find a dependency or plugin, you can search from http://mvnrepositorycom/

- change package type to war
<packaging>war</packaging>

 
- Servlet 2.5
<dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>servlet-api</artifactId>
   <version>2.5</version>
</dependency>


- build section with war file and Glassfish plugins
<build>
  <finalName>XProjectDemo</finalName>
  <!-- <directory>C:/EclipseWork/XProjectDemo/target</directory> -->
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
     <source>1.6</source>
     <target>1.6</target>
    </configuration>
   </plugin>
   <plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.1.1</version>
   </plugin>
   <plugin>
    <groupId>org.glassfish.maven.plugin</groupId>
    <artifactId>maven-glassfish-plugin</artifactId>
    <version>2.1</version>
    <configuration>
     <debug>true</debug>
     <echo>true</echo>
     <user>your Glassfish admin user</user>
     <adminPassword>your Glassfish admin password</adminPassword>
     <glassfishDirectory>C:/DevApps/glassfish3</glassfishDirectory>
     <components>
      <component>
       <name>${project.artifactId}</name>
       <artifact>${project.build.directory}/${project.build.finalName}.war</artifact>
      </component>
     </components>
     <domain>
      <name>${project.artifactId}</name>
      <adminPort>4848</adminPort>
      <httpPort>8989</httpPort>
      <httpsPort>8181</httpsPort>
     </domain>
    </configuration>
   </plugin>
  </plugins>
 </build>


- Maven-Glassfish commands (http://maven-glassfish-plugin.java.net/)
  • glassfish:create-domain Create a new Glassfish domain. (Creating an existing domain will cause it to be deleted and recreated.)
  • glassfish:start-domain Start an existing Glassfish domain. (Starting a non-existent domain will cause it to be created.)
  • glassfish:deploy Deploy JavaEE artifacts to a running domain. (Deploying to an inactive domain will cause it to be started and created if necessary.)
  • glassfish:redeploy Redeploy JavaEE artifacts to a running domain. (Cold redeployment by first calling undeploy and then deploy . Use deploy to effect a hot deployment.)
  • glassfish:undeploy Undeploy JavaEE components from a running domain.
  • glassfish:stop-domain Stop a running Glassfish domain.
  • glassfish:delete-domain Delete an existing Glassfish domain.