Maven Learning (10)-----Create a Java project using MAVEN

Source: Internet
Author: User
Tags assert unique id uuid maven central

The tools you need:

    1. Maven 3.3.3
    2. Eclipse 4.2
    3. JDK 8
Note: Make sure that Maven is properly installed and configured (in the Windows,*nix,mac OSX system), and then start this tutorial to avoid the MVN command 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-dgroupid={Project-packaging-dartifactid={Project-name} -darchetypeartifactid=maven-archetype-dinteractivemode=false   

This tells Maven to create a Java project from the Maven-archetype-quickstart template. If you ignore the Archetypeartifactid option, a huge list of Maven templates will be listed.

For example, the working directory here is: C:\worksp, the execution of the command process may be a long time to see the personal network situation.

C:\WORKSP&GT;MVN Archetype:generate-dgroupid=com.yiibai-dartifactid=numbergenerator-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 Batch Mode[info]----------------------------------------------------------------------------[INFO] Using following Parameters for creating project from the 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:artifactid, Value: Numbergenerator[info] Parameter:packagename, Value:com.yiibai[info] parameter:version, Value:1.0-snapshot[info] Project created from old (1.x) archetype in Dir:c:\worksp\numbergenerator[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 the above scenario, a new Java project is named "Numbergenerator", and the entire project's directory structure is created automatically.

Note
A few users said that the MVN archetype:generate command failed to generate the project structure. If you have any similar problems, don't worry, just skip this step and manually create the folders, see the project structure in step 2. 2.Maven Catalog Layout

Using the MVN archetype:generate + maven-archetype-quickstart template, the following project's directory structure was created.

Numbergenerator   |-SRC   |---main   |-----java   |-------com   |---------yiibai      |----------- App.java   |---test|-----java   |-------com   |---------yiibai   |-----------Apptest.java   | Pom.xml

Quite simply, all the source code is placed in the folder/src/main/java/, and all the unit test codes are put into/src/test/java/.

Note, please readMAVEN Standard Catalog Layout

A standard pom.xml that is attached is generated. This pom file is similar to the Ant Build.xml file, which describes the entire project information, everything from directory structure, project plug-ins, project dependencies, how to build this project, etc., please read the official POM Guide

POM.XML3. Eclipse IDE

To make it an Eclipse project, enter the "Numbergenerator" item in the terminal and 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_container[info] Not 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 executing the above command, it automatically downloads updates related resources and configuration information (it will take some time) 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"

Picture: The project is imported into the Eclipse IDE.

4. Updating the POM

The default pom.xml is too simple, and many times you need to add a compiler plugin to tell Maven which JDK version is used to compile the project. (Default JDK1.4, this is really too old a bit)

      <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 updating 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 XML snippet above is called "maven coordinates", and if you need a JUnit jar, you need to find its corresponding MAVEN coordinates. It applies to all other dependencies, such as Spring,hibernate,apache Common , and so on, just go to the MAVEN central repository and find out which ones are dependent on the correct MAVEN coordinates. pom.xml– Updated 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</ Version><scope>test</scope></dependency></dependencies><build> <plugins> <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> </plugins></build></project> 

At the terminal, the same command is issued again MVN eclipse:eclipse, maven will download the plug-in Project dependency (JUnit) from the MAVEN central repository and it will automatically be saved to your local repository.

5. Update business logic

Test-driven Development (TDD), which updates unit tests to ensure that application (app) objects have a way to generate unique keys that contain exactly 36-bit alphabets.

Apptest.java
Package Com.yiibai;import Org.junit.assert;import Org.junit.test;public class Apptest {@Testpublic 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 files compiled into a "jar". Refer to the Pom.xml file, package element defines what should 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 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-snapshot.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 packages the project into a jar file, and puts it in the Project/target folder. If there is an error:error:unable to locate the Javac Compiler in:, C:\Program Files (x86) \java\jre6\. \lib\tools.jar,please ensure you be 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, such as slices:

7. Example

Run the application sample from the project's JAR file

C:\WORKSP\NUMBERGENERATOR>JAVA-CP Target/numbergenerator-1.0-snapshot.jar com.yiibai.AppUnique ID: 94E5FD1A-C038-415F-A8ED-7FC58C397369C:\WORKSP\NUMBERGENERATOR>C:\WORKSP\NUMBERGENERATOR>JAVA-CP target/ Numbergenerator-1.0-snapshot.jar Com.yiibai.AppUnique id:48df568a-4b4b-4964-b767-664e206ca4b5c:\worksp\ NUMBERGENERATOR>JAVA-CP Target/numbergenerator-1.0-snapshot.jar com.yiibai.AppUnique ID: 4ac9156c-2e4a-45f4-8644-0707ae28d5a6
Download code download code-Maven-numbergenerator.zip

Maven Learning (10)-----Create a Java project using MAVEN

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.