Wednesday, April 3, 2013

Maven Deploy to Oracle Weblogic. Step By Step. The Basics. Part 1/4

Hi,
This is a series of  four (4) articles that will describe how to use Maven and deploy a sample application to Weblogic.

Contents of each part:

There are no assumptions here and we are going to do everything from scratch.

There is always a person out there that will have to use those technologies together without knowing any of them.

Maven 
What is Maven? Well, the official documentation says the following:

Maven, a Yiddish word meaning accumulator of knowledge, was originally started as an attempt to simplify the build processes in the Jakarta Turbine project. There were several projects each with their own Ant build files that were all slightly different and JARs were checked into CVS. We wanted a standard way to build the projects, a clear definition of what the project consisted of, an easy way to publish project information and a way to share JARs across several projects.
The result is a tool that can now be used for building and managing any Java-based project. We hope that we have created something that will make the day-to-day work of Java developers easier and generally help with the comprehension of any Java-based project.

http://maven.apache.org/what-is-maven.html

In other words and this is how it helps me remember it,  maven is a repository that holds the dependencies your project needs. Not only that, but you can include your projects in the repository and you can reuse them as dependencies to other projects.

The above is not perfect and certainly does not cover everything, but it provides the general idea.

So, whats with the repositories?  Well, a repository is a place where all your dependencies are stored. According to the official documentation (content extracted from http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html):
Dependency management is one of the features of Maven that is best known to users and is one of the areas where Maven excels. There is not much difficulty in managing dependencies for a single a project, but when you start getting into dealing with multi-module projects and applications that consist of tens or hundreds of modules this is where Maven can help you a great deal in maintaining a high degree of control and stability.

For example:
Lets say that you want a new house.. right? So you want a new house. The new house is your project.
But, a new house has more needs to be implemented, some of them are the following:
money, builders, mortgages, partner's approval and so on..
Each of those needs is called a dependency and each dependency has more needs (dependencies) 
With Maven, most of those dependencies are resolved automagicaly through the repositories! (except the partner of course...  :) )

In a more complex example where we would have to have 15 different houses. Your project now is to build 15 different houses. Maven would take care of the dependencies! Lets say we want a villa, a flat, a house and a two bed appartment and so on (use your imagination). As you can imagine, each house depends on different things.. This is where Maven comes to play.. It knows about those dependencies for every type of house and resolves them automatically. Additionally, we can provide more dependencies for each house depending on our taste! Maven will determine the dependencies of the new dependency and everything will be great and we will live happily ever after!
Well, there are cases where Maven cannot resolve all the dependencies and we will have to do it on our own..  but even in this case, things are quite simple.

I wish my life was that simple! xD

Again, this is not the definitive guide for Maven and it is not going to be. This article will give the general idea of Maven by describing some of it's key features in order to help complete our task which is to deploy a sample application to WebLogic, using Maven.

Maven needs JAVA. Since Maven is built for java applications you need to have java  in order to install and run Maven.


Maven creates Maven Projects. Yes, that is true.. with Maven you create a Maven project that has specific structure and is ready to be configured. So you can create a Maven project for a java application or for java web application or for java enterprise application etc and  you are ready to start developing.
How do you do that and why should you do that? The how is easy, by executing specific goals! (comming up, look below). Maven uses plugins and goals that really help in creating project structures based on conventions. So you dont have to worry much about


Maven has goals. Maven  works with goals. Each goal or a set of goals can serve a specific purpose. Imagine goals as a part of a bigger process. We can have standalone goals and we can have goals that depend on other goals. For example, a goal called test  of the SureFile plugin(comming up, look below) can be used to run unit tests. The create goal of the archetype plugin can be used to create a maven project. 


Maven has plugins. Anyone can create maven plugins. A plugin is a set of goals (look above) that serve a specific purpose. For example, the following link describes the SureFire plugin
http://maven.apache.org/surefire/maven-surefire-plugin/
As you will see in the link, SureFire has only one goal called test .
This is how SureFile plugin is designed. It has only one goal. So, we can call that goal of that plugin and it will do it's job. Of course plugins can and quite often do, accept configuration parameters. Those parameters determine the execution process of the plugin's goals. We wont dive into the details of configuration parameters since it is out of scope of this article. It is enough to know that plugin goals accept configuration parameters that can determine their execution process.


Maven has phases. Maven has a list of phases. That list is called build lifecycle. From the official documentation (content extracted from http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html), we can see the default lifecycle phases:

  • validate - validate the project is correct and all necessary information is available
  • compile - compile the source code of the project
  • test - test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
  • package - take the compiled code and package it in its distributable format, such as a JAR.
  • integration-test - process and deploy the package if necessary into an environment where integration tests can be run
  • verify - run any checks to verify the package is valid and meets quality criteria
  • install - install the package into the local repository, for use as a dependency in other projects locally
  • deploy - done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.
a more detailed information on the lifecycle phases can be found here:
http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference

Why do we need to know about those lifecycle phases? Simply because those phases will help us understand how to use Maven when we will deploy our project to weblogic.

The basic thing to remember here is that we can call any phase we want knowing that all previous phases will be executed prior the one we want. So for instance,  if we call  compile phase, we know that validate phase will be called prior compile  phase.


Installing Maven on Windows
Before installing Maven, make sure that you have installed JAVA (JDK to be exact since JRE will not be enough).
http://www.oracle.com/technetwork/java/index.html

To install Maven is fairly easy..  Download it from here: http://maven.apache.org/download.cgi
On the same page, it describes how to install it on windows. The below content is extracted from the official url:
  1. Unzip the distribution archive, i.e. apache-maven-3.0.5-bin.zip to the directory you wish to install Maven 3.0.5. These instructions assume you chose C:\Program Files\Apache Software Foundation. The subdirectory apache-maven-3.0.5 will be created from the archive.
  2. Add the M2_HOME environment variable by opening up the system properties (WinKey + Pause), selecting the "Advanced" tab, and the "Environment Variables" button, then adding the M2_HOME variable in the user variables with the value C:\Program Files\Apache Software Foundation\apache-maven-3.0.5. Be sure to omit any quotation marks around the path even if it contains spaces. Note: For Maven   2.0.9, also be sure that the M2_HOME doesn't have a '\' as last character.
  3. In the same dialog, add the M2 environment variable in the user variables with the value %M2_HOME%\bin.
  4. Optional: In the same dialog, add the MAVEN_OPTS environment variable in the user variables to specify JVM properties, e.g. the value -Xms256m -Xmx512m. This environment variable can be used to supply extra options to Maven.
  5. In the same dialog, update/create the Path environment variable in the user variables and prepend the value %M2% to add Maven available in the command line.
  6. In the same dialog, make sure that JAVA_HOME exists in your user variables or in the system variables and it is set to the location of your JDK, e.g. C:\Program Files\Java\jdk1.5.0_02 and that %JAVA_HOME%\bin is in your Path environment variable.
  7. Open a new command prompt (Winkey + R then type cmd) and run mvn --version to verify that it is correctly installed.
In a nutshell, after installing Maven, we have to setup some user environment variables to have it working on our Windows machine.
The most important part is t number 5:

In the same dialog, update/create the Path environment variable in the user variables and prepend the value %M2% to add Maven available in the command line.

And there you go! you have an accumulator of knowledge on your Windows machine!
If you want to learn more about maven and discover it's power, I strongly recomend the official documentation of maven along with the great books out there.
By now, we have a very basic understanding of what is Maven and it can do for us. We havent created any maven project yet simply because we are going to to do in Part 3  of this series. Why not now? Because I want you to try it on your own and see how is it is! If you are so eager to see how it is done, jump to part 3 here

The Next Part (part 2) of this series is about Weblogic .

References:
http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html
http://maven.apache.org/archetype/maven-archetype-plugin/
http://shop.oreilly.com/product/9780596517335.do
http://maven.apache.org/plugins/index.html
http://code.google.com/hosting/search?q=maven+plugin+label%3Amaven&projectsearch=Search+Projects 
http://maven.apache.org/guides/mini/guide-configuring-plugins.html
http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html


1 comment:

LinkWithin

Related Posts Plugin for WordPress, Blogger...