Monday, October 14, 2013

Get started with Maven

Create Maven web project
$mvn archetype: generate -DgroupId=com.mycompany.app -DartifactId=my-webapp -DarchetypeArtifactId=maven-archetype-webapp
Overview of 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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <!-- The Basics -->
  <groupId>...</groupId>
  <artifactId>...</artifactId>
  <version>...</version>
  <packaging>...</packaging>
  <dependencies>...</dependencies>
  <parent>...</parent>
  <dependencyManagement>...</dependencyManagement>
  <modules>...</modules>
  <properties>...</properties>

  <!-- Build Settings -->
  <build>...</build>
  <reporting>...</reporting>

  <!-- More Project Information -->
  <name>...</name>
  <description>...</description>
  <url>...</url>
  <inceptionYear>...</inceptionYear>
  <licenses>...</licenses>
  <organization>...</organization>
  <developers>...</developers>
  <contributors>...</contributors>

  <!-- Environment Settings -->
  <issueManagement>...</issueManagement>
  <ciManagement>...</ciManagement>
  <mailingLists>...</mailingLists>
  <scm>...</scm>
  <prerequisites>...</prerequisites>
  <repositories>...</repositories>
  <pluginRepositories>...</pluginRepositories>
  <distributionManagement>...</distributionManagement>
  <profiles>...</profiles>
</project>

What is groupId in maven ?
groupId identifies a particular project uniquely across all projects, so we should follow an naming convention. A very simple and commonly used way of doing this is to use reverse of your domain,

A good way of maintaining the integrity of groupId is to use the project structure. In case the project is consists of multiple modules than every module should append an identifier to the parent groupId. i.e. com.beingjavaguys.maven, com.beingjava guys. Spring, com.beingjavaguys.struts .. etc.
What is artifactId in maven?
ArtifactId is the name of war file without version, if you are creating it by yourself you are free to took any name of your choice in lower case and without any strange symbol. But if this is a third party jar than we have to take the name of jar as suggested by it’s distribution.

What is archetype in maven?
Archetype is a Maven project templating toolkit which tells the maven the type of project we are going to create. Archetype enables the maven to create a template project of user’s choice so that the user can get the project up and running instantly.
“archetype: generate” generates a new project from provided archetype or update the actual project if using a partial archetype. Maven provides a number of predefined archetypes, see more from
.

What is archetype ArtifactId in maven?
While creating a new project we provide the archetype. ArtifactId that informs maven about what archetype to use to create the initial structure of the project. Maven looks it up from the archetype Catalog and works accordingly. E.g. If we want to create a simple web-app project we specify -DarchetypeArtifactId=maven-archetype-webapp.
Maven java docs:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-source-plugin</artifactId>
  <executions>
    <execution>
      <id>attach-sources</id>
      <goals>
        <goal>jar</goal>
      </goals>
    </execution>
  </executions>               
</plugin>

         <plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-javadoc-plugin</artifactId>
  <executions>
    <execution>
      <id>attach-javadocs</id>
      <goals>
        <goal>jar</goal>
      </goals>
    </execution>
  </executions>
</plugin>
To download and attach java Doc from command line
     mvn eclipse:eclipse -DdownloadSources=true  -DdownloadJavadocs=true

Directory Structure:
<!-- Directory Structure -->
             <directory>${project.basedir}/target</directory>
              <outputDirectory>${project.build.directory}/classes</outputDirectory>
              <testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
              <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
              <finalName>SpringMVCDemo</finalName>
             <!-- TODO: MNG-3731 maven-plugin-tools-api < 2.4.4 expect this to be relative... -->
              <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>
              <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
             <resources>
                   <resource>
                        <directory>${project.basedir}/src/main/resources</directory>
                    </resource>
              </resources>
              <testResources>
                    <testResource>
                          <directory>${project.basedir}/src/test/resources</directory>
                     </testResource>
              </testResources>

              <!-- Maven Tomcat -->
              <plugin>
                     <groupId>org.apache.tomcat.maven</groupId>
                      <artifactId>tomcat7-maven-plugin</artifactId>
                       <version>${tomcat7-maven-version}</version>
                        <configuration>
                             <url>http://localhost:8080/manager/text</url>
                              <server>TomcatServer</server>
                         </configuration>
                </plugin>

No comments:

Post a Comment