Showing posts with label Maven. Show all posts
Showing posts with label Maven. Show all posts

Saturday, February 8, 2014

How to debug Maven Applications in Intellij IDEA

Before I start, I would like to emphasize that this article is based on a Java console application and you need to make minor justifications in order to make it work for web apps in general.

I am assuming that you have a maven project (check out Getting Started with Maven) to make sure that project structure is valid.

To be able to debug a maven application, you need to set a Maven Debug Configuration in your Intellij IDEA project. If you do not know anything about existence of such thing, do not be scare as I will follow you through the steps.

First, in order to create a maven debug configuration, go to Run and click on Edit Configurations:


After that the Run/Debug Configuration window will pop up. Click on the "+" sign at the top-left part of the window, find Maven and click on it:


Now, you will see your Maven Configuration window. You can set a name  for it based on the goal of this debug or even name of the project. Just right below the name textbox, there are three tabs: Parameters, General and Runner. For this tutorial, we stick with the project settings options for the last two tabs and just consider the options inside the first Tab. As you can see in the following screen capture, the project directory has already been set for you. The most important task here is to set the command line of this configuration plus the profile of which you would like to debug your application.




Since we have a console application here, we will use Exec Maven Plugin which exists in Maven version 1 onwards.

exec:java -Dexec.mainClass=MainClass -Dexec.args=args

The -Dexec.mainClass will be set to the entry point of the application, here is the class with the main method, and -Dexec.args will be a set of arguments which will be passed to that main method. Here is an example:


exec:java -Dexec.mainClass=TopologyMain -Dexec.args=src/main/resources/words.txt

Now after you have set your debug breakpoint, set your Maven configuration in the dropdown of Select Run/Configuration and click on Debug (Or alternatively, go to Run and click on Debug and in the new window, select your Maven Config file):


After that, the Exec Plugin will compile your code and detach to the debugger process, like the following:



Happy Debugging your Maven Code!

Wednesday, January 9, 2013

Create a Dynamic Web Project with Maven in Eclipse Juno

Recently, I began to show my interest to start using Apache Maven in nearly all of my projects. Today, I was  trying to get hold of creating a Dynamic Web Project (Jave EE) project using the M2Eclipse Plugin, which is a fascinating tool to work with, but is tricky as well. While there are other related posts like here, they are outdated and in case you will follow them, you will be exhausted.


1. Prerequisites

You have to have the followings in advance:

  • Eclipse Juno for Java EE developers
  • JDK 1.6 or above
  • Apache Maven 3.0 or above
  • M2E Plugin (You can get this easily by searching it in the search bar of Eclipse Market Place inside the Help  menu of your Eclipse)
  • Apache Tomcat 

2. Create a Maven Project

2.1 Plugin Prerequisites 

You need to have a plugin called M2Eclipse which you can fetch it via Eclipse Marketplace (In case you do not know, just browse to Help and then you can find it there)

Some of you may not know but you also need another plugin called Eclipse WTP plugin. The latter is the plugin that lets you run Web Applications with Built-in servers. Have a look at the plugins you need to have  in advance:


Also note that there is another Maven Eclipse WTP plugin which is not depreciated in favor of the one you can see in the above picture. Everytime you need to install a plugin, you also need to restart your Eclipse.


2.2 Create your project

This step is just simple and easy. First, click on File, then go to New and click on Other... . In there you can search for the word "Maven" and you will get the following options:



Then, simple click on the Next button and in the next dialogue, select maven-archetype-webapp move on:





And the final step is to name your project:



You should by now see the following structure of your project with the new look:


Notice that you have your Deployment Descriptor which enables you to run it on Eclipse's internal server. Open your pom.xml and change it to the following:



  4.0.0
  telecom
  agent
  war
  0.0.1-SNAPSHOT
  agent Maven Webapp
  http://maven.apache.org
  
    
      junit
      junit
      3.8.1
      test
    
    
 javax.servlet
   servlet-api
   2.5
    
  
  
    agent
  

As you can see I just added the servlet-api.jar to the dependencies of the project, when later on we will have to compile our Maven project. Now, I will add a basic Servlet in the package src/main/java/servlet/HelloWorld.java:

package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloWorld extends HttpServlet {
 private static final long serialVersionUID = 1L;
       
    protected void doGet(HttpServletRequest request, 
      HttpServletResponse response) throws ServletException, IOException {
     
             response.setContentType("text/html");
  PrintWriter out = response.getWriter();
  out.println("The app is up and running!");
  out.close();
  
 }

}


After that, add the servlet information and pattern in the web.xml which you may find under src/main/webapp:

  Archetype Created Web Application
  
      hello
      servlet.HelloWorld
  
  
      hello
      /hello
  



Now it is the time to compile the project. Here you have two options. First is to Right click on your project, then choose Maven and Click on Maven Clean and after that on Maven Install or else, run the following command inside your project directory:

cd /project/directory
mvn clean install

You can have your .war file in your target directory now and you can deploy it in your Apache Tomcat container, however the missing part here is that Eclipse does not know that your project is a web project, therefore you cannot deploy it in your local Apache Tomcat Server (Also your development Server). Every time you code, you need to compile and see the result in your Tomcat. Now we do two more steps so that we can have both the .war file and you will be able to run the application while coding in your tomcat server. First, go to Servers menu which resides in Window and then Show View:


And simply add a Apache Tomcat 7 instance, without changing any settings and click Finish.


3. Run the Project

Now, click on your project in Eclipse's navigation bar and click Refresh. As you can see, the project structure changes. Now you can simple right click on HelloWorld.java and run it on Server, as following:


And that is it! You now have a fully working Dynamic Web Project Maven Project in eclipse!


Remark: In contrast with a lot of tutorials out there, you should not manually change Project Facets of your project, while you are working with Maven projects in Eclipse. The project should read everything from your pom file and change the facets on the fly.

Wednesday, September 5, 2012

Getting Started with Apache Lucene 4 with Maven: Hello World (Part 1: Analyzers)

Apache Lucene is a powerful search-engine library that stands for a perfect tool in case you somehow need to search unstructured data/text inside your application. Lucene website has a nice beginner tutorial here, however details are not provided and beginners might be lost with terminologies that are coded and used inside that tutorial without knowing them.

So shall we start right off the bat?

In this article, we tend to develop a simple Lucene 4 application with Apache Maven 2.

 In all search projects, the challenge is the text itself. The reason is that text in considered unstructured data. In order to tackle this problem, Analyzers  are trying to give some structure to the text they need to search into.  They achieve this through tokenizing (Dividing text into text units such as words or a character sequence, a phrase, email address, ... ), stemming (Transforming each word token to its root) and stopping characters (characters that repeat often in the text and therefore are of less value) inside the stream.

Lucene comes up with a set of different Analyzers to be used in different situations. Here, we explains a set of them given the following text:


First: Theory
1. WhitespaceAnalyzer
The simplest Analyzer in the package that each token starts after a white space and ends with a white space without any stemming and stopping method performed:
public WhitespaceAnalyzer(Version.LUCENE_36)

2. SimpleAnalyzer

The Analyzer tokenizes the text into letters (with isLetter method in java.lang.Character class) abd applies a lower-case filter to it. This Analyzer shall have problems while working with far-eastern languages such as Chineese.
public SimpleAnalyzer(Version.LUCENE_36)

3. StandardAnalyzer

The most popular Analyzer for Enlgish texts that includes the following rules for  creating token which suites most European Languages (StandardTokenizer):
  • Splits words at punctuation characters, removing punctuation. However, a dot that's not followed by whitespace is considered part of a token.
  • Splits words at hyphens, unless there's a number in the token, in which case the whole token is interpreted as a product number and is not split.
  • Recognizes email addresses and internet hostnames as one token. 
It also inlcludes set of basic stops words which can be extended in different cases and contexts.

4. PorterAnalyzer

The Analyzer is based on Martin Porter stemming algorithm. In this approach, we tokenize texts based on all three stemming, filtering to lower-case and also filtering stop words. This approach is highly useful however it should be noted that it can create meaningless tokens such as "tire" for the word "tired". This is no problem as Analyzer are used in fetching documents and do not affect the response which user will read afterwards.

5. StandardBgramAnalyzer

The term bgram refers to subsequence of 2 characters from a string character. The Analyzer creates its index longer than other Analyzers. While it filters the text to lower case, in order to keep stop words in the index set, an underscore character "_" is placed between first and second part of the tokens. Start and End of a string will be indexed twice in this approach. The following is the tokens for "Bird on a wire":

bird
bird_on
on_a
a_wire
wire


It should be noted that idea of forming bgrams is based on finite-state machines. Lastly, it should be remembered that the big advantage of this approach is that it can match the queries which include stop words more precisely than others analyzers. 


Application

Since we are only starting, we will just start with setting pom.xml and building the application dependencies and we continue in the next blog post.

First, creating Maven Archetypes:
mvn archetype:generate -DgroupId=se.findwise -DartifactId=my-lucene-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false


Second, editing pom.xml:

  4.0.0
  se.findwise
  my-lucene-app
  jar
  1.0-SNAPSHOT
  my-lucene-app
  http://maven.apache.org
  
  
   
    maven-compiler-plugin
    2.0.2
    
      1.6
      1.6
    
   
   
        org.codehaus.mojo
        exec-maven-plugin
        
          java
          
            -Xms512m
            -Xmx512m
            -XX:NewRatio=3
            -XX:+PrintGCTimeStamps
            -XX:+PrintGCDetails
            -Xloggc:gc.log
            -classpath
            
            se.findwise.App
          
        
      
  
 
  
    
      junit
      junit
      3.8.1
      test
    
    
      org.apache.lucene
      lucene-core
      4.0.0-BETA
    
  

Have you notices that in the element there are some additional arugement (lines 22-33) ? Yes, check out this awesome article. We will use this in next posts to make the running process of our application really easier than before.
Now just run the following and we are finished for today! ;)

mvn install
or
mvn package