Get started with Maven 3-core concepts

Source: Internet
Author: User
Coordinates

For example, in the Pom. xml configuration file of helloworld

 

<Groupid> com. Zhaoyang </groupid>

<Artifactid> helloworld </artifactid>

<Version> 1.0-Snapshot </version>

 

Actually, it is the coordinate, so the coordinate elements of Maven include groupid, artifactid, version, packaging, Classifier

When a project is associated with another project through dependency, plug-in, or parent project reference, Maven uses coordinates to precisely locate a project.

 

Groupid: group, company, group, organization, project, or other group. The Convention of the Group logo is that it creates

The Reverse Domain Name of the Organization Name of the project.

 

Artifactid: the unique identifier under the groupid that represents a separate project

Version: defines the version of a project.

Packaging: packaging method, such as jar or war. The default value is jar.

Classifier: indicates the JDK version of the component, which cannot be defined directly.

 

 

Else ---------------------------------------------------------------------------------------------------------------------------------

 

We will install the started helloworld project to the local repository and run MVN clean install

Now, go to the local warehouse directory to view the project, and we can understand the structure of coordinates in the warehouse.

For example:

 

 

Note: The coordinates of Maven refer not only to a corresponding JAR file, but also to its Dependencies. Through the coordinates, we can precisely locate this Maven project.

 

 

Maven Life Cycle)

What is lifecycle?

Lifecycle refers to a series of processes, including project cleanup, initialization, compilation, testing, packaging, integration testing, verification, deployment, and site production, however, every company does the same thing in different ways, so Maven abstracts and unifies these processes (called building processes ).

 

Maven has three sets of independent lifecycles: clean, default, and site.

 

1) The clean life cycle is used to clear the project (the target folder is deleted by default)

 

2) The default life cycle is used to build a project.

 

3) The site lifecycle is used to create a project site.

 

Clean Lifecycle

 

Pre-clean

Preparations before cleaning

Clean

To clear the target folder.

Post-clean

Preparations after cleaning

 

Default Lifecycle

 

Validate

Verify that the project is correct and that all information necessary for the complete build is available

Initialize

 

Generate-Sources

Generate allSource code

Process-Sources

Processing SourceCodeFor example, filter some values.

Generate-Resources

Generate all resource files that need to be included in the packaging process

Process-Resources

Copy and process resource files to the target directory and prepare for packaging

Compile

Compile the source code of the project

Process-classes

Process compiled files, such as byte code enhancement for Java classes

Generate-test-Sources

Generate all test source code contained in the test compilation process

Process-test-Sources

Process the test source code, such as filtering some values

Generate-test-Resources

Generate the resource file required for the test

Process-test-Resources

Copy and process the test resource file to the test target directory.

Test-compile

Compile the test source code to the test target directory.

Process-test-classes

 

Test

Use a suitable unit test framework to run tests that do not require code to be packaged or released

Prepare-Package

Before packaging, perform the necessary operations to prepare for packaging.

Package

Package compiled code into a distributable format, such as jar, war, or ear.

Pre-integration-test

Perform the actions required before the integration test is run, such as creating the environment required for the integration test.

Integration-test

If necessary, process the package and release it to an environment where the integration test can run.

Post-integration-test

Perform the actions required after the integration test is run, such as clearing the integration test environment.

Verify

All checks are performed. The verification package is valid and complies with quality specifications.

Install

Install the installation package to the local repository for use by other local projects.

Deploy

Copy the final package to the remote repository and share it with other developers and projects.

 

Site Lifecycle

 

Pre-site

 

Site

 

Post-site

 

Site-deploy

 

 

 

Note: in different stages of each life cycle, if the latter stage is executed, the previous stage is automatically executed.

For example

MVN clean is equivalent to executing pre-clean and clean

MVN test is equivalent to executing validate --> initialize -->... --> Compile -->... ---> Test

Warehouse

In the maven world, any dependency, plug-in, or project build output can be called components. Each component has a set of unique coordinates.

 

Thanks to the coordinate mechanism, any Maven project uses any component in the same way. On this basis, Maven can store all the components shared by Maven projects in a unified manner at a specific location, which is the repository.

 

For Maven, there are only two types of warehouses: Local warehouses and remote warehouses. When Maven looks for a component based on the coordinates, it first looks at the local repository. If the local repository has this component, it is used directly. If the local repository has this component, maven will go to the remote repository to find the required components and download them to the local repository for use.

 

Dependency management (the most powerful feature of Maven)

What is dependency management )?

 

To put it simply, for example, our Java projects generally rely on other packages. In the maven world, these dependent packages are called dependency, as configured in our configuration file at the beginning, dependency is the coordinates of other projects.

 

Transitive Dependencies)

 

In Maven, a dependency is not just a jar. It is a pom file, which may also declare dependencies on other components. The dependencies of these dependencies are called Transmission dependencies. The Maven repository not only stores binary files, but also stores the constructed metadata (metadata)

 

For example, if your project depends on one library, and this library depends on five or ten other libraries (like spring or hibernate ). You don't have to figure out all these dependencies and write them in your pom. in XML, you only need to add the libraries you directly depend on. MAVEN will implicitly Add the libraries indirectly dependent on these libraries to your project.

. Maven also handles conflicts in these dependencies, and allows you to customize default behaviors, or exclude some specific transmission dependencies.

 

Maven also provides different dependency scope, such as JUnit configured in the previous project.

<Dependency>

<Groupid>JUnit</Groupid>

<Artifactid>JUnit</Artifactid>

<Version> 4.7 </version>

<Scope> test </scope>

</Dependency>

The scope is test, which indicates that it is added to the classpath only when the test is compiled.

 

Note:

When a jar file is created for a project, its dependencies are not bundled into the generated component. They are only used to compile

Translation. When you use Maven to create war or ear, you can configure Maven to bind dependencies to the generated component,

You can also configure Maven and use the provided range to exclude specific dependencies in the war file. Provided fan

Wai tells Maven that a dependency is required during compilation, but it should not be bundled in the build output. When you

When developing a web application, the provided range becomes very useful. You need to compile your code through the servlet API.

But you do not want the servlet API jar package to be included in the WEB-INF/lib directory of your web application.

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.