Create A Java Project With Maven
To create a Java Project from Maven Template open a terminal (*uix or Mac) or command prompt (Windows), navigate to the folder you want to store the project and call the following commands:
mvn archetype:generate -DgroupId={project-packaging} -DartifactId={project-name} -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Example:
mvn archetype:generate -DgroupId=com.mycompany -DartifactId=MyMavenProject -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
To convert the Maven project to support Eclipse IDE, navigate to the project in terminal and call this command:
mvn eclipse:eclipse
The default JDK version is 1.4. If you want to update the POM, open the pom.xml file and add the compiler plugin to tell Maven which JDK version to compile your project:
... <build> <plugins> ... <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.0</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build> </project>
You can also update the JUnit version to latest 4.11:
... <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <build> ...
The Packaging with Maven can be done with the following command:
mvn package
It compiles, runs your tests and generates the project “jar” file in project/target folder.
To run the application call this command:
java -cp target/{project-name}-1.0-SNAPSHOT.jar {project-packaging}.{main-class}
Example:
java -cp target/MyMavenProject-1.0-SNAPSHOT.jar com.mycompany.App
With the jar plugin you can add a Manifest file to the jar.
... <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>com.mycompany.App</mainClass> </manifest> </archive> </configuration> </plugin> ...
java -jar target/MyMavenProject-1.0-SNAPSHOT.jar