In this tutorial, we'll show you how to use Maven to create a Java project, import it into the Eclipse IDE, and package a Java project into a JAR file.
Tools Required: Maven 3.3.3 Eclipse 4.2 JDK 8 Note: Please make sure Maven is properly installed and configured (in the Windows,*nix,mac OS X System) and then start this tutorial to avoid MVN commands not finding errors. 1. Create a project from the Maven template
In the terminal (* Unix or Mac) or command prompt (Windows), browse to the folder where you want to create the Java project. Type the following command:
MVN archetype:generate-dgroupid={project-packaging}-dartifactid={project-name}-darchetypeartifactid= Maven-archetype-quickstart-dinteractivemode=false
This tells Maven to create a Java project from the Maven-archetype-quickstart template. If the Archetypeartifactid option is ignored, a list of huge Maven templates will be listed.
For example, the working directory here is: C:\WORKSP, which may take a long time to perform the command process, looking at the personal network situation.
C:\WORKSP>MVN Archetype:generate-dgroupid=com.yiibai-dartifactid=numbergenerat Or-darchetypeartifactid=maven-
Archetype-quickstart-dinteractivemode=false [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 BATC h mode [INFO]----------------------------------------------------------------------------[INFO] Using following Parameters for creating project from old (1.x) archetype:maven-archetype-quickstart:1.0 [INFO]----------------------------------------------------------------------------[INFO] parameter:basedir, value:c:\ WORKSP [INFO] parameter:package, Value:com.yiibai [info] parameter:groupid, Value:com.yiibai [INFO] Parameter:artifac TId, Value:numbergenerator [info] parameter:packagename, Value:com.yiibai [info] parameter:version, Value:1.0-snapsho T [INFO] project created from (1.x) archetype in Dir:c:\worksp\numbergenerato R [info]------------------------------ ------------------------------------------[INFO] Build SUCCESS [INFO]-------------------------------------------- ----------------------------[INFO] Total time:23.166 s [info] finished at:2015-10-27t11:03:48+08:00 [INFO] Final Memory : 17m/114m [INFO]------------------------------------------------------------------------
In these cases, a new Java project is named "Numbergenerator", and the entire project's directory structure is created automatically. Attention
A few users say the MVN archetype:generate command failed to build the project structure. If you have any similar questions, don't worry, just skip this step and create the folders manually, see the project structure in step 2. 2.Maven Directory Layout
The directory structure of the following items is created using the MVN archetype:generate + maven-archetype-quickstart template.
Numbergenerator
|-SRC
|---main
|-----java
|-------com
|---------yiibai
|----------- App.java
|---test|-----java
|-------com
|---------yiibai
|-----------Apptest.java
| Pom.xml
Very simply, all the source code is placed in the folder/src/main/java/, all the unit test code is put into/src/test/java/. Note that please read the MAVEN standard directory layout
An additional standard pom.xml is generated. This pom file is similar to the Ant Build.xml file, it describes the entire project information, everything from directory structure, Project Plug-ins, project dependencies, how to build this project, etc., please read the POM official guide Pom.xml 3. Eclipse IDE
To make it an Eclipse project, enter the "Numbergenerator" project at the terminal, type the following command:
C:\WORKSP\NUMBERGENERATOR>MVN eclipse:eclipse ..... [INFO] Using Eclipse Workspace:null [info] Adding default classpath container:org.eclipse.jdt.launching.JRE_CONTAIN ER [INFO] N
OT writing settings-defaults suffice [INFO] wrote Eclipse project for ' Numbergenerator ' to C:\worksp\NumberGenerator.
[INFO] [INFO]------------------------------------------------------------------------[INFO] Build SUCCESS [INFO]-------- ----------------------------------------------------------------[INFO] Total time:04:47 min [info] finished at: 2015-10-27t15:24:48+08:00 [INFO] Final memory:15m/164m [INFO]----------------------------------------------------- -------------------
After you execute the above command, it automatically downloads updates related resources and configuration information (it will take some time to wait) and produces all the project files required by the Eclipse IDE. To import the project into the Eclipse IDE, select "File-> Import ...-> general->existing Projects into Workspace"
Pictures: Items are imported into the Eclipse IDE.
4. Update Pom
The default pom.xml is too simple, and many times you need to add a compiler plugin to tell Maven which version of the JDK is used to compile the project. (Default JDK1.4, this is really too old a point)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId> maven-compiler-plugin</artifactid>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
< /plugin>
From the update JUnit 3.8.1 to the latest 4.11.
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
Maven coordinates
The above XML snippet is called "maven coordinates," and if you need a JUnit jar, you need to find out its corresponding MAVEN coordinates. It applies to all other dependencies, such as Spring,hibernate,apache, Common, and so on, as long as you go to the MAVEN Center repository and find out what is dependent on the correct Maven coordinates. pom.xml– Update Version
<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>com.mkyong</groupId> <artifactid>numbergenerator</ artifactid> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name> numbergenerator</name> <url>http://maven.apache.org</url> <dependencies> <dependency > <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</vers
ion> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <ARTIFACTID>MAVEN-COMPILER-PLUGIN&L T;/artifactid> <version>2.3.2</version>;configuration> <source>1.6</source> <target>1.6</target> </configuration> & lt;/plugin> </plugins> </build> </project>
In the terminal, the same command is issued again MVN eclipse:eclipse, maven will download the plug-in Project Dependencies (JUnit) from the MAVEN Center repository, which will automatically be saved to your local repository. 5. Update business logic
Test-driven Development (TDD), which first updates unit tests to ensure that application (app) objects have a method for generating unique keys that contain exactly 36-bit alphabets. Apptest.java
Package Com.yiibai;
Import Org.junit.Assert;
Import Org.junit.Test;
public class Apptest {
@Test public
void Testlengthoftheuniquekey () {
app obj = new app ();
Assert.assertequals (Obj.generateuniquekey (). Length ());
}
Complete the business logic. App.java
Package Com.yiibai;
Import Java.util.UUID;
/**
* Generate a unique number
*
*
/public class App
{public
static void Main (string[] args)
{
App obj = new app ();
System.out.println ("Unique ID:" + Obj.generateuniquekey ());
}
public string Generateuniquekey () {
string id = Uuid.randomuuid (). toString ();
return ID;
}
}
6. Maven Packaging
Now we will use MAVEN for this project and output the file compiled into a "jar". Refer to the Pom.xml file, where the package element defines what the package should output. Pom.xml
<project ...>
<modelVersion>4.0.0</modelVersion>
<groupid>com.yiibai</ groupid>
<artifactId>NumberGenerator</artifactId>
<packaging>jar</packaging >
<version>1.0-SNAPSHOT</version>
In the terminal input MVN package:
C:\worksp\numbergenerator> mvn Package ... ha-2/classworlds-1.1-alpha-2.jar (37 kb at 20.2&NBSP;KB/SEC) DOWNLOADED:&NBSP;HTTPS://REPO.MAVEN.APACHE.ORG/MAVEN2/ORG/CODEHAUS/PLEXUS/PLEXUS-IO/2 0.2/ plexus-io-2.0.2.jar (57 kb at 28.1 kb/sec) downloaded: https:// Repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-inte rpolation/1.15/plexus-interpolation-1.15.jar
(60&NBSP;KB&NBSP;AT&NBSP;21.4&NBSP;KB/SEC) Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-arch IVER/2.1/ plexus-archiver-2.1.jar (181 kb at 61.5 kb/sec) downloaded: https:// Repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-util s/3.0/plexus-utils-3.0.jar (221 KB AT&NBSP;60.3&NBSP;KB/SEC) [info] building jar: c:\worksp\numbergenerator\target\ Numbergenerator-1.0-snapsh Ot.jar [info] ------------------------------------------------------------------ ------[Info] bUild success [info] ------------------------------------------------------------------------[INFO] total time: 01:00 min [info] finished at: 2015-10-27t20:00:17+08:00 [INFO] final memory: 10m/54m [info] ------------------------------------------------------------- -----------
It compiles, runs unit tests and packs the project into a jar file and puts it in the Project/target folder. If an error occurs: Error:unable to locate the Javac Compiler in:, C:\Program Files (x86) \java\jre6\ ... \lib\tools.jar,please ensure you are using JDK 1.4 or above And,not a JRE (the Com.sun.tools.javac.Main class is required) ...
Reference: http://my.oschina.net/u/1449336/blog/199802
The directory structure of the final project, the following picture:
7. Example
To run an application sample from a project's jar file
C:\WORKSP\NUMBERGENERATOR>JAVA-CP Target/numbergenerator-1.0-snapshot.jar com.y Iibai. APP Unique id:94e5fd1a-c038-415f-a8ed-7fc58c397369 c:\worksp\numbergenerator> C:\worksp\numbergenerator>java -CP Target/numbergenerator-1.0-snapshot.jar com.y Iibai. APP Unique id:48df568a-4b4b-4964-b767-664e206ca4b5 C:\WORKSP\NUMBERGENERATOR>JAVA-CP target/ Numbergenerator-1.0-snapshot.jar com.y Iibai. APP Unique id:4ac9156c-2e4a-45f4-8644-0707ae28d5a6