Wednesday, June 15, 2011

Arquillian Getting Started difficulties with JBoss AS

If you're looking forward to use Arquillian following "Getting Started" section at:
 http://docs.jboss.org/arquillian/reference/latest/en-US/html_single/#containers.configuration

One thing I would tell you, It's not that easy! It doesn't mean you've better ways to learn about the APIs, the link above is single best source for exploring Arquillian. No offense to community/authors, but Getting started section need to be either updated/framed for a single container.

I got the example working after investing 2-3 days, 3-4 hours/day.. reading from the blogs at communities.

Hope this blog can save some of your time. I'll mainly list out the differences from Getting Started section:


1. You need to place a file "arquillian.xml" at "src/test/resources". This is what it reads for me:

<?xml version="1.0"?>
<arquillian xmlns="http://jboss.com/arquillian"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://jboss.org/schema/arquillian http://jboss.org/schema/arquillian/arquillian_1_0.xsd">

 <container qualifier="jbossas" default="true">
     <configuration>
        <property name="providerUrl">jnp://localhost:1099</property>
     </configuration>
     <protocol type="Servlet 3.0">
        <configuration>
            <property name="host">localhost</property>
            <property name="port">8080</property>
        </configuration>
     </protocol>
 </container>
</arquillian>

2. Below is important sections from my Project's POM.

I'm using jboss-as-client 6.0 M1, because I got some errors (If i remember correctly there were some problem with POMs while downloading woodstox jar, saaj-apis etc..) while maven tried to download dependencies for jboss-as-client 6.0.Final.

    <profiles>
        <profile>
            <id>jbossas-remote-6</id>
            <dependencies>
                <dependency>
                    <groupId>org.jboss.jbossas</groupId>
                    <artifactId>jboss-as-profileservice-client</artifactId>
                    <version>6.0.0.Final</version>
                    <type>pom</type>
                </dependency>
                <dependency>
                    <groupId>org.jboss.arquillian.container</groupId>
                    <artifactId>arquillian-jbossas-remote-6</artifactId>
                    <version>${arquillian.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.jboss.jbossas</groupId>
                    <artifactId>jboss-as-client</artifactId>
                    <version>6.0.0.M1</version>
                    <type>pom</type>
                </dependency>
            </dependencies>
        </profile>
    </profiles>

2. You need to use Shrinkwrap alpha-12.

        <dependency>
            <groupId>org.jboss.shrinkwrap</groupId>
            <artifactId>shrinkwrap-api</artifactId>
            <version>1.0.0-alpha-12</version>
        </dependency>

3. You need to use one more repository:

        <repository>
            <id>jboss-deprecated-public-repository-group</id>
            <name>JBoss Deprecated Public Maven Repository Group</name>
            <url>https://repository.jboss.org/nexus/content/repositories/deprecated/</url>
            <layout>default</layout>

Now I think you should be set.

Start JBoss, execute your test cases and you should see below messages at JBoss console:


Hope this helps. Drop a comment, If you've any question.


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:

Wednesday, February 9, 2011

Drawing ANT build file

Sometimes best way to learn about any new Open source Framework, APIs, Tools etc is to look in their source code. Documentation does help, but most of the time, specially for Open source software they fall short of their purpose.

I usually start by looking at build script, following different targets..corresponding source code and so on. For some of the Frameworks...let's say Log4J, you'll see plenty of targets in build file..It does not stop there, those targets refer other targets that reside in some other build file and it goes on.

Tracking all the build dependency by looking at the build file becomes very difficult, especially for the projects having more than 50-60 targets.

I’m going to show you how you can prepare a diagram for your build file.

I will be using Vizant and GraphViz for this purpose.
1)      Download Vizant jar file from sourceforge
2)      Install GraphViz. You can get .msi file from GraphViz web site. Check this URL for downloading installer. http://www.graphviz.org/Download_windows.php

Once you’re done installing the GraphViz, let’s use a sample build file to see, how it works.

build.xml  

<project name="HelloWorld" basedir="." default="main">
    <property name="src.dir" value="src"/>
    <property name="build.dir" value="build"/>
    <property name="classes.dir" value="${build.dir}\classes"/>
    <property name="jar.dir" value="${build.dir}\jar"/>
    <property name="main-class" value="oata.HelloWorld"/>

    <target name="clean">
        <delete dir="${build.dir}"/>
    </target>
   
    <target name="compile">
        <mkdir dir="${classes.dir}"/>
        <javac srcdir="${src.dir}" destdir="${classes.dir}"/>
    </target>
   
    <target name="jar" depends="compile">
        <mkdir dir="${jar.dir}"/>
        <jar basedir="${classes.dir}"
             includes="**\*.class"
             destfile="${jar.dir}\${ant.project.name}.jar">
        <manifest>
            <attribute name="Main-Class"
                value="${main-class}"/>
        </manifest>
        </jar>
    </target>
   
    <target name="run" depends="jar">
        <java jar="${jar.dir}\${ant.project.name}.jar" fork="true"/>
    </target>
   
    <target name="clean-build" depends="clean,jar"/>
   
    <target name="main" depends="clean,run"/>
</project>

3) I will now be adding a new target for preparing an image for this build file.

First I will define a new task:
    <taskdef name="vizant" classname="net.sourceforge.vizant.Vizant" classpath="lib/vizant.jar"/>
    
You can keep vizant.jar in whatever location you want on your local system, I kept it in /lib folder.

Next step, is to add a target that you can invoke for creating a pictorial representation of your build file.

    <target name="draw">
        <vizant antfile="build.xml" outfile="build.dot"/>
        <exec executable="dot" ><arg line="-Tpng build.dot -o build.png"/></exec>
    </target>

    
4) Once you add these lines to your build file, type

D:\Project-RND\RAndD>ant draw

You will see usual output on command window.

Buildfile: D:\Project-RND\RAndD\build.xml
draw:
BUILD SUCCESSFUL
Total time: 0 seconds

5) Now check your project folder, you will be seeing two new files: build.dot and build.png

I'm attaching build.png for your reference, and you can now see target dependency tree.


For more information, check this URL: http://vizant.sourceforge.net/#parameters

Happy reading. Leave a comment, if you've any question.