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:

Create Maven Plugin

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.

You also need to have good understanding of Maven lifecycle and plugins to really benefit from this post. You can refer below link to know more about Maven:
You can also check out Maven site for more details.
A plugin can have several MOJOs, where each MOJO refers to a goal. So when you type
$ mvn jar:jarGoal
That means you’re trying to execute ‘jarGoal’ from ‘jar’ plugin.
Based on my experience, I believe existing plugins offered by Maven will be more than enough for your project needs; however that does not stops you from writing your own plugin J and we’re here for the same.


Your first step will be to create a plug in project.

-          Create Plugin Project

To create a plugin project, you should use the Maven Archetype plugin. The following command-line will create a plugin with a groupId of org.sonatype.mavenbook.plugins and the artifactId of first-maven-plugin:

$ mvn archetype:create \
-DgroupId=org.sonatype.mavenbook.plugins \
-DartifactId=first-maven-plugin \
-DarchetypeGroupId=org.apache.maven.archetypes \
-DarchetypeArtifactId=maven-archetype-mojo

The Archetype plugin is going to create a directory named my-first-plugin which contains the following POM.
<?xml version="1.0" encoding="UTF-8"?>
<project>
      <modelVersion>4.0.0</modelVersion>
      <groupId>org.sonatype.mavenbook.plugins</groupId>
      <artifactId>first-maven-plugin</artifactId>
      <version>1.0-SNAPSHOT</version>
      <packaging>maven-plugin</packaging>
      <name>first-maven-plugin Maven Mojo</name>
      <url>http://maven.apache.org</url>
      <dependencies>
            <dependency>
                  <groupId>org.apache.maven</groupId>
                  <artifactId>maven-plugin-api</artifactId>
                  <version>2.0</version>
            </dependency>
            <dependency>
                  <groupId>junit</groupId>
                  <artifactId>junit</artifactId>
                  <version>3.8.1</version>
                  <scope>test</scope>
            </dependency>
      </dependencies>
</project>

-          Create MOJO(or goal)
Mojo class is nothing but description of your Goal. You could write this class in Java or any of the scripting language like Groovy, BeanShell, Ruby etc.
Each Mojo class is going to implement the org.apache.maven.plugin.Mojo interface, the Mojo implementation shown in our example implements the Mojo interface by extending the org.apache.maven.plugin.AbstractMojo class.

The Mojo interface is concerned with two things: logging the results of goal execution and executing a goal. When you are writing a custom plugin, you'll be extending AbstractMojo. AbstractMojo takes care of handling the setLog() and getLog() implementations and contains an abstract execute() method. When you extend AbstractMojo, all you need to do is implement the execute() method.

package org.sonatype.mavenbook.plugins;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;

/**
 * Echos an object string to the output screen.
 *
 * @goal echo
 * @requiresProject false
 */
public class EchoMojo extends AbstractMojo {
                /**
                 * Any Object to print out.
                 *
                 * @parameter expression="${echo.message}" default-value="Hello World..."
                 */
                private Object message;

                public void execute() throws MojoExecutionException, MojoFailureException {
                                getLog().info(message.toString());
                }
}

Other thing to note is that, we have also provided our goal a name: ‘echo’ (with annotation on top @goal echo), and also mentioned that our Mojo (or goal) accepts a parameter echo.message whose default value is ‘Hello World’.
That means, while executing our plugin’s goal (i.e. echo), we could write:
$mvn org.sonatype.mavenbook.plugins:first-maven-plugin:1.0-SNAPSHOT:echo –Decho.message=Print me
And on console you could see a message : Print me
Specifying the groupId, artifactId, version, and goal on the command-line is cumbersome. What we want is to execute our goal with simple command like
$mvn first:echo
To achieve this, we need to tell Maven that ‘first’ actually means org.sonatype.mavenbook.plugins:first-maven-plugin:1.0-SNAPSHOT”, and this mapping need to be done in your ~/.m2/settings.xml file.

In your ~/.m2/settings.xml, add this line:
<pluginGroups>
<pluginGroup>org.sonatype.mavenbook.plugins</pluginGroup>
</pluginGroups>

That’s it! You’re done!
Now on command line, if you type:
$mvn first:echo
You can see ‘Hello World…’ on console.
This worked because our project adhered to a naming convention for Maven plugins. If your plugin project has an artifactId which follows the pattern maven-first-plugin or first-maven-plugin, maven will automatically assign a plugin goal prefix of first to your plugin. Well, you’re also free to customize any name to your plugin, so instead of first:echo, you could also execute your goal with second:echo or whatever you like. Check maven-plugin-plugin Plugin for more details.

References: