Monday, March 7, 2011

Create Maven Site

Your first step in order to play with Maven will be to download Maven and install the same. It’s very simple and you just need to follow instructions available here:


I’m using Maven 3.

Now we need to create a project, for which you need to execute below command:

mvn archetype:generate
-DgroupId=com.mycompany.app
-DartifactId=my-project
-DarchetypeArtifactId=maven-archetype-quickstart
-DinteractiveMode=false

Archetype allows you to create your project, and there is several archetypes available for creating web application, command line application and many more. I’ve barely scratched the surface.

Once you run above command, maven will create a project and you can see the set of instructions getting executed command window.



In your project directory (my-project) , you could see your POM file.

<project ….">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-project</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>my-project</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>
The pom file is core of your project’s configuration. It is a single configuration file that contains the majority of information required to build a project in just the way you want.

Let’s add some more information about our project.

-          Adding License details.
Paste below code snippet in your POM file (just before <dependencies> element starts.

  <licenses>
        <license>
                        <name>Apache 2</name>
                        <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
                        <distribution>repo</distribution>
                        <comments>A business-friendly OSS license</comments>
        </license>
  </licenses>

-          Add organization details
You can also add details about your organization (paste below content after <licenses> details). We usually do this after adopting Maven at corporate level.

  <organization>
        <name>Mix and Match Organization</name>
        <url>http://www.mixandmatch.com</url>
  </organization>

-          Add team details
You could also add your team information, member’s name, timezone etc.

  <developers>
        <developer>
                        <id>Vicky</id>
                        <name>Vicky Sharma</name>
                        <email>solid.api@gmail.com</email>
                        <url> http://www.mixandmatchblogs.com/vicky </url>
                        <organization>Mix And Match</organization>
                        <organizationUrl> http://www.mixandmatch.com </organizationUrl>
                        <roles>
                                        <role>developer</role>
                        </roles>
                        <timezone>+5</timezone>
        </developer>
  </developers>



-          Add site plugin (after <dependencies> section)
Actually you do not need to add this plugin, but there is a bug with Maven 3 that needs this plugin to be added in your project’s POM.

<build>
        <plugins>
                        <plugin>            
                                        <groupId>org.apache.maven.plugins</groupId>            
                                        <artifactId>maven-site-plugin</artifactId>            
                                        <version>3.0-beta-3</version>        
                        </plugin>           
        </plugins>
</build>

-          Allow the project to create Javadoc for our artifacts:

To do this, add another plugin in <reporting> section (after <build>)

  <reporting>    
        <plugins>        
                        <plugin>            
                                        <groupId>org.apache.maven.plugins</groupId>            
                                        <artifactId>maven-javadoc-plugin</artifactId>            
                                        <version>2.7</version>            
                        </plugin>    
        </plugins>
  </reporting>


Ok.. Enough details added J

Now let’s run our site.

On command prompt type: mvn site:run
If you have just installed Maven, it may take a while on the first run. This is because Maven is downloading the most recent artifacts (plugin jars and other files) into your local repository. You may also need to execute the command a couple of times before it succeeds. This is because the remote server may time out before your downloads are complete.
Once our site starts running, command window appears like screen below:



Now type http://localhost:8080 in your browser and you will be able to see your newly created site.



Click on various links and Maven will display you appropriate details; for example when I clicked on ‘Dependencies’, I could see all the details about dependencies configured in our project.


You can check Project reports navigation to see Javadoc.

Maven internally uses jetty to run this web site.

This is not enough to actually learn how to create sites with Maven. You can refer Maven site or some of the links mentioned below for detailed information.

References:

No comments:

Post a Comment