Friday, September 20, 2019

Post#93.Create Maven Project from Command Prompt


Create New Maven Project from Command Prompt
Step 1: Install and setup maven
Step 2: Use Maven template to generate project structure and artifacts

Syntax:
mvn archetype:generate -DgroupId=YourProjectGroupId -DartifactId=YourProjectName -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false


Update above template with your project parameters. Open command prompt/terminal , navigate to your preferred directory you would like to create your project in and fire above command.

$>mvn archetype:generate -DgroupId=com.websystique.maven -DartifactId=SampleMavenJavaProject -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
You will see following output
E:\workspace>mvn archetype:generate -DgroupId=com.websystique.maven -DartifactId=MavenSampleJavaProject -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom >>
> 
[INFO]
[INFO] <<< maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom <<
< 
[INFO]
[INFO] --- maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom --
-
[INFO] Generating project in Batch mode
[INFO] -------------------------------------------------------------------------
---
[INFO] Using following parameters for creating project from Old (1.x) Archetype:
 maven-archetype-quickstart:1.0
[INFO] -------------------------------------------------------------------------
---
[INFO] Parameter: groupId, Value: com.websystique.maven
[INFO] Parameter: packageName, Value: com.websystique.maven
[INFO] Parameter: package, Value: com.websystique.maven
[INFO] Parameter: artifactId, Value: MavenSampleJavaProject
[INFO] Parameter: basedir, Value: E:\workspace
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: E:\workspace\MavenSampleJavaProject
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.112s
[INFO] Finished at: Fri Jul 25 22:12:30 CEST 2014
[INFO] Final Memory: 11M/112M
[INFO] ------------<------------------------------------------------------------

Browse to generated project directory and you should see the following:


Basically two different directories structure are generated along with maven pom.xml. All your source code of application should be in src/main/java & all unit-test code should be in src/test/java
File pom.xml contains the information and instructions for maven to do it’s job. You must update pom.xml with all the information your project will rely upon(dependencies, profiles,…).
Below is the default generated 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.websystique.maven</groupId>
  <artifactId>MavenSampleJavaProject</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>MavenSampleJavaProject</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>

Step 3: Convert generated Maven project to Eclipse compatible form.
Go back to command line, navigate to project root, and fire mvn eclipse:eclipse

E:\workspace\MavenSampleJavaProject>mvn eclipse:eclipse

You will see that maven downloads required resources/dependencies from maven repository. You will also note that maven have created a .class and .project file on project root. You can now import your project into Eclipse IDE.
Step 4: Import your project into Eclipse

Open Eclipse. Select File->Import->Maven->Existing Maven Projects->Next , Browse to your newly created project directory, click Finish.
From here on, you can use eclipse to manage your porject, edit pom.xml and enjoy other handy eclipse plugins like m2eclipse to quickly clean build or install your project.


No comments:

Post a Comment