| By Murali Kashaboina, Geeth Narayanan | Article Rating: |
|
| July 19, 2007 01:15 PM EDT | Reads: |
26,353 |
We'll use the built-in maven-archetype-quickstart arechetype to create a directory structure representing a typical Java project. Open a command prompt and change the directory to the working directory where the project directory should be created. At the command prompt, execute this Maven command:
mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=
maven-archetype-quickstart -DgroupId=com.somecompany -DartifactId=EmployeeInfo -Dversion=1.0
On the command we provided the Maven coordinates for our project; com.somecompany as the group ID, EmployeeInfo as artifact ID, and 1.0 as the artifact version. Executing the command above creates this directory structure as shown in Figure 10.
A default POM file is created for our project as shown below. The directory structures src/main/java/ and /src/test/java are created with directories for the default package. Note that Maven Archetype uses a group ID for default package names unless a -DpackageName property is explicitly specified on the command. Note that by default Archetype includes Junit as a dependency and creates App.java in the main source directory and a Junit test case AppTest.java in the test source directory:
<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.somecompany</groupId>
<artifactId>EmployeeInfo</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>EmployeeInfo</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>
Importing a Maven Project into Eclipse
The following steps show how to import a Maven project into Eclipse:
1.
The first thing would be to create Eclipse-specific project files based
on Maven's POM information so that Maven project can be imported into
Eclipse. This is fairly easy to do using Maven's Eclipse plug-in. At
the command prompt change the directory to the EmployeeInfo directory
and run the Maven command to execute the Eclipse plug-in with eclipse
goal as shown in Figure 11.
Executing the Eclipse plug-in with an eclipse goal will result in
creating the .class-path and .project files that Eclipse uses.
2. Import the newly created project into Eclipse. Click on File >
Import to open the Eclipse import wizard and select Existing Projects
into Workspace as shown in Figure 12.
3. In the next window browse through the directory to select the
EmployeeInfo project directory to be imported. Optionally, you can
select the Copy projects into workspace checkbox if you have to copy
the project directory (see Figure 13).
4. Eclipse will import and open the project in the explorer package as shown in Figure 14. Note that Eclipse displays a project failure flag indicating that the project isn't Maven-enabled yet
5. The next step would be to enable Maven for the project by right clicking on the project and selecting Maven > enable option as shown in the Figure 15.
6. Once the project is Maven-enabled, Maven tasks can be done from
Eclipse. The first thing would be to update the Maven source
directories using the Maven option as shown in Figure 16. Enabling
Maven and updating the Maven sources will set the Maven class-path to
be used according to Eclipse and will update all the source directories
including the test source directories.
7. Now that we have a parent directory structure laid out for the
EmployeeInfo project, we have to make a slight change to the POM so it
can start accepting other modules as children. By default the POM file
has a jar packaging type. The packaging type for a parent POM that can
contain other Maven modules should be set to pom. By changing the
packaging type to pom, there will be no specific artifact coming out of
the parent POM and so the src directory, as it is, can be removed.
However, specifying the group ID and version in the parent POM will let
all sub-modules inherit such attributes and make the parent POM a
central place to specify such attributes. Once the packaging type is
changed to pom and the source directories deleted, Maven source
directories should be updated using the Maven option shown in Figure 16.
8. The project in Eclipse should look like Figure 17.
Note that by default Maven uses a target/classes directory to store
main Java classes and a target/test-classes directory to store test
Java classes.
Creating Project Modules
Now that we have the
parent project it's time to create the individual module projects
inheriting the POM from the parent project.
Setting Up a Castor-based xmlBinding Module
Since
an xmlBinding module generates a JAR artifact, we need a simple JAR
project directory structure. To create this, go to the EmployeeInfo
directory on the command prompt and execute the following Maven
command:
mvn archetype:create -DgroupId=com.somecompany -DartifactId=xmlBinding -Dversion=1.0
This creates a simple project structure with src/main/java and src/test/java directories containing a sample Java class and its Junit test case respectively. Now refresh the EmployeeInfo project in Eclipse and update the Maven source directories using the Maven option in the right-click menu. Eclipse will add the xmlBinding directory to the project and show both xmlBinding/src/main/java and xmlBinding/src/test/java as Eclipse Java source directories as shown in Figure 18. Since we intend to generate XML-bound Java classes using Castor, the xmlBinding/src/main/java and xmlBinding/src/test/java default Java source directories can be deleted.
Let 's compare the xmlBinding POM file with the parent EmployeeInfo POM file (see Table 2).
Published July 19, 2007 Reads 26,353
Copyright © 2007 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Murali Kashaboina
Murali Kashaboina is a lead architect at Ecommerce Technology, United Airlines, Inc. He has more than 10 years of enterprise software development experience utilizing a broad range of technologies, including JEE, CORBA, Tuxedo, and Web services. Murali previously published articles in WLDJ and SilverStream Developer Center. He has master’s degree in mechanical engineering from the University of Dayton, Ohio.
More Stories By Geeth Narayanan
Geeth Narayanan is a senior architect at Ecommerce Technology, United Airlines, Inc. He has 10 years of experience in the IT industry, specializing in solutions using Java EE technologies. Geeth has master's degree in electrical engineering from the University of Toledo, Ohio.
![]() |
Brad 10/05/07 04:58:49 PM EDT | |||
On page 2 the Figure 11 cuts off the command that is used. I think the command is supposed to be "mvn eclipse:eclipse" to generate Eclipse descriptor. |
||||
![]() |
Nazir Khan 09/27/07 05:37:33 AM EDT | |||
This is an amazing intro to maven 2, it has been very beautifully crafted, which starts telling about the internals of maven and how it works on different scenarios and then taking to practicalities of it. Hats off to the guys who made this wonderful work. |
||||
- Kindle 2 vs Nook
- Cloud Computing on Gartner's Top 10 List and SYS-CON Events' 2010 Calendar
- Confessions of a Ulitzer Addict
- IBM Hardware Chief, Intel VC Exec Arrested in Insider Trading Scam
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Ulitzer.com Named Exclusive "New Media" Sponsor of Cloud Computing Conference & Expo
- Moving Your RIA Apps into the Cloud: Seven Challenges
- Adobe’s Aiming ColdFusion at Multiple Clouds
- Windows 7 – Microsoft’s First Step to the Cloud
- Ulitzer Provides a Powerful Social Journalism Platform
- Jill Tummler Singer, Deputy CIO of CIA, Keynotes at GovIT Expo
- Open Source Mobile Cloud Sync and Push Email
- Kindle 2 vs Nook
- The Difference Between Web Hosting and Cloud Computing
- Cloud Computing on Gartner's Top 10 List and SYS-CON Events' 2010 Calendar
- Ajax in RichFaces 3.3, JSF 2 and RichFaces 4
- Confessions of a Ulitzer Addict
- IBM Hardware Chief, Intel VC Exec Arrested in Insider Trading Scam
- My Thoughts on Ulitzer
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Ulitzer.com Named Exclusive "New Media" Sponsor of Cloud Computing Conference & Expo
- US Post Office Hops a Ride on NetSuite’s Cloud
- Moving Your RIA Apps into the Cloud: Seven Challenges
- Adobe’s Aiming ColdFusion at Multiple Clouds
- Building a Drag-and-Drop Shopping Cart with AJAX
- What Is AJAX?
- Google Maps! AJAX-Style Web Development Using ASP.NET
- Flashback to January 2006: Exclusive SYS-CON.TV Interviews on "OpenAjax Alliance" Announcement
- AJAXWorld Conference & Expo to Take Place October 2-4, 2006, at the Santa Clara Convention Center, California
- AJAX Sponsor Webcasts Are Now Available at AJAXWorld Website
- How and Why AJAX, Not Java, Became the Favored Technology for Rich Internet Applications
- "Real-World AJAX" One-Day Seminar Arrives in Silicon Valley
- AJAXWorld University Announces AJAX Developer Bootcamp
- AJAX Support In JadeLiquid WebRenderer v3.1
- Where Are RIA Technologies Headed in 2008?
- Struts Validations Framework Using AJAX







































