MAVEN archetype is predefined template for specific project type like web project, spring project, provided by Maven. In Maven a template is called
an archetype.
As we know that when we create a project in eclipse it ask us to select the type of project like simple java project, dynamic web project ... when we select the type of project a structure of the project is created by eclipse.
similarly maven provides different archetype to create project.
Using an Archetype :
To create a new project based on an Archetype, you need to call mvn archetype:generate goal, like the following:
$ mvn archetype:generate
when you run this command maven download the diffrent archetype provided by maven, and ask you to select the archetype for your project.sample output of the command
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------ [INFO]
[INFO] maven-archetype-plugin:3.0.1:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO] [INFO] <<< maven-archetype-plugin:3.0.1:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:3.0.1:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0) Choose archetype:
1: remote -> am.ik.archetype:maven-reactjs-blank-archetype (Blank Project for React.js)
2: remote -> am.ik.archetype:msgpack-rpc-jersey-blank-archetype (Blank Project for Spring Boot + Jersey)
3: remote -> am.ik.archetype:mvc-1.0-blank-archetype (MVC 1.0 Blank Project)
4: remote -> am.ik.archetype:spring-boot-blank-archetype (Blank Project for Spring Boot)
5: remote -> am.ik.archetype:spring-boot-docker-blank-archetype (Docker Blank Project for Spring Boot)
6: remote -> am.ik.archetype:spring-boot-gae-blank-archetype (GAE Blank Project for Spring Boot)
7: remote -> am.ik.archetype:spring-boot-jersey-blank-archetype (Blank Project for Spring Boot + Jersey)
8: remote -> at.chrl.archetypes:chrl-spring-sample (Archetype for Spring Vaadin Webapps)
9: remote -> br.com.address.archetypes:struts2-archetype (an archetype web 3.0 + struts2 (bootstrap + jquery) + JPA 2.1 with struts2 login system)
10: remote -> br.com.address.archetypes:struts2-base-archetype (An Archetype with JPA 2.1; Struts2 core 2.3.28.1; Jquery struts plugin; Struts BootStrap plugin; json Struts plugin; Login System using Session and Interceptor)
11: remote -> br.com.anteros:Anteros-Archetype (Anteros Archetype for Java Web projects.)
12: remote -> br.com.codecode:vlocadora-json (Modelos com Anotações Gson)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 1007:
Press Enter to choose to default option or chosse the number of archetype. Enter project detail as asked.Press Enter if default value is provided. You can override them by entering your own value.
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------ [INFO]
[INFO] maven-archetype-plugin:3.0.1:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO] [INFO] <<< maven-archetype-plugin:3.0.1:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:3.0.1:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0) Choose archetype:
1: remote -> am.ik.archetype:maven-reactjs-blank-archetype (Blank Project for React.js)
2: remote -> am.ik.archetype:msgpack-rpc-jersey-blank-archetype (Blank Project for Spring Boot + Jersey)
3: remote -> am.ik.archetype:mvc-1.0-blank-archetype (MVC 1.0 Blank Project)
4: remote -> am.ik.archetype:spring-boot-blank-archetype (Blank Project for Spring Boot)
5: remote -> am.ik.archetype:spring-boot-docker-blank-archetype (Docker Blank Project for Spring Boot)
6: remote -> am.ik.archetype:spring-boot-gae-blank-archetype (GAE Blank Project for Spring Boot)
7: remote -> am.ik.archetype:spring-boot-jersey-blank-archetype (Blank Project for Spring Boot + Jersey)
8: remote -> at.chrl.archetypes:chrl-spring-sample (Archetype for Spring Vaadin Webapps)
9: remote -> br.com.address.archetypes:struts2-archetype (an archetype web 3.0 + struts2 (bootstrap + jquery) + JPA 2.1 with struts2 login system)
10: remote -> br.com.address.archetypes:struts2-base-archetype (An Archetype with JPA 2.1; Struts2 core 2.3.28.1; Jquery struts plugin; Struts BootStrap plugin; json Struts plugin; Login System using Session and Interceptor)
11: remote -> br.com.anteros:Anteros-Archetype (Anteros Archetype for Java Web projects.)
12: remote -> br.com.codecode:vlocadora-json (Modelos com Anotações Gson)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 1007:
Choose org.apache.maven.archetypes:maven-archetype-quickstart version:
1: 1.0-alpha-1
2: 1.0-alpha-2
3: 1.0-alpha-3
4: 1.0-alpha-4
5: 1.0
6: 1.1
Choose a number: 6:
Define value for property 'groupId': com.xyz
Define value for property 'artifactId': mvntest
Define value for property 'version' 1.0-SNAPSHOT: :
Define value for property 'package' com.xyz: : com.xyz.mvntest
1: 1.0-alpha-1
2: 1.0-alpha-2
3: 1.0-alpha-3
4: 1.0-alpha-4
5: 1.0
6: 1.1
Choose a number: 6:
Define value for property 'groupId': com.xyz
Define value for property 'artifactId': mvntest
Define value for property 'version' 1.0-SNAPSHOT: :
Define value for property 'package' com.xyz: : com.xyz.mvntest
Maven will ask for project detail confirmation. Press enter or press Y
Confirm properties configuration:
groupId: com.xyz
artifactId: mvntest
version: 1.0-SNAPSHOT
package: com.esc.mvntest
Y: :
Now Maven will start creating project structure and will display the following:
groupId: com.xyz
artifactId: mvntest
version: 1.0-SNAPSHOT
package: com.esc.mvntest
Y: :
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.1
[INFO] ---------------------------------------------------------------------------- [INFO] Parameter: basedir, Value: /home/expert
[INFO] Parameter: package, Value: com.esc.mvntest
[INFO] Parameter: groupId, Value: com.xyz
[INFO] Parameter: artifactId, Value: mvntest
[INFO] Parameter: packageName, Value: com.xyz.mvntest [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] project created from Old (1.x) Archetype in dir: /home/expert/mvntest
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 06:26 min
[INFO] Finished at: 2017-07-19T18:10:36+05:30
[INFO] Final Memory: 18M/199M
[INFO] ------------------------------------------------------------------------
You'll see a java application project created named mvntest which was given as artifactId at the time of project creation.
Maven will create a standard directory layout for the project as shown below:
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.1
[INFO] ---------------------------------------------------------------------------- [INFO] Parameter: basedir, Value: /home/expert
[INFO] Parameter: package, Value: com.esc.mvntest
[INFO] Parameter: groupId, Value: com.xyz
[INFO] Parameter: artifactId, Value: mvntest
[INFO] Parameter: packageName, Value: com.xyz.mvntest [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] project created from Old (1.x) Archetype in dir: /home/expert/mvntest
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 06:26 min
[INFO] Finished at: 2017-07-19T18:10:36+05:30
[INFO] Final Memory: 18M/199M
[INFO] ------------------------------------------------------------------------
mvntest
├── pom.xml
└── src
├── main
│ └── java
│ └── com
│ └── xyz
│ └── mvntest
│ └── App.java
└── test
└── java
└── com
└── xyz
└── mvntest
└── AppTest.java
Maven generates a POM.xml file for the project as listed below:
├── pom.xml
└── src
├── main
│ └── java
│ └── com
│ └── xyz
│ └── mvntest
│ └── App.java
└── test
└── java
└── com
└── xyz
└── mvntest
└── AppTest.java
<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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.xyz</groupId> <artifactId>mvntest</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>mvntest</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
package com.esc.mvntest; /** * Hello world! * */ public class App { public static void main( String[] args ) { System.out.println( "Hello World!" ); } }
To compile the code we use the following command
$ mvn compile
The sample output of the above commmand
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building mvntest 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mvntest ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/expert/mvntest/src/main/resources
[INFO] [INFO] --- maven-compiler-plugin:3.2:compile (default-compile) @ mvntest ---
[INFO] Nothing to compile - all classes are up to date
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.150 s
[INFO] Finished at: 2017-07-22T11:04:23+05:30
[INFO] Final Memory: 7M/121M
[INFO] ------------------------ ------------------------------------------------
[INFO] Building mvntest 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mvntest ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/expert/mvntest/src/main/resources
[INFO] [INFO] --- maven-compiler-plugin:3.2:compile (default-compile) @ mvntest ---
[INFO] Nothing to compile - all classes are up to date
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.150 s
[INFO] Finished at: 2017-07-22T11:04:23+05:30
[INFO] Final Memory: 7M/121M
[INFO] ------------------------ ------------------------------------------------
Packaging the code
As we know that this project is just a simple java program so we can create the jar file of this project. To create the jar file of the project we use the following command
The default naming convention of Maven artifacts is:
{artifact-name}-{artifact-version}
$ mvn package
Sample output of the following command
[INFO] Scanning for projects...
[INFO] [INFO] ------------------------------------------------------------------------
[INFO] Building mvntest 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mvntest ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/expert/mvntest/src/main/resources
[INFO] [INFO] --- maven-compiler-plugin:3.2:compile (default-compile) @ mvntest ---
[INFO] Nothing to compile - all classes are up to date
[INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mvntest ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/expert/mvntest/src/test/resources
[INFO] [INFO] --- maven-compiler-plugin:3.2:testCompile (default-testCompile) @ mvntest ---
[INFO] Nothing to compile - all classes are up to date
[INFO] [INFO] --- maven-surefire-plugin:2.17:test (default-test) @ mvntest ---
[INFO] Surefire report directory: /home/expert/mvntest/target/surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.esc.mvntest.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.004 sec - in com.esc.mvntest.AppTest
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ mvntest ---
[INFO] Building jar: /home/expert/mvntest/target/mvntest-1.0-SNAPSHOT.jar [INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.885 s
[INFO] Finished at: 2017-07-22T11:28:38+05:30
[INFO] Final Memory: 16M/158M
[INFO] ------------------------------------------------------------------------
No comments:
Post a Comment