Maven Builds Java Projects
Maven uses the archetype plugin to create projects. To create a simple Java application, we will use the maven-archetype-quickstart plugin.
In the following example, we will create a Maven-based Java application project under the C:\MVN folder.
The command format is as follows:
mvn archetype:generate "-DgroupId=com.companyname.bank" "-DartifactId=consumerBanking" "-DarchetypeArtifactId=maven-archetype-quickstart" "-DinteractiveMode=false"
Parameter explanations:
- -DgroupId: Organization name, reverse of the company's website URL + project name
- -DartifactId: Project name-module name
- -DarchetypeArtifactId: Specifies the ArchetypeId, maven-archetype-quickstart, to create a simple Java application
- -DinteractiveMode: Whether to use interactive mode
The generated folder structure is as follows:
Folder Structure | Description |
---|---|
consumerBanking | Contains the src folder and pom.xml |
src/main/java contains | Java code files under package structure (com/companyName/bank) |
src/main/java contains | Test code files under package structure (com/companyName/bank) |
src/main/resources | Contains images/property files (in the above example, we need to manually create this structure) |
In the C:\MVN\consumerBanking\src\main\java\com\companyname\bank folder, you can see an App.java, with the following code:
App.java
package com.companyname.bank;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}
Open the C:\MVN\consumerBanking\src\test\java\com\companyname\bank folder to see the Java test file AppTest.java.
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 );
}
/**
* Rigorous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}
In subsequent development, we only need to place the files according to the structure mentioned in the table above, and Maven will handle the rest for us.