Maven uses Maven tutorial 1 getting started

Simple and practical (partially forwarded to other bloggers)

1. Build the standard project mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=my-app

2. Compile and package test mvn package
3.java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App run
all:
1.maven clean compile
2.maven clean test
3.maven clean package
4.maven clean install
5.java -jar .jar
 
build web project
1. mvn archetype:create -DgroupId=com.mycompany.webapp -DartifactId=myweb -DarchetypeArtifactId=maven-archetype-webapp (the source code is placed in the java folder and needs to be created by yourself, according to
http://outofmemory.cn/wr/?u=http%3A%2F%2Fmaven.apache.org%2Fguides%2Fintroduction%2Fintroduction-to-the-standard-directory-layout.html, as a document establishing a standard
2mvn install/package
Directory structure: source files are placed in java
 
 
Maven is an open source project management tool written in pure Java. Maven uses a concept called project object model (POM) to manage projects. All project configuration information is defined in a file called POM.xml. Through this file, Maven can manage the entire declaration cycle of the project , including compiling, building, testing, publishing, reporting, and more. At present, most projects under Apache have been managed by Maven. Maven itself also supports a variety of plug-ins, which can facilitate more flexible control of the project. 

1: http://maven.apache.org/download.html Download the latest version of Maven 3.0.2 (Binary zip) 
 

2: Unzip it to D: 
 

3: Configure the environment variable 
   MAVEN_HOME : D:\apache-maven-3.0.2 
   MAVEN : %MAVEN_HOME%\bin 
  (optional) MAVEN_OPTS : -Xms256m -Xmx512m 

   Add %MAVEN% in front of the path; 
 

4: To verify whether the installation is successful 
, enter on the command line: mvn -version; Successful installation: 

 

5: Create a project 
and enter on the command line: mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=my-app press Enter, 

 

if you run this command for the first time (goal), maven will take some time Go and download the latest toolkit (Maven calls it artifacts) to your local repository. 

 

After the command is executed, you will see that maven has generated a directory named my-app. This name is the artifactId you specified in the command. Enter this directory and you will find the following standard project structure: 

 

Among them: src/main/ The java directory contains the source code of the project, the src/test/java directory contains the test code of the project, and pom.xml is the project object model (Project Object Model or POM) of the project. 

6: POM 
pom.xml file is the core configuration of maven for a project, this file will contain most of the configuration information of how you want to build the project. POMs are large and complex, but you don't need to understand them all, just use some common configurations. The contents of this POM are listed below: 
quote

<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.mycompany.app</groupId> 
  <artifactId>my-app</artifactId> 
  <version>1.0-SNAPSHOT</version> 
  <packaging>jar</packaging> 

  <name>my-app</name> 
  <url>http://maven.apache.org</url> 

  <properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
  </properties> 

  <dependencies> 
    <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>3.8.1</version> 
      <scope>test</scope> 
    </dependency> 
  </dependencies> 
</project> 

7: Step 5 What we did 
quote
mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=my-app

Execute the Maven command (goal) archetype:create, and set some parameters ( -DgroupId=com.mycompany.app -DartifactId=my-app). 

In this command, the prefix archetype is a maven plugin that contains the create command. The goal command builds a simple project based on a project archetype (a maven-compliant project template). 

Now it can be said with certainty that a maven plugin is a collection of goals commands with the same purpose, such as the jboss-maven-plugin plugin, which is to handle various tasks related to jboss. 

8: Enter the Build project 
on the command line: cd my-app Enter, enter the project path 

 

and enter mvn package Enter. At this time, the command line will print out various actions, and end with the following information: The 

 

same as the first executed command ( that's a goal
quote
archetype:create

The difference, this time is just a simple command --- package. Different from goal, this is a phase (phase) , a phase refers to a phase of the construction life cycle, and the construction life cycle refers to an ordered series of phases. When given a phase, Maven will execute all phases before this phase and itself. For example, if we execute the compile phase, the phases actually executed are: 
quote
validate 
generate-sources 
process-sources 
generate-resources 
process-resources 
compile

You can use the following command to test the newly compiled and packaged jar package, 
quote
java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App

This will print the most classic: 
quote
Hello World!

 

9: Running Maven tools 

Although it is difficult to list a very comprehensive list, here is a list of the most common default life cycle phases: 
quote
validate: Verify that the project is correct and that all required resources are available. 
compile: Compile the source code of the project.   
test: Use a suitable unit testing framework to test the compiled source code. These tests do not need to be packaged and deployed. 
Package: Package the compiled code into a releasable format, such as a jar. 
integration-test: Process and publish the package to an environment capable of integration testing, if needed. 
verify: Runs all checks to verify that the package is valid and meets quality standards. 
install: Install the package in the local repository, which can be used as a dependency by other projects. 
Deploy: Executed in an integration or release environment, copies the final version of the package to a remote repository so that other developers or projects can share it. 
clean: Clears the previously built artifacts (in maven, the packages generated by the project are called artifacts). 
site: Generates a documentation site for the project. 
  
The phases listed above actually correspond to potential goals. The special goals executed by each phase are determined by the type of the project. For example, if the type of the project is jar, the package phase will execute jar by default: The goals of the jar, if the project type is war, the goals executed in the package phase will be war:war. 
One interesting thing to note is that phases and goals need to be executed in a certain order. 
The command mvn clean dependency:copy-dependencies package 
will first clear the project, then copy the dependencies, and finally package the project. Of course, before packaging, the stages before this stage will be executed first. Such as compile, test, etc. 
Generate the site 
mvn site 
this The stage generates project information based on the pom.xml configuration. You can see the generated documentation in the target/site directory

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326634610&siteId=291194637