Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the basic use of maven

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

What is the basic use of maven? I believe many inexperienced people are at a loss about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

What is maven?

Apache Maven is a set of software engineering management and integration tools. It is based on the concept of Engineering object Model (Project Object Model, POM). Through a central information management module, Maven can manage project construction, reports and documents. To put it bluntly, the maven project is to achieve project management through a pom.xml, project testing, packaging, deployment and other functions, a good use of the maven plug-in, almost twice the result with half the effort! Maven is a Java-based tool, so the first thing to do is to install JDK.

Maven features:

Maven can help developers do the following:

Construction

Document generation

Report

Dependence

SCMs

Publish

Distribute

Mailing list

In general, Maven simplifies and standardizes the construction process of the project. It seamlessly integrates compilation, release, document generation, teamwork, and other tasks. Maven improves reusability and takes care of most of the build-related tasks.

Developers no longer need to care about every configuration detail. Maven provides reasonable default behavior for projects. When you create a Maven project, Maven creates a default engineering structure. Developers only need to place files reasonably, and there is no need to define any configuration in pom.xml.

Maven uses conventions instead of configuration, which means that developers no longer have to create their own build process. The maven built-in variables are as follows:

${basedir} represents the project root directory, that is, the directory containing pom.xml files; ${version} represents the project version; ${project.basedir} is the same as ${basedir}; ${project.baseUri} indicates the project file address; ${maven.build.timestamp} indicates the project component start time ${maven.build.timestamp.format} represents the display format of the attribute ${maven.build.timestamp}. The default value is yyyyMMdd-HHmm, and its format can be customized. Its type can be found in java.text.SimpleDateFormat. ${project.build.directory} represents the main source code path; ${project.build.sourceEncoding} represents the encoding format of the main source code; ${project.build.sourceDirectory} represents the main source code path; ${project.build.finalName} represents the name of the output file; ${project.version} represents the project version, which is the same as ${version}; ${project.xxx} the contents of any node in the current pom file ${env.xxx} gets the system environment variables. ${settings.xxx} refers to the value of the corresponding element in settings.xml.

A project has only one pom file, so how do you uniquely mark the current project?

All POM files require the project element and three required fields: groupId, artifactId,version.

The project in the warehouse is identified as groupId:artifactId:version

The root element of POM.xml is project, which has three main child nodes:

Com.companyname.project-group project 1.0groupId: company domain name backwards artifactId: current project name version: version number

When performing a task or target, Maven looks for the POM in the current directory, reads the required configuration information from it, and then executes the target. Some of the configurations that can be set in POM are as follows:

Project dependencies

Plugins

Goals

Build profiles

Project version

Developers

Mailing list

For more details, you can go to the directory where pom.xml is located and execute the following mvn command mvn help:effective-pom to see the complete content.

Maven lifecycle

Maven abstracts and defines the process of build, which is called the lifecycle of build. The life cycle (lifecycle) consists of multiple phases (phase), each of which is linked to one or more goal. Goal is the smallest unit of tasks defined in maven. Goal is divided into two categories, one is bound to phase, which is executed to a certain phase, then the goal will be triggered, and the other type is not bound, that is, individual tasks, which is equivalent to target in ant.

Taking the clean lifecycle as an example, when we execute the mvn post-clean command, Maven invokes the clean lifecycle, which consists of the following phases:

Pre-clean: perform some work that needs to be done before clean

Clean: removes all files generated by the last build

Post-clean: perform some work that needs to be done immediately after clean

The clean in mvn clean is the clean above. In a lifecycle, when you run a phase, all previous phases are run, that is, if you execute mvn clean, you will run the following two lifecycle phases:

Pre-clean, clean

If we run mvn post-clean, we run the following three lifecycle phases:

Pre-clean, clean, post-cleanMaven build configuration file

Using the build configuration file, you can customize the build method for different environments, such as production (Production) and development (Development) environments.

1. Pom file settings

Configuration files are specified using activeProfiles or profiles elements in the pom.xml file and can be triggered in a variety of ways. The configuration file modifies the POM at build time and is used to set parameters to different target environments (for example, addresses of database servers in development (Development), test (Testing), and production (Production) environments).

Scenario: the configuration files in the development environment are definitely different from those in the production environment. How can you control them through profile?

Suppose there are three test files under the src/main/resources folder:

Then make the following configuration under the pom.xml file:

Normal org.apache.maven.plugins maven-antrun-plugin 1.8 test Run Using env.properties Prod org.apache.maven.plugins maven-antrun-plugin 1.8 Test run Using env.prod.properties

Use pom.xml to define different profile, and use the maven command to activate different profile.

Mvn test-Pnormal

An environment with id of normal will be activated

two。 Activate the profile through Maven settings

Open the settings.xml file under the% USER_HOME%/.m2 directory, where% USER_HOME% represents the user's home directory. If the setting.xml file does not exist, copy% M2_HOME%/conf/settings.xml directly to the .m2 directory, where% M2contains home% represents the Maven installation directory.

Configure the setting.xml file to add properties:

... Test

Execute the command:

Mvn test

At this point, there is no need to use-Ptest to enter parameters, the above setting.xml file has specified the test parameter instead.

In addition, it can also be activated by environment variables.

In general, you can set the following repository source in the setting file, and you can quickly download the corresponding jar package

After reading the above content, alimaven aliyun maven http://maven.aliyun.com/nexus/content/groups/public/ central, have you mastered the basic method of using maven? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report