Wednesday, April 3, 2013

Maven Deploy to Oracle Weblogic. Step By Step. The Basics. Part 3/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.

So far we have learned about Maven and Weblogic. So far we have no connection between those two tools.
In this article, we are going to create our applications using mvn in three ways.
One with the cmd prompt by executing  maven commands.
One using Netbeans 7.3

In case you need a small refresher about Maven and the general idea behind it, jump to Part 1 before continuing.

 In our case, we are going to keep things quite simple. We are just going to use a java web application with one jsp saying Hello World! Nothing more.
As mentioned earlier, we are going to create three projects in three different ways.

Maven command line.
In order to create a new project, first make sure that you have maven installed. On a Windows machine, open the command prompt and type mvn --version
If everything is ok, you will something similar to this:

Apache Maven 3.0.5 (r01de14724cdef164cd33c7c8c2fe155faf9602da; 2013-02-19 13:51:
28+0000)
....

Next, using the command line, navigate to a folder (or create one..) that will store our sample projects.
In this example we are going to use the following path
C:\Users\Dimitrios\mavenToWeblogic>
 Now we are ready to fly!

 As we found out in part one, Maven has plugins with goals. Additionally, we create Maven based projects. But how?

Archetype plugin comes to play.
According to the official documentation
(content extracted from http://maven.apache.org/guides/introduction/introduction-to-archetypes.html )

In short, Archetype is a Maven project templating toolkit. An archetype is defined as an original pattern or model from which all other things of the same kind are made. The names fits as we are trying to provide a system that provides a consistent means of generating Maven projects. Archetype will help authors create Maven project templates for users, and provides users with the means to generate parameterized versions of those project templates.
........
You may want to standardize J2EE development within your organization so you may want to provide archetypes for EJBs, or WARs, or for your web services. Once these archetypes are created and deployed in your organization's repository they are available for use by all developers within your organization.



 As you can see, the Archetype plugin helps us create maven project based on some predefined templates. In other words, it creates the project for us with a specific structure. This is very helpful when it comes to large application where a lot of projects have to be created.

Can we create our own archetypes?
Yes we can.

Are we going to create our own archetype?
No we wont. But here is the official documentation http://maven.apache.org/guides/mini/guide-creating-archetypes.html

Why not? We dont itend to replace the official documentation here.. We just learn what we need to perform our task which is to deploy a maven application to weblogic using maven.

Maven has a long list of archetypes. In order to see that list type the following on your command prompt:

mvn archetype:generate

First you will see that maven is downloading the archetype plugin and then is resolving any dependences for that plugin and goal. Next it is going to print a very long list of archetypes. At the end, it will ask you to choose which one you want to use.
Sure, you can go through the list and pick one, as you see it has a default one with number 254.
For more information check:
http://maven.apache.org/archetype/maven-archetype-plugin/usage.html

In our case, we want to create a webapp.  Since we already have a very long list of archetypes, how are we going to find the correct number?
One way is to  find the right number from the list.
 Another way is to re-filter the list by providing some keywords. Instead of number, try typing
archetype-webapp and hit enter.

The above will narrow down the list. However most probably you will see more than one options. The one we are looking for is:

 remote -> org.apache.maven.archetypes:maven-archetype-webapp (An archetype
which contains a sample Maven Webapp project.)
So, hit the number you see for that option, or, re-filter it by typing
maven-archetype-webapp  and hit enter.

This will give only one option with number 1.
Type 1 and hit enter.

Next you will be asked for the version of the archetype.
 Choose the latest stable one.
 By the time of this writting, 1.0 was the latest version.
Type the number of the option and hit enter.


Next, the process will ask you to specify a groupId.

What is a groupId?
According to the official documenation
( From http://maven.apache.org/guides/getting-started/index.html#How_do_I_make_my_first_Maven_project )
groupId This element indicates the unique identifier of the organization or group that created the project. The groupId is one of the key identifiers of a project and is typically based on the fully qualified domain name of your organization. For example org.apache.maven.plugins is the designated groupId for all Maven plug-ins.

( From  http://maven.apache.org/pom.html )
groupId: This is generally unique amongst an organization or a project. For example, all core Maven artifacts do (well, should) live under the groupId org.apache.maven. Group ID's do not necessarily use the dot notation, for example, the junit project. Note that the dot-notated groupId does not have to correspond to the package structure that the project contains. It is, however, a good practice to follow. When stored within a repository, the group acts much like the Java packaging structure does in an operating system. The dots are replaced by OS specific directory separators (such as '/' in Unix) which becomes a relative directory structure from the base repository. In the example given, the org.codehaus.mojo group lives within the directory $M2_REPO/org/codehaus/mojo.

So in other words, we have to provide a unique name for our organization.
Since this is an example, we have an example organization.

Type com.organization.example and hit enter.

After that is done, the process asks for an artifactId

What is an artifactId?
Well, according to the official documentation
(from http://maven.apache.org/guides/getting-started/index.html#How_do_I_make_my_first_Maven_project )
artifactId This element indicates the unique base name of the primary artifact being generated by this project. The primary artifact for a project is typically a JAR file. Secondary artifacts like source bundles also use the artifactId as part of their final name. A typical artifact produced by Maven would have the form -. (for example, myapp-1.0.jar).
(from maven.apache.org/pom.html )
artifactId: The artifactId is generally the name that the project is known by. Although the groupId is important, people within the group will rarely mention the groupId in discussion (they are often all be the same ID, such as the Codehaus Mojo project groupId: org.codehaus.mojo). It, along with the groupId, create a key that separates this project from every other project in the world (at least, it should :) ). Along with the groupId, the artifactId fully defines the artifact's living quarters within the repository. In the case of the above project, my-project lives in $M2_REPO/org/codehaus/mojo/my-project.

So in our case,  the artifact will be:
CmdMvnApp . Hit enter.

Next, the process will ask you for the version of the project. Since this is an example, we can leave it as is.
Just hit enter,

Next, the process will ask you for the package.

The screen already defines the default value of the package option.


 the package value is optional and  it represents
Next, it shows a list with our options for confirmation:





Hit Enter to proceed.

Maven will create the project and it will notify us upon completion.



And thats it!! We have just created a Maven application from the command line!!
If you go and check your mavenToWeblogic folder(or the name you have specified) you will see that we have a new folder called CmdMvnApp.

The important stuff.
As you have alreay noticed, inside our new application, there is a file called pom.xml

What is a pom.xml ?
According to the official documentation
(from http://maven.apache.org/pom.html#What_is_the_POM )
POM stands for "Project Object Model". It is an XML representation of a Maven project held in a file named pom.xml. When in the presence of Maven folks, speaking of a project is speaking in the philosophical sense, beyond a mere collection of files containing code. A project contains configuration files, as well as the developers involved and the roles they play, the defect tracking system, the organization and licenses, the URL of where the project lives, the project's dependencies, and all of the other little pieces that come into play to give code life. It is a one-stop-shop for all things concerning the project. In fact, in the Maven world, a project need not contain any code at all, merely a pom.xml.
So in a nutshell: The POM holds the information of our project. It holds the dependencies, the plugins to be used etc..

Let see what our pom file has so far. if you open it with your preffered editor, you will something like the folowing:


You can easily identify the values we have set a while back during our command line process.
Furhtermore, you will see that Maven has added a dependency without asking us. That is the dependency of JUnit for unit testing.
Maven strongly believes that we should do some unit testing!
In case you never heard of JUnit, have a look here
http://junit.org/


We are not going to dive to unit testing in the article since it is out of scope. But I highly encourage you to use it if you havent done already. It will free your mind and your code!

What have we done so far? We have create a full java web app!!!
Congratulations!
We wont be doing anything else in this part of the article. The kinky details are comming up in the next and final part of this series.

Doing the same thing with Netbeans 7.3
We are going to do the same thing with Netbeans 7.3. We will create a new maven application.

Get Netbeans 7.3
Netbeans 7.3 is the latest  version at the time of this writting.. feel free to get the latest version
http://netbeans.org/downloads/
You could start with the JEE version..

Installation is pretty straightforward. Just call the executable. And the wizard is pretty simple.
As soon as you have your Netbeans installed, start it!

Netbeans comes with a bundled version of Maven. Since we have already installed Maven on our machine, it would be good to use that version.

Go to tools->options, select JAVA and then select Maven tab:


 OK. now we have the same Maven version on Netbeans.

Let create the maven application!

Then, select file-> new project.
You will see a dialog where you will get to choose the type of the project:



and guess what! we want a java web app!
hit next


It looks shinny doesnt it?

I am sure you can spot the artifactId, groupId and so on. One thing to notice here is the difference in the default package netbeans provides. It adds up the artifactId in the package suggestion. You can leave it as is if you want. I have removed the ending part just to make a difference. xD

hit next.

Then it will ask you to use a server. Leave it balnk. No server selected.
hit finish.

Thats it!

We now have bright new maven application.



As you can see, Netbeans produces a bit longer pom.xml file with more dependencies and plugins.

The bottom line is that, we have another web app!

We wont be dealing with the details of the pom.xml for the time being as it will lead to full blown book and we dont want that!

That is about it!

Lets go now to the final part of this series where put everything together and we deploy our applications using Maven:
http://dstas.blogspot.com/2013/04/maven-toweblogic-part4.html

No comments:

Post a Comment

LinkWithin

Related Posts Plugin for WordPress, Blogger...