Recent Posts

Wednesday, 27 November 2019

Archetypes in MAVEN


     MAVEN is providing Standard directory structures to prepare projects depending on the ARCHETYPE selection.
To create sample MAVEN standalone application directory structure we will use the following command on command prompt .
C:\ashok\maven>mvn archetype:create -DgroupId=com.ashok -DartifactId= sample  -Dpackagename=com.ashok.bankapp

Steps to Create First Project in MAVEN In Standalone
☀ Install MAVEN Software
☀ Create MAVEN Project
☀ Write Java Code
☀ Compile Java Code
☀ Execute Java Application

1. Install MAVEN Software
1. download apache-maven-3.5.4.zip file from internet [https://maven.apache.org/download.cgi]
2. Unzip apache-maven-3.5.4.zip file under C drive and we will get apache-maven-3.5.4 folder.
3. Set the following Environment Variables in System.
    a) JAVA_HOME: C:/Java/jdk1.8.0;
    b) path: C:/Java/jdk1.8.0/bin;C:\apache-maven-3.5.4\bin;
    c) M2_HOME: C:/apache-maven-3.5.4
    d) MAVEN_HOME: C:/apache-maven-3.5.4 [Optional]
4. Test MAVEN Installation
    Open Command prompt and use the following command.
        C:\apache-maven-3.5.4\bin>mvn --version
    Apache Maven 3.5.4 (1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-06-
    18T00:03:14+05:30)
    Maven home: C:\apache-maven-3.5.4\bin\..
    Java version: 1.8.0, vendor: Oracle Corporation, runtime: C:\Java\jdk1.8.0\jre
    Default locale: en_US, platform encoding: Cp1252
    OS name: "windows 8.1", version: "6.3", arch: "x86", family: "windows".

2. Create MAVEN Project
    a. Create a seperate folder for MAVEN applications
    E:/mvn_projects
    b. Use the following command on Command prompt.
    E:/mvn_projects>mvn archetype:generate
    c. Provide archetype Number:1202 [ for maven-archetype-quickstart application]
    d. Provide archetype version number : 7
    e. Provide "groupId" value: com.ashok
    f. Provide "archetype" value : sampleapp
    g. Provide "version" number : 1.0 or 1.0-SNAPSHOT
    h. Provide "package" name : com.ashok
    i. Provide "Y" to confirm all the above detail.
If we do the above steps then MAVEN will create quick start project.

3. Write Java Code
package com.ashok;

public class App {
   public static void main( String[] args ) {
      System.out.println( "Welcome to Waytoeasylearn" );
   }
}
4. Compile Java Code
     To Compile JAVA code, first, go to application folder then use the following command on command prompt.
E:\mvn_projects\sampleapp>mvn compile
     If we use the above command then MAVEN will compile JAVA files and generates the .class files by creating "target" folder under "application folder".
5. Execute MAVEN Project
IN Two ways we are able to execute MAVEN Project.
1. Execute Test case directly
2. Execute Main App by creating project.

1. Execute Test case directly:
     E:\mvn_projects\sampleapp>mvn test

2. Execute Main App by creating project.
     a. Create jar file for the project
     E:\mvn_projects\sampleapp>mvn package
     b. Set classpath environment variable to the generated jar file:
    E:\mvn_projects\sampleapp>set classpath=E:\mvn_projects\sampleapp\target\sampleapp-1.0.jar;
     Execute Main Class:
     E:\mvn_projects\sampleapp>java com.ashok.App
     c) Welcome to Waytoeasylearn

Steps to prepare MAVEN Project in Eclipse
1. Create Maven Project in Eclipse:
2. Provide the required configurations in pom.xml
3. Write Application Logic in AppTest.java file
4. Run Maven Project

1. Create Maven Project in Eclipse
1. Open Eclipse IDE
2. Right Click on "Project Explorer"
3. Select "New"
4. Select "Others"
5. Search for "Maven".
6. Select for "Maven Project".
7. Click on "Next" button.
8. Click on "Next" button
9. Provide the following details.
    a. groupId: com.ashok
    b. artifactId: sampleapp
    c. version: 0.0.1-SNAPSHOT
    d. package: com.ashok
10. Click on "Finish" Button.

2. Provide the required configurations in pom.xml 
1. Open pom.xml file File and provide java8 plugin configuration.
<?xml version="1.0" encoding="UTF-8"?>
<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.ashok</groupId>
   <artifactId>helloapp</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>
   <name>helloapp</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>
   <build>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
               <source>1.8</source>
               <target>1.8</target>
            </configuration>
         </plugin>
      </plugins>
   </build>
</project>
2. Save pom.xml file and see "Markers" for errors.
3. Right Click on :Project"
4. Select "Maven" .
5. Select "Update Project".
6. Click on "OK" button.

No comments:

Post a Comment