Maven Frequently Asked Questions

Source: Internet
Author: User
Tags inheritance sonatype
1. Preface

Maven, pronunciation is [' meivin], ' expert ' means. It was a good project management tool, and it was early in my list of essential tools, but this time I did some deep learning about maven in order to completely migrate the Project1 project and apply maven. The purpose of writing this study note is to write for yourself, and two to be able to learn to use Maven for other people to shorten some time. 2. Maven Overview

First, I quickly combed the MAVEN concept, allowing us to quickly build a more accurate MAVEN application scenario. 2.1 maven is not what

When reading, we should limit the scope and avoid some harmful reverie. To say that Maven is not something, we can start with the following points: Maven is not ant or make.
The build tool that we used to touch, we need to write some detailed steps, such as: Compile Project1/src/*.java and other similar statements. These statements are exactly what we want to write with Ant and make. MAVEN uses a "contract better than Configuration" approach, and some of the commonly used operations and procedures have been cured in Maven, so users no longer have to write those annoying statements. At the same time, MAVEN has built-in support for the development process, which not only compiles, but also packages, publishes, and one go all of these steps. Maven's not ivy.
Dependency management is one of Maven's features, although many people including I used to rely on the management function only, but in order to use it in depth, we can see more content. More importantly, Maven joins the concept of scope in dependency relationships, further refining the partitioning of dependencies. What is 2.2 maven?

MAVEN positions itself as a project management tool. It is responsible for managing almost everything in the project development process: Version
MAVEN has its own version definition and rule building
MAVEN supports many types of applications and defines a set of build rules and toolsets for each supported application type. Output Management
Maven can manage the artifacts of project builds and add them to the user library. This feature can be used for delivery behavior between project groups and other departments. Dependent relationships
Maven makes detailed analysis and partitioning of dependency characteristics to avoid dependency and contamination behavior documents and build results in the development process
The MAVEN site command supports the publication of various document information, including the various outputs of the build process, Javadoc, product documentation, and so on. Project relationship
A large project usually consists of several small projects or modules, and MAVEN can be used to manage migration management easily.
Maven can output different kinds of output for different development scenarios. 2.3 maven's life cycle

Maven divides the construction of the project into different life cycles (lifecycle), which in my opinion is already very carefully divided, as you can see here. Roughly speaking, it is a process (phase) that includes: compilation, testing, packaging, integration testing, validation, deployment. All the execution actions (goal) in MAVEN need to indicate where they are executing in the process, and then Maven executes the process by invoking the goal in turn.

This is also a basic scheduling mechanism for MAVEN. In general, the process later in the location depends on the previous procedure. Of course, MAVEN also provides configuration files that can be skipped at certain stages, depending on user requirements. 2.4 Maven's "convention is better than configuration"

The so-called "contract is better than configuration" is not completely modifiable in Maven, they are just the default values for some configurations. However, users do not need to modify the agreed content unless necessary. The default file storage structure for MAVEN is as follows:/project directory Pom.xml configuration file for Maven/src source code directory/src/main Project source code catalog/src/main/java Project Java source code directory/src/main/resource work /src/test Unit Test Directory/src/test/java/target output directory, all outputs are stored in this directory/target/classes the compiled class file

Each stage of the task knows how to properly complete their work, such as compile task will know from Src/main/java compiled all the Java files, and its output class file stored in target/classes.

For MAVEN, adopting a "contract better than configuration" strategy can reduce the amount of configuration work, reduce the cost of learning, and more importantly, introduce a unified specification to the project. version specification for 2.5 maven

MAVEN uses several features to uniquely locate an output: GroupId:artifactId:packaging:version. Like org.springframework:spring:2.5. The explanations for each section are as follows: GroupId
Groups, companies, groups, organizations, projects, or other groups. The Convention for the Community identity is that it starts with the reverse domain name (reverse domain name) that creates the organization name for the project. The project from Sonatype has a groupid that begins with Com.sonatype, and the Apache software project has org.apache that begins with GroupID. Artifactid
A unique identifier that represents a single item under GroupID. Like our Tomcat, Commons and so on. Do not include the dot number (.) in the Artifactid. Version
A specific version of a project. A published project has a fixed version identity that points to a specific version of the project. Projects under development can be marked with a special identifier, which adds a "SNAPSHOT" to the version.
Although the package format of a project is also an important part of MAVEN coordinates, it is not a part of the project's unique identifier. The groupId:artifactId:version of a project makes it a unique project; You cannot have a project that has the same groupId, Artifactid, and version identities. Packaging
The type of project, by default, is the jar, which describes the output after the project is packaged. A project of type jar produces a jar file, and a project of type War produces a Web application. Classifier
Rarely used coordinates, generally can be ignored classifiers. If you want to publish the same code, but for technical reasons you need to generate two separate artifacts, you will have to use a classifier (classifier). For example, if you want to build two separate artifacts into jars, one using the Java 1.4 compiler and the other using the Java 6 compiler, you can use a classifier to generate two separate jar artifacts that have the same groupId:artifactId:version combination. If your project uses a local extension class library, you can use the classifier to generate a widget for each target platform. Classifiers are commonly used in source code, Javadoc, or binary collections of packaged components.

MAVEN has its own version specification, which is generally defined as <major Version>.<minor version>.<incremental version>-<qualifier>, Like 1.2.3-beta-01. To illustrate, Maven's own judgment version of the algorithm is the major,minor,incremental part with a number comparison, qualifier part with a string comparison, so be careful alpha-2 and alpha-15 comparative relationship, the most useful The format of the alpha-02.

Maven can use several special string SNAPSHOT, LATEST, release when versioning. such as "1.0-snapshot". The meanings and processing logic of each part are as follows: SNAPSHOT
If a version contains the string "SNAPSHOT", Maven expands the symbol to a date and time value, converted to UTC time, when the component is installed or released. For example, "1.0-snapshot" will become 1.0-20100505-141000-1 when it is released on May 5, 2010 2:10 P.M..
This word can only be used in the development process, because in general, the project team will frequently release some versions, finally, when the actual release, will be in these snapshot version of the search for a stable, for the official release, For example, before the release of the 1.4 release, there will be a series of 1.4-snapshot, and the actual release of 1.4, is also a stable version from it. LATEST
Refers to the latest release of a particular artifact, which may be a release or a snapshot version, depending on which time is last. RELEASE
Refers to the last released version. part of 2.6 maven

Maven divides the entire MAVEN-managed project into sections, one for the source code, the source code itself, the various resources involved, one for the unit test case, and one for the various Maven plugins. For these parts, MAVEN can manage them independently, including various external dependencies. 2.7 Maven's dependency management

Dependency management is typically the most appealing feature of Maven, which allows developers to focus on the direct dependencies of the code, such as when we use spring, to add spring dependency instructions, and what external things spring relies on, and Maven helps us get it done.

Any external dependency description includes the following elements: GroupId, Artifactid, version, scope, type, optional. Among them the first 3 are necessary, the respective meanings are as follows: GroupId must artifactid must be version must.
The version here can be represented by an interval expression, such as (2.0,) representing >2.0,[2.0,3.0), which means that 2.0<=ver<3.0; multiple conditions are separated by commas, such as [1,3], [5,7]. Scope scope restriction type typically occurs when a pom reference is dependent, and other times it is not optional optional dependency

Maven believes that the dependencies of the program will vary depending on the stage and application scenario of the program, so dependencies in Maven have scope (scope) limitations. In Maven, scope contains the following values: Compile (compilation scope)  
Compile is the default scope; If a scope is not provided, the scope of the dependency is the compilation scope. Compilation scope dependencies are available in all classpath, and they are also packaged. Provided (scope provided)  
provided dependencies are used only when the JDK or a container has provided the dependency. For example, if you develop a Web application, you might need the Servlet API available in the compilation Classpath to compile a servlet, but you do not want to include the Servlet API in the packaged war, the Servlet API The jar is provided by your application server or the servlet container. A dependency that has been provided for scope is available at compile classpath (not at runtime). They are not transitive and will not be packaged. Runtime (runtime Range)  
Runtime relies on running and testing the system, but it is not required at compile time. For example, you might only need the JDBC API JAR when compiling, and only the JDBC driver implementation is required when running. Test range  
Test range dependencies are not required for general compilation and runtime, they are only available during the test compilation and test run phases. The test scope depends on the previous??? described in. System-wide  
Systems-scoped dependencies are similar to provided, but you must explicitly provide a path to the jar file in the local system. This is done to allow compilation based on local objects, which are part of the System class library. Such artifacts should always be available, and MAVEN will not look for it in the warehouse.   If you set a dependency scope to a system scope, you must provide a SYSTEMPATH element at the same time  . Note that this range is deprecated (you should always try to reference dependencies from a public or custom Maven repository).

In addition, the code has its own dependencies, and the plugins used by each MAVEN can have their own dependencies. Dependencies can also be optional, such as there is no cache dependency in our code, but hibernate may have to configure the cache, so the cache's dependency is optional. 2.8 Multi-project management

MAVEN's multi-project management is also very powerful. In general, MAVEN requires that all sub-projects of the same project be placed in the same directory, with each subdirectory representing a project, such as the Pom profile subproject of the total project/Pom.xml project, and the Pom file sub-project of subproject 1 1/pom.xml 2/pom.xml Pom files for sub-item 2

According to this format, is the way of inheritance, all the specific sub-project Pom.xml will inherit the contents of the total project Pom, the value of the sub-project POM content first.

To set the inheritance method, first add the following configuration to the total project's POM < modules >
< module > Simple-weather </module >
< module > Simple-webapp </module >
</Modules >

Next Add < parent > in each sub-project
< groupId > org.sonatype.mavenbook.ch06 </groupId >
< Artifactid > simple-parent </artifactid >
< version > 1.0 </version >
</Parent >

Can.

Of course, inheritance is not the only common way to configure files, and Maven also supports reference methods. The way to reference Pom is simpler, and adding a type to the dependency is a dependency of the Pom.
< Project >
< description > This is a project requiring JDBC </description >
...
< dependencies >
...
< dependency >
< groupId > Org.sonatype.mavenbook </groupId >
< Artifactid > persistence-deps </artifactid >
< version > 1.0 </version >
< type > Pom </type >
</dependency >
</dependencies >
</Project >2.9 Properties

Users can define some properties in Maven and then use ${xxx} to refer to them elsewhere. Like what:
< Project >
< modelversion > 4.0.0 </modelversion >
...
< properties >
< var1 > value1 </var1 >
</Properties >
</Project > MAVEN provides three implicit variables for accessing system environment variables, POM information, and maven Settings:env
Expose the operating system's environment variables, such as Env. PATH Project
Exposing the contents of the Pom with a dot number (.) The path to reference the value of the POM element, such as ${project.artifactid}. In addition, Java's system attributes, such as User.dir, are also exposed here. Settings
Exposing Maven's settings information, you can also use the dot (.) To reference. Maven stores the system configuration file in the Maven installation directory and stores user-related profiles in ~/.m2/settings.xml (Unix) or%userprofile%/.m2/settings.xml (Windows). 2.10 Maven's profile

Profile is an important feature of Maven that allows Maven to automatically adapt to external environmental changes, like a project, build Linux under Linux, build win under win, and more. A project can have multiple profiles set up, or multiple profiles can be activated (active) at the same time. The conditions of the automatically activated profile can be a variety of setting conditions, grouped in the activation node, or directly from the command line. The other configuration content that the profile contains can override the corresponding value for the POM definition. If you think the profile setup is complicated, you can move all the profiles content to a dedicated profiles.xml file, but remember to put it together with Pom.xml.

The following are common in the activation conditions in the activation node: OS
Determine the operating system related parameters, it contains the following can be freely combined child node element message-the message displayed after the rule failed arch-matching CPU structure, commonly known as x86 family-matching operating system family, the common value is: dos,mac,netware,os/ 2,unix,windows,win9x,os/400. Name-matches the operating system's names version-matches the operating system version number display-information that is displayed after the operating system is detected

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.