Tuesday, June 17, 2008

Maven + Eclipse Java Project Setup Quick Steps

How to create a project .jar , .war file structure using Maven

1) Create a folder D:\mavenProject
2) Traverse to that Directory : CD D:\ mavenProject
3) Execute (for .jar): mvn archetype:create -DgroupId=com.abc.example -DartifactId=exampleLogic
4) Execute (for .war): mvn archetype:create -DgroupId=com.abc.example -DartifactId=exampleWeb -DarchetypeArtifactId=maven-archetype-webapp

Now we can independently build .jar and .war files by excecuting mvn celan install , command on respective folders.

i.e on D:\mavenProject\exampleLogic and D:\mavenProject\exampleWeb

The .jar file and .war file will be installed in the maven local repository.

Note : the maven local repository is configured in settings.xml located @ <maven Installaion directory>\conf

Say suppose our exampleLogic .jar is used in exampleWeb then we can add that as dependency in the .pom file of exampleWeb as follows

<dependency>
<groupId>com.abc.example</groupId>
<artifactId>exampleLogic</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

In this case first we have to build the .jar file and then we have to build the .war file

If the .jar file is not installed in the local maven repository and mvn clean install is executed on exampleWeb then we will receive an error.

If we want to run a single command to clean and install both the logic and web project we can create a .pom file in the D:\mavenProject as follows

<project>
<name>Maven Example</name>
<url>http://localhost:8080/</url>
<modelVersion>4.0.0</modelVersion>
<groupId>com.abc.example</groupId>
<version>1.0</version>
<artifactId>mavenExample</artifactId>
<packaging>pom</packaging>
<modules>
<module>exampleLogic</module>
<module>exampleWeb </module>
</modules>
</project>

How to configure the created project to work with eclipse.

1) Create a folder D:\workspaceExample
2) CD D:\mavenProject\exampleLogic : the following commands have to be executed from a folder having .pom file
3) Execute : mvn eclipse:add-maven-repo -Declipse.workspace=D:\workspaceExample
Note : This will create M2_REPO variable for the given workspace
4) Execute mvn -Dwtpversion=1.0 eclipse:eclipse from D:\mavenProject\exampleLogic
Note : This will create .class and .project files for Eclipse for ExampleLogic project.
5) Execute mvn -Dwtpversion=1.0 eclipse:eclipse from D:\mavenProject\exampleWeb
Note : This will create .class and .project files for Eclipse for ExampleLogic project.
6) Open the workspace (D:\workspaceExample) from eclipse
7) Select File > Import > General > Existing Projects into workspace

Comments: Post a Comment

Subscribe to Post Comments [Atom]





<< Home

This page is powered by Blogger. Isn't yours?

Subscribe to Comments [Atom]