Friday, October 28, 2011

maven


How to install Maven on Windows
Apache Maven is not require to install on Windows as a service component, you just need to download the Maven’s zip file, extract it and configure the Windows environment path variable. See following steps :

1. Add JAVA_HOME

Make sure you had installed JDK, and add a new “JAVA_HOME” into Windows environment variable, and point to your JDK folder.
Maven - add JAVA_HOME to windows environment

2. Download Apache Maven

Download Apache Maven zip file from this official website, e.g apache-maven-2.2.1-bin.zip.

3. Extract It

Extract the downloaded zip file into your folder, for instance, “D:\maven “.

4. Add MAVEN_HOME

Now, add Maven folder “MAVEN_HOME” to Windows environment variable also.
Maven - Add MEVEN_HOME to widnows environment

5. Add PATH

Add the “Maven bin folder” into Windows environment variable, so that you can access Maven command everywhere.
Maven - Add Maven bin folder to windows environment

6. Verification

Done, to verify it, in command prompt, type “mvn –version” to verify the installation detail.
C:\Documents and Settings\mkyong>mvn -version
Apache Maven 2.2.1 (r801777; 2009-08-07 03:16:01+0800)
Java version: 1.6.0_13
Java home: C:\Program Files\Java\jdk1.6.0_13\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows xp" version: "5.1" arch: "x86" Family: "windows"
If you see similar message, means your Apache Maven is installed successful on Windows.

How to enable proxy setting in Maven

There are high chance your company is set up a firewall and force the developers to connect internet via using HTTP proxy. If you are using HTTP proxy, Maven may not able to download the dependency libraries outside. To bypass it, you have to enable the proxy setting in Maven configuration file “settings.xml“.

1. Maven configuration file

Find your Maven configuration file – e.g, {M2_HOME}/conf/settings.xml

2. Edit it “settings.xml”

Find the following pattern
<proxies>
    <!-- proxy
     | Specification for one proxy, to be used in connecting to the network.
     |
    <proxy>
      <id>optional</id>
      <active>true</active>
      <protocol>http</protocol>
      <username>proxyuser</username>
      <password>proxypass</password>
      <host>proxy.host.net</host>
      <port>80</port>
      <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
    </proxy>
    -->
</proxies>
Comment out the proxy setting and fill in your proxy information.
<proxies>
 
    <proxy>
      <id>optional</id>
      <active>true</active>
      <protocol>http</protocol>
      <username>proxyuser</username>
      <password>proxypass</password>
      <host>proxy.yourcompany.com</host>
      <port>80</port>
      <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
    </proxy>
 
</proxies>

4. Saved it, Done.

Apache Maven is able to connect to Internet via proxy server now.

Maven Repository

Maven local, central and remote repository configuration and explanation, some terms you may need to understand before use Maven.

Where is Maven local repository ?

Published: January 23, 2009 , Updated: April 14, 2011 , Author: mkyong
Maven local repository is used to store all your projects’ dependency libraries (plugin jars and other files which downloaded by Maven), . In simple, when you use Maven to build your project, it will automatically download all dependency libraries into your Maven local repository.
Maven local repository is default to home directory :
  1. Unix/Mac OS X – ~/.m2 on
  2. Windows – C:\Documents and Settings\username\.m2 on Windows
Often times, you need to change the default location for maintainability purpose, after all, the .m2 is not a meaningful name.

1. Maven configuration file

Maven local repository is defined in the Maven’s configuration file, for example, {M2_HOME}\conf\setting.xml.

2. Edit it “setting.xml”

Find the following “localRepository” pattern, and define your new Maven local repository inside the “<localRepository>” element like this :
<settings>
  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ~/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->
 
<localRepository>D:/maven_repo</localRepository>

3. Saved it

Done, your new Maven local repository is now changed to D:/maven_repo, all the future project’s dependency libraries or related files will be downloaded into this folder. See following folder :
Maven local repository

What is Maven central repository
When you use Maven to build your project, Maven will check your pom.xml file to download all the project dependency libraries.
In this case, If Maven can’t find your dependency libraries in your Maven local repository, it will try download it from the default Maven central repository, which is http://repo1.maven.org/maven2/.
This is how Maven central repository look like :
Maven center repository
What is Maven remote repository, and how to configure it
By default, Maven’s will download all dependency libraries from Maven Central Repository. The central repository is full of Java libraries, but there are still many libraries are missing.
Often times , you need to include other Maven’s remote repositories like Java.net Repository and JBoss repository.

Java.net Repository

1. Add Java.net remote repository details in “pom.xml” file.
<project ...>
<repositories>
    <repository>
      <id>java.net</id>
      <url>http://download.java.net/maven/2</url>
    </repository>
</repositories>
</project>
2.Done

JBoss Maven Repository

1. Add JBoss remote repository details in “pom.xml” file. (This Maven repository is deprecated).
<project ...>
<repositories>
    <repository>
      <id>JBoss repository</id>
      <url>http://repository.jboss.com/maven2/</url>
    </repository>
</repositories>
</project>
The above repository is deprecated, uses latest JBoss repository below :
<project ...>
<repositories>
        <repository>
               <id>JBoss repository</id>
               <url>http://repository.jboss.org/nexus/content/groups/public/</url>
        </repository>
</repositories>
</project>
Maven’s dependency mechanism will download all the necessary dependency libraries automatically, and maintain the version upgrade as well.

Case study

Assume you want to use Log4J as your project logging mechanism (actually i more prefer SLF4J). Here is what you do…

1. In traditional way

  1. Visit http://logging.apache.org/log4j/
  2. Download the Log4j jar library
  3. Manually include it into your project dependency
  4. All manage by yourself, you need to do everything
If there is Log4j version upgrade, you need to repeat above steps again.

2. In Maven way

  1. You need to know the log4j “Maven coordinates“, for example
2.    <groupId>log4j</groupId>
3.    <artifactId>log4j</artifactId>
  <version>1.2.14</version>
·  It will download the log4j version 1.2.14 library automatically. If the “version” tag is ignore, it will upgrade the library automatically when there is a newer version.
·  Include “Maven coordinates” into “pom.xml” file, under “<dependencies>” tag
<dependencies>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.14</version>
    </dependency>
</dependencies>
  1. While Maven is compiling or building, the log4j will download automatically and put it into your Maven local repository.
  2. All manage by Maven… automatically.

See the different? So what just happened in Maven?

When you use Maven to build your project, “pom.xml” will be parsed, and the Maven search the log4j library in this order :
  1. Search log4j in Maven local repository.
  2. Search log4j in Maven central repository.
  3. Search log4j in Maven remote repository (if define in pom.xml).
The Maven dependency library management is quite impressive and handy ~ nice tool.

How to find the Maven coordinates?

The only problem is how do you know what Maven coordinates you want to put? To get it, you always can refer to the Maven Central Repository for detail, or using this Google workaround – Use Google site search function.

How to search the Maven coordinates – pom.xml dependency?

If you want to include a library dependency in “pom.xml” file, you have to define the “Maven coordinate
<!-- MySQL database driver -->
<dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.9</version>
</dependency>
However, one of the annoying problem of this is you do not know what’s the Maven coordinate details – group id, artifactId and etc. Many Java developers just browse the Maven central repository and search for it. If the dependency library is package in a deep folder structure, you may have problem in digging work.

Maven coordinates workaround

Here’s a dirty workaround it – Google site search Maven repository. The Google search function is powerful enough to find the Maven’s coordinate for you.
Let say, you want to know the “logback” Maven’s coordinates , you can search it in Google with search text
logback site:http://repo1.maven.org/maven2/
Google will return the result.
http://repo1.maven.org/maven2/ch/qos/logback/
There are still many Java libraries that are not support for Maven, or may be you want to create a custom library which is required to include it into your Maven local repository.
Fortunately, Maven comes with command to let you include your “non-maven-support” library into your Maven local repository easily.
For example, “kaptcha” is a third party library which is used to generate “captcha” image to stop spamming, but it did not support Maven.
Here’s a guide to show you how to install the “kaptcha” jar into your Maven’s local repository

1. Install library – mvn install

Download the “kaptcha” jar file and put it into your “C:” drive, and issue following Maven’s command :
mvn install:install-file -Dfile=c:\kaptcha-2.3.jar -DgroupId=com.google.code 
-DartifactId=kaptcha -Dversion=2.3 -Dpackaging=jar
Result :
D:\>mvn install:install-file -Dfile=c:\kaptcha-2.3.jar -DgroupId=com.google.code 
-DartifactId=kaptcha -Dversion=2.3 -Dpackaging=jar
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'install'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Default Project
[INFO]    task-segment: [install:install-file] (aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] [install:install-file]
[INFO] Installing c:\kaptcha-2.3.jar to 
D:\maven_repo\com\google\code\kaptcha\2.3\kaptcha-2.3.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: < 1 second
[INFO] Finished at: Tue May 12 13:41:42 SGT 2009
[INFO] Final Memory: 3M/6M
[INFO] ------------------------------------------------------------------------
Now, the “kaptcha” jar library is included into your Maven local repository.

2. Modify pom.xml file

After installed, you can add the custom library details into your “pom.xml” file like this
<dependency>
      <groupId>com.google.code</groupId>
      <artifactId>kaptcha</artifactId>
      <version>2.3</version>
</dependency>

3. Done

Build it, now the “kaptcha” jar is able to retrieve from your Maven local repository.

Reference

  1. Maven install file documentation

Maven based project and Eclipse IDE

To create a Java project with Maven, you can issue this command :
mvn archetype:generate -DgroupId={packaging.path} -DartifactId={project-id} 
  -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Replace “{packaging.path}” and “{project-id}” value to suit your needs.

Steps to create a Java Project With Maven

See following guide to create a Java project with Maven.

1. mvn archetype:generate

Navigate to the folder you want to generate your Java project, e.g “D:\workspace-new\maven-test“. In console, issue this command :
mvn archetype:generate -DgroupId=com.mkyong.core -DartifactId=mkyong-core 
  -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
See output…
D:\workspace-new\maven-test>mvn archetype:generate -DgroupId=com.mkyong.core -Da
rtifactId=mkyong-core -DarchetypeArtifactId=maven-archetype-quickstart -Dinterac
tiveMode=false
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'archetype'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Default Project
[INFO]    task-segment: [archetype:generate] (aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] Preparing archetype:generate
[INFO] No goals needed for project - skipping
[INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus
.velocity.ContextClassLoaderResourceLoader'.
[INFO] Setting property: velocimacro.messages.on => 'false'.
[INFO] Setting property: resource.loader => 'classpath'.
[INFO] Setting property: resource.manager.logwhenfound => 'false'.
[INFO] [archetype:generate {execution: default-cli}]
[INFO] Generating project in Batch mode
[INFO] -------------------------------------------------------------------------
---
[INFO] Using following parameters for creating OldArchetype: maven-archetype-qui
ckstart:1.0
[INFO] -------------------------------------------------------------------------
---
[INFO] Parameter: groupId, Value: com.mkyong.core
[INFO] Parameter: packageName, Value: com.mkyong.core
[INFO] Parameter: package, Value: com.mkyong.core
[INFO] Parameter: artifactId, Value: mkyong-core
[INFO] Parameter: basedir, Value: D:\workspace-new\maven-test
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] ********************* End of debug info from resources from generated POM
***********************
[INFO] OldArchetype created in dir: D:\workspace-new\maven-test\mkyong-core
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Thu Apr 14 16:37:39 SGT 2011
[INFO] Final Memory: 8M/14M
[INFO] ------------------------------------------------------------------------
D:\workspace-new\maven-test>

2. Java Project Structure

Maven just generated so called “Maven Standard Directory Layout” for your new Java project, and the directory name is same as the given name in “artifactId”.
See Maven generated project structure :
Mavan Java Project Structure
In short, puts your source code in src/main/java, resources file in src/main/resources, unit test file in src/test/java.

3. pom.xml

See the generated pom.xml file, the tag “packaging” is “jar“, when build with Maven, this project will group it into a jar file.
File : pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
  http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mkyong.core</groupId>
  <artifactId>mkyong-core</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>mkyong-core</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>
Done. Maven just created a Java project, with all standard folder structure.
Convert Maven based Java project to support Eclipse IDE
In last tutorial, you created a Java project with Maven, but that project is not able to import into Eclipse IDE, because it is not Eclipse style project.
Here’s a guide to show you how to convert the Maven generated Java project to Eclipse supported style project.

1. mvn eclipse:eclipse

It’s really easy to do it. Navigate to your Java project folder, where pom.xml file is placed. And issue this command :
mvn eclipse:eclipse
See a full example :
D:\workspace-new\maven-test\mkyong-core>mvn eclipse:eclipse
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'eclipse'.
[INFO] ------------------------------------------------------------------------
[INFO] Building mkyong-core
[INFO]    task-segment: [eclipse:eclipse]
[INFO] ------------------------------------------------------------------------
[INFO] Preparing eclipse:eclipse
[INFO] No goals needed for project - skipping
[INFO] [eclipse:eclipse {execution: default-cli}]
[INFO] Using Eclipse Workspace: D:\workspace-new
[INFO] no substring wtp server match.
[INFO] Using as WTP server : Apache Tomcat v6.0
[INFO] Adding default classpath container: org.eclipse.jdt.launching.JRE_CONTAINER
[INFO] Not writing settings - defaults suffice
[INFO] Wrote Eclipse project for "mkyong-core" to D:\workspace-new\maven-test\mkyong-core.
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5 seconds
[INFO] Finished at: Thu Apr 14 22:47:38 SGT 2011
[INFO] Final Memory: 7M/14M
[INFO] ------------------------------------------------------------------------
Note
If this is your first time to run this command, it may take some time to download all required dependency to convert your project to Eclipse style project.

2. Verify Java Project

After that, you will notice two new files are created – “.classpath” and “.project“. Both files are created for Eclipse IDE.
File : .classpath
<classpath>
  <classpathentry kind="src" path="src/test/java" 
           output="target/test-classes" including="**/*.java"/>
  <classpathentry kind="src" path="src/main/java" including="**/*.java"/>
  <classpathentry kind="output" path="target/classes"/>
  <classpathentry kind="var" path="M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar" 
           sourcepath="M2_REPO/junit/junit/3.8.1/junit-3.8.1-sources.jar"/>
  <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
</classpath>
Wait, M2_REPO !?
Did you notice a “M2_REPO” classpath variable is generated? For first time, you have to add this M2_REPO classpath variable to your Eclipse IDE. Otherwise, Eclipse will raise error about your imported project later.

How to add M2_REPO classpath variable to Eclipse IDE

Normally, when you use mvn eclipse:eclipse to convert your Java project to support Eclipse IDE, Maven will create the entire dependency classpath by using the M2_REPO variable, which is not defined in the Eclipse IDE by default, you have to add it manually.

Note
In this case, M2_REPO is a “classpath variable“, which let Eclipse IDE knows where is your local Maven repository.
Here are two ways to add M2_REPO classpath variable to Eclipse IDE.

1. Add M2_REPO Manually

Define and add M2_REPO classpath variable manually to Eclipse IDE. Follow below steps :
  1. Eclipse IDE, menu bar
  2. Select Window > Preferences
  3. Select Java > Build Path > Classpath Variables
  4. Click on the new button > defined a new M2_REPO variable and point it to your local Maven repository
Done.

2. Add M2_REPO Automatically – eclipse:configure-workspace

Define and add M2_REPO with Maven command “eclipse:configure-workspace“.
mvn -Declipse.workspace="your Eclipse Workspace" eclipse:configure-workspace
You do not need any pom.xml file to execute this command, just run this “mvn” command everywhere you want.
See an example :
C:\>mvn -Declipse.workspace="C:\Users\mkyong\workspace" eclipse:configure-workspace
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'eclipse'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Default Project
[INFO]    task-segment: [eclipse:configure-workspace] (aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] [eclipse:configure-workspace {execution: default-cli}]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Thu Apr 14 20:45:17 SGT 2011
[INFO] Final Memory: 9M/112M
[INFO] ------------------------------------------------------------------------
Done.

Verify M2_REPO is Added

Both ways will add a M2_REPO classpath variable to Eclipse IDE, like what the following diagram has shown :
Add ME_REPO to Eclipse IDE
You need to add this M2_REPO classpath variable once only, and this variable is shared among your Eclipse’s workspace.
eclipse:add-maven-repo
The old “eclipse:add-maven-repo” command is deprecated , and not working anymore. Uses “eclipse:configure-workspace” instead.

 



File : .project
<projectDescription>
  <name>mkyong-core</name>
  <comment/>
  <projects/>
  <buildSpec>
    <buildCommand>
      <name>org.eclipse.jdt.core.javabuilder</name>
    </buildCommand>
  </buildSpec>
  <natures>
    <nature>org.eclipse.jdt.core.javanature</nature>
  </natures>
</projectDescription>

3. Import into Eclipse IDE

Now, import it into Eclipse IDE, follow below steps :
In Eclipse IDE, menu bar , File -> Import… -> General -> Existing Projects into Workspace -> select root directory (select your project folder) -> Done.

Import Maven project to Eclipse IDE
Your Maven generated Java project is imported into Eclipse IDE, you can start your development now.

How to create a Web Application Project with Maven

In last tutorial, you created a Java Project with Maven. But that project is not a web application project, to create a web project with Maven, issue this command :
mvn archetype:generate -DgroupId={packaging.path} -DartifactId={project-id} 
   -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
Replace “{packaging.path}” and “{project-id}” value to suit your needs.

Steps to create a Web Application Project With Maven

See following guide to create a web application project with Maven.

1. mvn archetype:generate

Navigate to the folder you want to generate your web project, e.g “D:\workspace-new\“. In console, issue this command :
mvn archetype:generate -DgroupId=com.mkyong.core -DartifactId=mkyongweb-core 
   -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
See output…
D:\workspace-new>mvn archetype:generate -DgroupId=com.mkyong.core 
  -DartifactId=mkyongweb-core -DarcetypeArtifactId=maven-archetype-webapp 
  -DinteractiveMode=false
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'archetype'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Default Project
[INFO]    task-segment: [archetype:generate] (aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] Preparing archetype:generate
[INFO] No goals needed for project - skipping
[INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus.velocity.ContextCl
ssLoaderResourceLoader'.
[INFO] Setting property: velocimacro.messages.on => 'false'.
[INFO] Setting property: resource.loader => 'classpath'.
[INFO] Setting property: resource.manager.logwhenfound => 'false'.
[INFO] [archetype:generate {execution: default-cli}]
[INFO] Generating project in Batch mode
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating OldArchetype: maven-archetype-webapp:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.mkyong.core
[INFO] Parameter: packageName, Value: com.mkyong.core
[INFO] Parameter: package, Value: com.mkyong.core
[INFO] Parameter: artifactId, Value: mkyongweb-core
[INFO] Parameter: basedir, Value: D:\workspace-new
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] ********************* End of debug info from resources from generated POM ******************
****
[INFO] OldArchetype created in dir: D:\workspace-new\mkyongweb-core
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Fri Apr 15 10:10:34 SGT 2011
[INFO] Final Memory: 8M/14M
[INFO] ------------------------------------------------------------------------
D:\workspace-new>

2. Web Application Project Structure

Maven will generate this “Maven Standard Web Application Directory Layout“. See following diagram :
Maven web application project
A new super clean “web.xml” file is created under “D:\workspace-new\mkyongweb-core\src\main\webapp\WEB-INF” folder.
File : web.xml
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
 
<web-app>
  <display-name>Archetype Created Web Application</display-name>
</web-app>
Note
Please refer to this Maven Standard Web Application Directory Layout article for detail explanation.

3. pom.xml

Refer to the generated pom.xml file, the tag “packaging” is “war“, when build with Maven, this project will group it into a war file for deployment.
File : pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
  http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mkyong.core</groupId>
  <artifactId>mkyongweb-core</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>mkyongweb-core Maven Webapp</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>
  <build>
    <finalName>mkyongweb-core</finalName>
  </build>
</project>
Done. Maven generated a standard web application project structure and a simple web.xml file.

Reference

  1. http://maven.apache.org/archetype/maven-archetype-plugin/generate-mojo.html

How to convert Maven based web application to support Eclipse IDE

In last tutorial, you created a web application with Maven. Here’s a guide to show you how to convert that web application to support Eclipse IDE.
Note
Eclipse IDE support web application via WTP tool, so you need to make your Maven based project support it as well.

1. mvn eclipse:eclipse -Dwtpversion=2.0

To convert a Maven based Java project to support IDE, you use this command :
mvn eclipse:eclipse
For web application, you need extra parameter to make it support Eclipse’s wtp, instead, you should use this command :
mvn eclipse:eclipse -Dwtpversion=2.0
See output …
D:\workspace-new\mkyongweb-core>mvn eclipse:eclipse -Dwtpversion=2.0
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'eclipse'.
[INFO] ------------------------------------------------------------------------
[INFO] Building mkyongweb-core Maven Webapp
[INFO]    task-segment: [eclipse:eclipse]
[INFO] ------------------------------------------------------------------------
[INFO] Preparing eclipse:eclipse
[INFO] No goals needed for project - skipping
[INFO] [eclipse:eclipse {execution: default-cli}]
[INFO] Adding support for WTP version 2.0.
[INFO] Using Eclipse Workspace: D:\workspace-new
[INFO] no substring wtp server match.
[INFO] Using as WTP server : Apache Tomcat v6.0
[INFO] Adding default classpath container: org.eclipse.jdt.launching.JRE_CONTAINER
[INFO] Not writing settings - defaults suffice
[INFO] Wrote Eclipse project for "mkyongweb-core" to D:\workspace-new\mkyongweb-core.
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Fri Apr 15 11:51:53 SGT 2011
[INFO] Final Memory: 7M/14M
[INFO] ------------------------------------------------------------------------

2. Eclipse WTP

Standard Eclipse’s “.classpath” and “.project” files are created. And you will noticed a new “.setting” folder is created and inside contains “org.eclipse.wst.common.component” and “org.eclipse.wst.common.project.facet.core.xml“, both files for WTP or Faces support in Eclipse.
File : org.eclipse.wst.common.project.facet.core.xml
<faceted-project>
  <fixed facet="jst.java"/>
  <fixed facet="jst.web"/>
  <installed facet="jst.web" version="2.4"/>
  <installed facet="jst.java" version="1.4"/>
</faceted-project>
Note
Maven 2.x generated web application with JDK1.4 (see above), which is rather outdated, you may need to upgrade it to latest JDK version.
File : org.eclipse.wst.common.component
<project-modules id="moduleCoreId" project-version="2.0">
  <wb-module deploy-name="mkyongweb-core">
    <property name="context-root" value="mkyongweb-core"/>
    <wb-resource deploy-path="/" source-path="src/main/webapp"/>
    <property name="java-output-path" value="/target/classes"/>
    <wb-resource deploy-path="/WEB-INF/classes" source-path="src/main/resources"/>
  </wb-module>
</project-modules>

3. Import into Eclipse IDE

Now, you have everything what a Eclipse web application want, so, you can start import your Maven based web application into Eclipse IDE.
Steps :
In Eclipse IDE, menu bar , File -> Import… -> General -> Existing Projects into Workspace -> select root directory (select your project folder) -> Done.
Convert Maven based web application to support Eclipse IDE
Done.

Maven Basic Operations

Some basic operations, compile, build, unit test, install, generate site and deploy your Maven based project.

Maven FAQ

Some common Maven FAQs.

Maven Errors

Some common error messages in Maven.

No comments:

Post a Comment