Monday, 8 July 2019

Maven: Create new project


By using 'archetype' plugin of maven, you can create maven project.

List all the archetypes?
Maven has many archetypes to generate project skelton.

For example,
maven-archetype-quickstart : Generate simple maven project.
maven-archetype-j2ee-simple : Generate simple j2ee application.

How to list all the available archetypes?
Use the command ‘mvn archetype:generate’ to list all the archetypes available in the system.

When you execute the command, maven will list all the available archetypes. At the end, maven asks you to select the archetype to create the project. You can enter the archetype number to create the project of specified archetype project.

Syntax
mvn archetype:generate -DarchetypeArtifactId={ARCHE_TYPE_ID}

Example
mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart

When you execute above command, maven asks groupId, artifactId, version, and package information and create a simple project.
C:\Users\krishna\Documents\Study\Maven\projects>mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ 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) < generate-sources @ standalone-pom <<<
[INFO]
[INFO]
[INFO] --- maven-archetype-plugin:3.0.1:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
Define value for property 'groupId': selfLearningJava
Define value for property 'artifactId': helloworld
Define value for property 'version' 1.0-SNAPSHOT: : 1
Define value for property 'package' selfLearningJava: : com.sample.app
Confirm properties configuration:
groupId: selfLearningJava
artifactId: helloworld
version: 1
package: com.sample.app
 Y: : y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: C:\Users\krishna\Documents\Study\Maven\projects
[INFO] Parameter: package, Value: com.sample.app
[INFO] Parameter: groupId, Value: selfLearningJava
[INFO] Parameter: artifactId, Value: helloworld
[INFO] Parameter: packageName, Value: com.sample.app
[INFO] Parameter: version, Value: 1
[INFO] project created from Old (1.x) Archetype in dir: C:\Users\krishna\Documents\Study\Maven\projects\helloworld
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 46.931 s
[INFO] Finished at: 2018-04-13T14:35:49+05:30
[INFO] ------------------------------------------------------------------------


After the command is executed, it creates below project structure.


All the java application source code is located in ${PROJECT_ROOT_DIRECTORY}/src/main/java.

All the tests will be located at
${PROJECT_ROOT_DIRECTORY}/src/test/java

In addition to above folder hierarchy, maven generates two files.

a.   pom.xml
b.   App.java
c.    AppTest.java

pom.xml contains below information.
<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>selfLearningJava</groupId>
  <artifactId>helloworld</artifactId>
  <packaging>jar</packaging>
  <version>1</version>
  <name>helloworld</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>


App.java
package com.sample.app;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
    }
}

AppTest.java
package com.sample.app;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * Unit test for simple App.
 */
public class AppTest 
    extends TestCase
{
    /**
     * Create the test case
     *
     * @param testName name of the test case
     */
    public AppTest( String testName )
    {
        super( testName );
    }

    /**
     * @return the suite of tests being tested
     */
    public static Test suite()
    {
        return new TestSuite( AppTest.class );
    }

    /**
     * Rigourous Test :-)
     */
    public void testApp()
    {
        assertTrue( true );
    }
}

Go to the project root directory, where pom.xml is located, run the command ‘mvn clean install’ to generate the jar file.


When you execute the command ‘mvn clean install’, it executes all the test cases and generate a jar file.
C:\Users\krishna\Documents\Study\Maven\projects\helloworld>mvn clean install
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------< selfLearningJava:helloworld >---------------------
[INFO] Building helloworld 1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ helloworld ---
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ helloworld ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\krishna\Documents\Study\Maven\projects\helloworld\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ helloworld ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 1 source file to C:\Users\krishna\Documents\Study\Maven\projects\helloworld\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ helloworld ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\krishna\Documents\Study\Maven\projects\helloworld\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ helloworld ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 1 source file to C:\Users\krishna\Documents\Study\Maven\projects\helloworld\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ helloworld ---
[INFO] Surefire report directory: C:\Users\krishna\Documents\Study\Maven\projects\helloworld\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.sample.app.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.01 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ helloworld ---
[INFO] Building jar: C:\Users\krishna\Documents\Study\Maven\projects\helloworld\target\helloworld-1.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ helloworld ---
[INFO] Installing C:\Users\krishna\Documents\Study\Maven\projects\helloworld\target\helloworld-1.jar to C:\Users\krishna\.m2\repository\selfLearningJava\helloworld\1\helloworld-1.jar
[INFO] Installing C:\Users\krishna\Documents\Study\Maven\projects\helloworld\pom.xml to C:\Users\krishna\.m2\repository\selfLearningJava\helloworld\1\helloworld-1.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.508 s
[INFO] Finished at: 2018-04-13T14:41:08+05:30
[INFO] ------------------------------------------------------------------------

‘jar’ file is located in ‘target’ directory of project root folder.

Execute that jar file
Syntax
Java -jar JAR_FILE_NAME

When I ran the command ‘java -jar helloworld-1.jar’, I end up in below error.
no main manifest attribute, in helloworld-1.jar

It is because, we need to specify the manifest file to run the jar file.


I added below statements in pom.xml and specify main class as ‘App.java’ in pom.xml file.

   <build>
      <plugins>
         <plugin>
            <!-- Build an executable JAR -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.0.2</version>
            <configuration>
               <archive>
                  <manifest>
                     <addClasspath>true</addClasspath>
                     <classpathPrefix>lib/</classpathPrefix>
                     <mainClass>com.sample.app.App</mainClass>
                  </manifest>
               </archive>
            </configuration>
         </plugin>
      </plugins>
   </build>


pom.xml
<?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/maven-v4_0_0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>selfLearningJava</groupId>
   <artifactId>helloworld</artifactId>
   <packaging>jar</packaging>
   <version>1</version>
   <name>helloworld</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>
   <build>
      <plugins>
         <plugin>
            <!-- Build an executable JAR -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.0.2</version>
            <configuration>
               <archive>
                  <manifest>
                     <addClasspath>true</addClasspath>
                     <classpathPrefix>lib/</classpathPrefix>
                     <mainClass>com.sample.app.App</mainClass>
                  </manifest>
               </archive>
            </configuration>
         </plugin>
      </plugins>
   </build>
</project>

Regenerate the jar file by executing ‘mvn clean install’ command.


Execute the jar file now, you can able to see ‘Hello World’ message.

C:\Users\krishna\Documents\Study\Maven\projects\helloworld\target>java -jar helloworld-1.jar
Hello World!



Previous                                                    Next                                                    Home

No comments:

Post a Comment