Maven for project construction: 2. HelloWorld Project Construction Process

Source: Internet
Author: User

File structure description:




Project Build lifecycle:

Clear

Compile

Test

Package

Run

Deployment


Cleaning and compilation
Hello \ pom. xml


POM: Project Object Model, Project Object Model

Pom. xml is similar to ant build. xml.

<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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.demo.hello</groupId><artifactId>hello-world</artifactId><version>1.0.0-SNAPSHOT</version><name>hello</name></project>

Note:

ModelVersion: Specifies the current POM model version. Maven2 and Maven3 can only be 4.0.0

GroupId: Project Team name

ArtifactId: the unique id of the current Maven project in the group

Version: version


Hello. java under Hello \ src \ main \ java

package com.demo ;public class Hello {public void sayHi(){System.out.println("hello world");}public static void main(String [] args){new Hello().sayHi();}}

Run the cleanup and compilation commands:

Enter the hello folder path and run

mvn clean compile

Running result





View the content of the target folder






View local repository






Test:


Hello \ pom. xml

<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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.demo.hello</groupId><artifactId>hello-world</artifactId><version>1.0.0-SNAPSHOT</version><name>hello</name><properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  </properties>  <dependencies><dependency>  <groupId>junit</groupId>  <artifactId>junit</artifactId>  <version>4.10</version>  <scope>test</scope></dependency>  </dependencies></project>

Dependencies: indicates the dependent packages required by this project.

Hello \ src \ test \ java \ HelloTest. java

package com.demo;import org.junit.Test;public class HelloTest {@Testpublic void testHello(){new Hello().sayHi();}}

Run the test command

mvn clean test


View results





View local repository





Package
Run commands

mvn clean package


View results






Run

Rebuild pom. xml and add the plug-in

Pom. xml

<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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.demo.hello</groupId><artifactId>hello-world</artifactId><version>1.0.0-SNAPSHOT</version><name>hello</name><properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  </properties>  <dependencies><dependency>  <groupId>junit</groupId>  <artifactId>junit</artifactId>  <version>4.10</version>  <scope>test</scope></dependency>  </dependencies>  <build>    <plugins>    <plugin>      <groupId>org.apache.maven.plugins</groupId>      <artifactId>maven-compiler-plugin</artifactId>      <configuration>        <source>1.5</source>        <target>1.5</target>      </configuration>    </plugin>    <plugin>      <groupId>org.apache.maven.plugins</groupId>      <artifactId>maven-shade-plugin</artifactId>      <version>1.2.1</version>      <executions>        <execution>          <phase>package</phase>          <goals>            <goal>shade</goal>          </goals>          <configuration>            <transformers>              <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">                <mainClass>com.demo.Hello</mainClass>              </transformer>            </transformers>          </configuration>        </execution>      </executions>    </plugin>    </plugins>  </build></project>


Run the packaging command again:

mvn clean package


After the execution is complete, go to hello/target and run the command

java -jar hello-world-1.0.0-SNAPSHOT.jar



View results:





Install it to the local repository for other projects to depend on

Run commands

mvn clean install



View local repository





Source code download


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.