Maven - Maven Creating Project- maven tutorial



maven tutorial tags : apache maven , maven repository , maven central

How to create a project in Maven ?

  • Java project can be created using Maven. Maven can also be used to build and manage projects developed using C#, Ruby, Scala and few other languages.
  • But, this is primarily used for java projects

Archetype

  • Maven uses archetype to create any project.
  • Archetype is nothing but the tool provided by the maven to create a project.
  • It is also defined as 'an organizational pattern from which all other things of the similar kind are made'.
  • Using archetypes provides an easy and instrumental way to create projects to the developers, where the best practices are employed as per the standards defined at the organization level.
  • learn maven tutorial - maven project-maven build system flow - Apache Maven example programs

    learn maven tutorial - apache maven - maven project-maven build system flow - Apache Maven example programs

  • Also, archetypes enables the developers to quickly create a project and start running the same within a matter of commands.
  • In order to create a new simple maven java project just use the maven archetype plugin in the command line.
  • Currently there are more than 850+ maven archetypes listed out in order to create projects under different frameworks like struts, spring, hibernate, web services (RESTful/SOAP) and many others.
  • Create one simple project in maven.
  • Open the command prompt and navigate to the workspace folder in your local machine (E.g. D:\Java\workspace).
  • Create the Workspace folder wherever you want on your PC or use your Workspace, if already created.
  • Then type the maven command(its a single line command) and press enter.
C:\MVN>mvn archetype:generate
-DgroupId=com.companyname.bank 
-DartifactId=consumerBanking 
-DarchetypeArtifactId=maven-archetype-quickstart 
-DinteractiveMode=false
  • This maven command will create a Java project with the below details:
    • groupId will be com.java.samples, representing the package.
    • artifactId will be JavaSamples (on the build, JavaSamples.jar will be created)
    • archetypeArtifactId is nothing but the template used for creating this Java project.
    • interactiveMode is used when the developer is aware of the actual spelling of the artifact id.
    • In the above case maven-archetype-quickstart is the one which is used and it is the proper one.
    • If developer is not aware of this then the interactiveMode is set to be TRUE so that it will scan the remote repositories for all available archetypes. This might take longer time.
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.4:generate (default-cli) > generate-sources
@ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.4:generate (default-cli) < generate-sources
@ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:2.4: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.companyname.bank
[INFO] Parameter: packageName, Value: com.companyname.bank
[INFO] Parameter: package, Value: com.companyname.bank
[INFO] Parameter: artifactId, Value: consumerBanking
[INFO] Parameter: basedir, Value: C:\MVN
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: C:\MVN\consumerBanking
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 03:19 min
[INFO] Finished at: 2015-09-26T12:18:26+05:30
[INFO] Final Memory: 15M/247M
[INFO] ------------------------------------------------------------------------
  • The above command instructs the maven to create a project with maven-archetype-quickstart template.
  • If you navigate to the folder D:\Java\workspace, a new project named JavaSamples will be created including all the base structure of a java project as well as shown below :
Maven Create Project Structure
  • Using above example, we can understand following key concepts
Folder Structure Description
consumerBanking contains src folder and pom.xml
src/main/java contains java code files under the package structure (com/companyName/bank).
src/main/test contains test java code files under the package structure (com/companyName/bank).
src/main/resources it contains images/properties files (In above example, we need to create this structure manually).
  • If you see, Maven also created a sample Java Source file and Java Test file. Open C:\MVN\consumerBanking\src\main\java\com\companyname\bank folder, you will see App.java.
maven tutorial tags : apache maven , maven repository , maven central

App.java

package com.companyname.bank;

/**
 * Hello wikitechy!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello Wikitechy!" );
    }
}
  • Open C:\MVN\consumerBanking\src\test\java\com\companyname\bank folder, you will see AppTest.java.
maven tutorial tags : apache maven , maven repository , maven central

AppTest.java

package com.companyname.bank;

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 );
    }
}
  • Developers are required to place their files as mentioned in table above and Maven handles the all the build related complexities.
    • The test class code which is marked as Rigorous Test is the code which is automatically generated when a sample java project is created using maven.
    • This method will just return TRUE and the test will be passed after the run.
    • This method is just included as one sample test.
    • Modify this method or add your own test methods.
    • Also, the TestCase class is an abstract class from the Junit API which is used here in the sample project.
    • This will also be included by default automatically when a sample maven java project is created (check the dependency entry for junit in the pom.xml).
    • The TestCase class defines the fixtures to run mutilple tests in a class.

Wikitechy provides an indepth knowledge on the below maven tutorial items such as maven download , maven install , maven goals , maven build , maven commands , maven plugin , maven search , maven eclipse , maven deploy , eclipse maven , maven junit , maven java , hibernate maven , maven project , java maven , maven version , maven proxy , maven 2 , maven pom , maven eclipse plugin , maven java version , maven properties , maven install windows , how to create maven project in eclipse , eclipse maven plugin , maven dependencies , what is maven in java , maven apache , maven project structure , how to install maven in eclipse

Related Searches to Maven Creating project