Maven archetypes make it easy to start projects with a standard/consistent code structure. Instead of repeating these steps in every blog that needs starting a project, this blog will be used to show common steps used to start new projects using Maven Archetypes. This post focuses on standalone apps.
Create Application structure
The name of artifactId to be changed as per the actual name of the project to be created
1 2 |
cd C:\tvi\Software\workspace_mars mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=com.techvalueinsight -DartifactId=standalone-utils -DinteractiveMode=false |
Create Folders
Create folders src/main/resources, src/test/resources.
Update pom.xml
Add properties to maintain versions and other variables at one place :
1 2 3 4 5 |
<properties> <java.version>1.8</java.version> <junit.version>4.12</junit.version> <maven.compiler.plugin.version>3.3</maven.compiler.plugin.version> </properties> |
Dependencies to use the version numbers from properties:
1 2 3 4 5 6 7 8 |
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies> |
Plugin to specify java version as desired:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<build> <finalName>standalone-utils</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${maven.compiler.plugin.version}</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> </plugins> </build> |
Import Maven project into Eclipse
Note: This step can be repeated when ever eclipse is not recognizing the changes in pom.xml etc and refresh the project in Eclipse
1 2 |
cd C:\tvi\Software\workspace_mars\standalone-utils mvn eclipse:clean eclipse:eclipse |
Open Eclipse with C:\tvi\Software\workspace_mars as workspace directory. File –> Import –> Existing Maven Projects. Root Directory C:\tvi\Software\workspace_mars\standalone-utils. Select standalone-utils’s pom.xml
Test
Open App.java (test java code created by Maven) and change its package location to com.techvalueinsight.apps. Right-Click on the file and Run As –> Java Application. It prints the customary ‘Hello World!’