Maven Combat (ii) build a simple MAVEN project

Source: Internet
Author: User
Tags unique id

In the previous section, we talked about the installation and configuration of Maven, and in this section we'll learn to create a simple MAVEN project

1. Create a simple MAVEN project with the MAVEN command

Run the following command in CMD: C code mvn archetype:generate-dgroupid=com.mycompany.app-dartifactid=my-app-simple-dversion=1.0 -darchetypeartifactid=maven-archetype-quickstart

MVN archetype:generate 
-dgroupid=com.mycompany.app 
-dartifactid=my-app-simple
 -dversion=1.0 
- Darchetypeartifactid=maven-archetype-quickstart

You can create a simple MAVEN project in the current directory and, of course, create it by downloading the dependencies from the Maven library and patiently waiting.

The general structure of MAVEN is as follows: Java code   my-app    |-- pom.xml    '-- src      & Nbsp; |-- main        |   |-- java         |   |    '-- com        |    |        '-- mycompany         |   |            '-- app         |   |                 '-- app.java        |    '--  resources        |        '--  Meta-inf        |             '--&Nbsp;application.properties         '-- test              '-- java                  '-- com                      '-- mycompany                          '-- app                               '-- apptest.java  

My-app |
| pom.xml '--src |--
    main
    |   | --java
    |   |   '--com
    |   |       '--mycompany
    |   |           '--app
    |   |               '--App.java
    |   '--Resources
    |       '--Meta-inf
    |           '--application.properties
    '--Test
        '--Java
            '--com
                '--mycompany
                    '--app
                        '-- Apptest.java

Src/main/java : Java source file storage location

Src/main/resource : Resource resources, such as configuration files, etc.

src/test/java : Test code source file storage location

2. Simple Pom.xml

Open Project to see Pom.xml XML code<projectxmlns= "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.mycompany.app</groupId><artifactId>My-app-simple</artifactId><packaging>Jar</packaging><version>1.0</version><name>My-app-simple</name><url>http://maven.apache.org</url><dependencies><dependency><groupId>Junit</groupId><artifactId>Junit</artifactId> <version>3.8.1</version><scope>Test</scope></dependency></dependencies></project>

<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.mycompany.app</groupId>
  < artifactid>my-app-simple</artifactid>
  <packaging>jar</packaging>
  <version> 1.0</version>
  <name>my-app-simple</name>
  <url>http://maven.apache.org</url >
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      < artifactid>junit</artifactid>
      <version>3.8.1</version>
      <scope>test</ scope>
    </dependency>
  </dependencies>
</project>

The most important part of this code is the three lines that contain GroupID, Artifactid, and version. These three elements define the basic coordinates of a project

 

groupId defines which group the project belongs to, and this group is often associated with the organization or company where the project is located. For example, a project called MyApp was established on the Googlecode, then GroupID should be Com.googlecode.myapp

Artifactid defines the unique ID of the current MAVEN project in the organization, which can be understood as the module in the project, and the module is the smallest unit component in Maven

versions of version projects

3. Run the simple maven command

We've created the simplest MAVEN project, so let's do some simple build commands

compiling: Compile

In cmd, switch directories to my-app-simple and perform mvn clean compile

After build success, we'll see a new target directory under My-app-simple that holds the compiled files for the project, such as. class files

Cleaning: Clean

CMD directory my-app-simple execute command mvn clean

Deletes the target file, which is the cleanup project, which can be run with other commands

Testing: Test

CMD directory my-app-simple execute command MVN test

will perform the JUnit test code under Src/test/java

Of course, the compilation command is executed automatically before the test is executed, and the results are as follows:



Packing: Package

CMD directory my-app-simple execute command MVN package

The project is made into a jar package and placed in the target directory

Compile and test commands are executed before executing this command

Installation: Install

CMD directory my-app-simple execute command mvn install

The project jar package is installed in the local warehouse so that other projects can use

The compile, test, Package command is executed before executing this command

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.