Maven learning – first glimpse

Recently, I worked as an intern in a company. I still don't know the development process of the company because I just left school. I know little about Maven. I've been mending Maven recently.

Maven definition

Maven is a project management tool, which includes a project object model, a set of standards, a project lifecycle, a dependency management system, and logic used to run plug-in goals defined in the life cycle phase. When you use maven, you use a well-defined project object model to describe your project, and then Maven can apply crosscutting logic, These logic come from a set of shared (or customized) plug-ins. Maven is a software project management tool based on the project object model (POM), which can manage the construction, reporting and documentation of the project through a short description information.

Without customization, the source code is assumed to be ${basedir} / SRC / main / Java, and the resource file is assumed to be ${basedir} / SRC / main / resources. The test code is assumed to be in ${basedir} / SRC / test. The project assumes that a jar file will be generated. Maven assumes that you want to put the compiled bytecode in ${basedir} / target / classes and create a distributable jar file in ${basedir} / target. Although this may seem trivial, think about the fact that most ant based builds must define these directories for each subproject. Maven's application of convention over configuration is not just a simple directory location. Maven's core plug-in uses a set of general conventions to compile source code, package distributable components, generate web sites, and many other processes. Global reuse of Maven plug-ins

The core of Maven is to parse some XML documents and manage the life cycle and plug-ins. Maven is designed to delegate the main responsibilities to a group of Maven plug-ins, which can affect the Maven life cycle and provide access to the target. Most of Maven's actions take place in the goals of Maven plug-ins, such as compiling source code, packaging binary code, publishing sites and other construction tasks. Maven downloaded from Apache does not know how to package war files or run unit tests. Most of Maven's intelligence is realized by plug-ins, which are obtained from Maven repository. The first time you run a command like MVN install with a new Maven installation, it will download most of the core Maven plug-ins from the central Maven repository. This is not only a technique to minimize the size of Maven distribution package, but also allows you to upgrade plug-ins to improve the construction ability of your project. The fact that Maven gets dependencies and plug-ins from a remote repository allows global reuse of the build logic.

Dependency management

Because the project is defined according to a unique coordinate containing group identifier, component identifier and version. These coordinates can be used between projects to declare dependencies on remote warehouses

Related to project dependencies, we can use the coordinates defined in the project object model (POM) to create a warehouse of Maven components.

The difference between ant and maven

Apache Ant

Ant has no formal agreement, such as the directory structure of a general project. You must clearly tell ant where to find the source code and where to place the output. Over time, informal conventions have emerged, but they have not yet been modeled in the product.

Ant is programmed. You must clearly tell ant what to do and when to do it. You have to tell it to compile, then copy, and then compress.

Ant doesn't have a lifecycle. You have to define goals and dependencies between goals. You must manually attach a task sequence to each goal.

Apache Maven

Maven has conventions because you follow them and it already knows where your source code is. It puts the bytecode into target / classes, and then generates a jar file at target.

Maven is declarative. All you need to do is create a POM XML file, and then put the source code in the default directory. Maven will help you with other things.

Maven has a life cycle that is called when you run MVN install. This command tells Maven to perform a series of orderly steps until you reach the specified life cycle. One effect of traversing the life cycle is that Maven runs many default plug-in goals that do things like compiling and creating a jar file.

Maven provides built-in intelligence for some general project tasks in the form of plug-ins. If you want to write and run unit tests, all you need to do is write tests, put them in ${basedir} / SRC / test / Java, add a test scope dependency on TestNG or JUnit, and then run MVN test. If you want to deploy a web application instead of a jar, all you need to do is change your project type to war, and then set your document root directory to ${basedir} / SRC / main / webapp. Of course, you can do these things with ant, but you will need to write these instructions from scratch. Using ant, you first need to determine where the JUnit jar file should be placed, then you need to create a classpath containing the JUnit jar file, then tell ant where it should find the test source code, write a target to compile the test source code into bytecode, and use JUnit to execute unit tests

Simple use statement

MVN - V view version compile compile test test package package package clean target install install jar package into local warehouse

Automatically create directory skeleton

The archetype plug-in is used to create a maven compliant directory skeleton. There are two ways to create a directory

Method 1: archetype: generate select according to the prompt method 2: archetype: generate - dgroupid = organization name, reverse write of company website + project name - dartifactid = project name - module name - dversion = version number - dpackage = package name where the code exists

Other knowledge points

Coordinate component warehouse is divided into local warehouse and remote warehouse mirror warehouse clean project pre clean work before cleaning clean all files generated in the last build post clean files after cleaning site generate project site pre site work to be completed before generating project site generate project site site document post site Completed work site deploy publishes the generated site to the server dependency range dependency conflict dependency transfer aggregation and inheritance. Other knowledge points will be introduced in detail later..

summary

The above is all about Maven learning - getting a glimpse of the door. It gives a brief introduction to Maven. If there is anything wrong, you are welcome to leave a message and point out that Xiaobian can also learn. Interested friends can continue to refer to this site: three methods of running Java main with maven, Java using future to obtain multi-threaded running results in time, etc. thank you for reading. I hope it will help you.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>