The first time we've run Maven, it's time to introduce some of the core concepts of maven. In the previous example, we gave birth to a project that contains a pom and some source code that together make up the MAVEN standard directory layout. Then you run maven using the life cycle phase (phase) as a parameter, which prompts Maven to run a series of Maven plug-in targets. Finally, we install the Maven component (artifact) to your local repository (repository). Wait a minute? What is the life cycle? What is a "local warehouse"? The following summary
Describes some of the core concepts of maven.
1:maven plug-ins and targets (Plugins and goals)
In the previous article, we ran MAVEN with two types of command-line arguments. The first command is a single plug-in target, the archetype plugin's generate target. Maven second run is a life cycle phase –install. To run a single MAVEN plug-in target, we use the
MVN archetype:generate This syntax, where archetype is a plug-in identifier and generate is the target identity. When Maven runs a plug-in target, it prints out the plug-in identity and target identity to the standard output:
A Maven plugin is a collection of single or multiple targets. The Maven plugin example has some simple but core plugins, like the jar plugin, which contains a set of targets for creating jar files, the compiler plugin, which contains a set of targets for compiling source code and testing code, or surefire plugins, It contains a set of targets for running unit tests and generating test reports. Other, more specialized plugins include the Hibernate3 plugin, which integrates the popular persistence framework Hibernate,jruby plugin, which allows us to run Ruby as part of a MAVEN build or to write Maven plugins with Ruby. MAVEN also provides the ability to customize plugins. A custom plugin can be written in Java, or in some other language such as Ant,groovy,beanshell and the previously mentioned Ruby.
A goal is a clear task that can be run as a separate target, or as part of a large build with other targets. One goal is a "unit Ofwork" in Maven. Examples of goals include the compile target in the compiler plugin, which compiles all the source files in the project, or the test target in the Surefire plug-in to run unit tests. The target is configured with configuration properties to be used to customize the behavior. For example, the compile target of the compiler plugin defines a set of configuration parameters that allow you to set the target JDK version or choose whether to use compile optimizations. In the previous example, we passed the command-line arguments-dgroupid=com.gao.test and-dartifactid= HelloWorld passed GroupID and Artifactid configuration parameters to the generate target of the archetype plug-in. We have no generate target passed in the PackageName parameter, and its value is Org.sonatype.mavenbook. We ignored the PackageName parameter, so the default value for the package name is Com.gao.test
Attention:
When it comes to a plug-in target, we often use shorthand notation: pluginid:goalid.
For example, when referring to the generate target of the archetype plugin, we wrote Archetype:generate.
The target defines parameters that define some sensible defaults. In the case of archetype:generate, we did not specify what type of archetype to create on the command line, we simply passed in a groupid and a artifactid. This is our first stroke of agreement over configuration (Convention over config). Here generate the Convention of the target, or the default value, is to create a simple project called QuickStart. The generate target defines a configuration property Archetypeartifactid, which has a default value of Maven-archetype-quickstart. Quickstart archetype generates a shell of the smallest item, including a Pom and a class. The archetype plugin is much more powerful than the first example, but it's a good way to start a new project quickly. After that we will see that the archetype plug-in can be used to build complex projects such as Web applications, and how to use the archetype plugin to define a collection of our own projects. The core of Maven is almost completely unknown to the specific tasks in our project building. In itself, Maven doesn't know how to compile your code, it doesn't even know how to make a jar file, it proxies all of these tasks to Maven plugins like the compiler plugin and jar plug-ins, which are downloaded when needed and regularly updated from the MAVEN central repository. When we download maven, we get a maven core with a basic shell that knows how to parse the command line, manage Classpath, parse the pom file, and download the Maven plugin when needed. By keeping the compiler plugin separate from the MAVEN core and providing an update mechanism, it is easy to use the latest version of the compiler. In this way, the MAVEN plug-in provides global reusability of the common build logic, with no compilation tasks defined during the build cycle, and a compiler plugin that uses all MAVEN users to share. If there is an improvement to the compiler plug-in, each project that uses Maven can immediately benefit from this change. (And, if we don't like the compiler plugin, we can overwrite it with your implementation).
2:maven life cycle (Lifecycle)
We run the command MVN package. Instead of specifying a plug-in target, the command line specifies a maven life cycle stage. A phase is a step in what Maven calls a "build lifecycle." The life cycle is a series of sequential phases that are contained in a project build. Maven can support many different lifecycles, but the most common life cycle is the default Maven life cycle, where the first phase of the lifecycle is to validate the basic integrity of the project, and the final stage is to publish a project as a product. The phases of the life cycle are specifically left vague, defined individually as validation (validation), test (testing), or Release (deployment), and they mean different things to different projects.
For example, this phase of packaging (package) generates a jar in a project, which also means "to make a project into a jar", while in another project, the packaging phase may generate a war file. The "One Life cycle is a sequence of some stages" shows the simplicity of the default Maven life cycle.
The plug-in target can be attached to the life cycle phase. As Maven moves along the life cycle stage, it executes targets attached to a specific phase. Each phase may have 0 or more targets bound. In the previous section, when we ran the MVN package, we might have noticed that more than one target was executed. After checking the output after running the MVN package, you will notice the various targets that are running. When this simple example arrives at the package stage, it runs the jar target of the jars plugin. Since our simple QuickStart project (default) package type, the Jar:jar target is bound to the packaging stage.
We know that in a project with a package type of jar, the packaging phase creates a jar file. But what are the goals before it, like Compiler:compile and surefire:test? The following targets are executed sequentially after the life cycle passes through the default life cycle of the end:
Resources:resources
The resources target of the resources plugin is bound to the resources stage. This target replicates all resources under Src/main/resources and any other configured resource directory to the output directory.
Compiler:compile
The compile target of the compiler plugin is bound to the compile phase. This target compiles all source code and any other configured resource directory under Src/main/java to the output directory.
Resources:testresources
The testresources target of the resources plugin is bound to the test-resources phase. This target replicates all resources under src/test/resources and any other configuration of the test resource directory, to the test output directory.
Compiler:testcompile
The testcompile target of the compiler plugin is bound to the test-compile phase. This target compiles the test cases under Src/test/java and any other configuration of the testing resource directory to the test output directory.
Surefire:test
The test target of the Surefire plugin is bound to the test phase. This target runs all the tests and creates the output files that capture the detailed test results. By default, this target terminates if a test fails.
Jar:jar
The jar target of the jar plugin is bound to the package stage. This target packages the output directory into a jar file.
To sum it up, when we run MVN Package,maven all phases of running to packaging, it runs all the targets bound at each stage as Maven steps forward along the life cycle. You can also explicitly specify a series of plug-in targets as follows to get the same result:
MVN resources:resources \
Compiler:compile \
Resources:testresources \
Compiler:testcompile \
Surefire:test \
Jar:jar
Running the package phase keeps track of all the targets contained in a particular build, and it also allows each project to use MAVEN to follow a well-defined set of standards. This life cycle allows us to jump from one MAVEN project to another MAVEN project without having to know the details of each project build. If we can build a MAVEN project, then we can build all of the MAVEN projects.
3:maven coordinates (coordinates)
The archetype plugin created a project with a file named Pom.xml. This is the Project object Model (POM), a declarative description of the project. When Maven runs a target, each goal accesses the information defined in the Project Pom. When a Jar:jar target needs to create a jar file, it finds out the name of the jar file by observing the POM. When the Compiler:compile task compiles the Java source code into bytecode, it looks at the Pom to see if there are parameters for the target. The target runs in the context of the POM. The goal is the action we want to run against the project, and the project is defined by the POM. Pom names The project, provides a unique set of identifiers (coordinates) for the project, and defines the relationships with other projects through dependencies (dependencies), parent (parents), and prerequisites (prerequisite). Pom can also customize plugin behavior to provide information about the community and developers involved in the project.
A plugin in the Mavenpom. Take a look at the pom below.
Coordinates of the HelloWorld project
We circled the coordinates of this project in frames: GroupId, Artifactid, version and packaging. The identifiers of these combinations are spelled into the coordinates of an item. Just like any other coordinate system, a MAVEN coordinate is an address, a point in "space": from General to special. When a project is associated with a dependency, a plug-in, or a parent project reference and another project, MAVEN uses coordinates to pinpoint a project. Maven coordinates are usually written as delimiters using colons, such as the format: GroupId:artifactId:packaging:version. In the above pom.xml, its coordinates can be expressed as com.gao.test:helloworld:jar1.0-snapshot. Version, which contains a dependency on junit:junit:jar:3.8.1.
GroupId
D 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.
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. The package format of a project is also an important part of MAVEN coordinates, but 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. In the vast space of other "maven" projects, these four elements are a key factor in locating and using a particular project. The Maven repositories (repositories) (public, private, and local) are organized by these identifiers. When a project is installed in a local MAVEN repository, it can be used by any other project at once. All we need to do is to use Maven's unique coordinates in other projects to add dependency on this particular component.
4:maven Warehouse (repositories)
When we run Maven for the first time, we notice that Maven has downloaded a lot of files from a remote maven repository. If this simple project is the first time you run Maven, then the first thing it does when it triggers the resources:resource target is to download the latest version of the resources plugin. In Maven, artifacts and plugins are taken from a remote repository when they are needed. The initial maven download package size is quite small (1.8 trillion), one of the reasons is that the initial MAVEN does not include many plugins. It contains only near-naked minimum values and is then taken from the remote repository when needed. Maven comes with a remote repository address (http://repo1.maven.org/maven2) to download the MAVEN core plugin and dependencies. We often write a project that relies on packages that are neither free nor public. In this case, we need to either install a custom warehouse in your organization's network, or manually install these dependencies. The default remote repository can be replaced, or a reference to a custom Maven repository maintained by our own organization is added. There are many ready-made projects that allow organizations to manage and maintain images of public maven warehouses. What makes the MAVEN repository a maven repository? The MAVEN repository is defined by structure, a MAVEN repository is a collection of project artifacts that are stored under a directory structure, and their format can easily be understood by Maven. In a Maven repository, everything is stored in a directory structure that matches the MAVEN project coordinates. We can open the browser and then browse the central Maven repository http://repo1.maven.org/maven2/to see this structure. You will see that the components with the coordinates org.apache.commons:commons-email:1.1 can be/org/apache/commons/commons-email/1.1/in the directory. The file name Commons-email-1.1.jar is found below. The standard for MAVEN repositories is to store artifacts in the following directory format, relative to the repository's root directory:
////-. Maven downloads widgets and plugins from remote repositories to your home, stored in our local maven repository. Once maven has downloaded a widget from a remote repository, it will never need to be downloaded again because MAVEN will first look for the plug-in in the local repository and then the other place. On Windows XP, your local repository is likely to be on C:\Documents and settings\username.m2\repository, on Windows Vista, will be C:\Users\USERNAME.m2\ Repository On UNIX systems, our local repositories are ~/.m2/repository. When you create a simple project like the one created in the previous section, the install phase executes a target and installs the artifacts of your project to our local repository. In your local repository, we should be able to see the artifacts created by our simple project. If we run the MVN install command, MAVEN installs the artifacts of our project to the local repository. Try it.
Since we have used this command in the last article, we will not repeat the demonstration here. As we can see from the output of this command, MAVEN installs our project's jar file to our local maven repository. Maven shares dependencies through a local warehouse in a local project. If we have developed two projects-Project A and project b--project B depends on the artifacts generated by project A. When you build project B, Maven gets the artifact of project A from the local repository. The MAVEN repository is both a cache of components downloaded from a remote repository and allows your projects to depend on each other.
5:maven Dependency Management (Dependency Management)
In the simple example in this chapter, MAVEN handles the JUnit-dependent coordinate--junit:junit-3.8.1, pointing to/junit/junit/3.8.1/junit-3.8.1.jar in the local maven repository. This ability to locate components based on MAVEN coordinates allows us to define dependencies in the project's Pom. If we examine the Pom.xml file of the simple project, we will see that there is a section in the file that deals with dependencies, which contains a separate dependency--junit. A complex project will contain a lot of dependencies, and it may also contain dependencies that depend on other artifacts. This is one of the most powerful features of MAVEN, and it supports transitive dependencies (transitive dependencies). If our project relies on a library that relies on five or 10 other libraries (like spring or hibernate). We don't have to find all these dependencies and write them in your pom.xml, just add the libraries you rely on directly, and MAVEN implicitly adds the libraries that indirectly depend on them to your project. MAVEN also handles conflicts in these dependencies, while allowing you to customize the default behavior or exclude certain transitive dependencies. Let's take a look at the dependencies that you downloaded to your local repository when you ran the previous sample.
Take a look at this directory: ~/.M2/REPOSITORY/JUNIT/JUNIT/3.8.1/. There will be files Junit-3.8.1.jar and Junit-3.8.1.pom, as well as a checksum file that Maven uses to verify the accuracy of the downloaded artifacts. It is important to note that Maven does not just download the JUnit jar file, it also downloads a pom file for this junit dependency. This behavior of maven downloading artifacts and pom files at the same time is important for Maven to support transitive dependencies. When we install the artifacts of the project into the local repository, we find that in the same directory as the jar file, MAVEN publishes a slightly modified version of Pom.xml. Storing the Pom file in the warehouse provides other projects with information about the project, the most important of which is its dependencies. If Project B relies on project A, it also relies on the dependency of Project A. When Maven handles dependency artifacts through a set of MAVEN coordinates, it also acquires the POM, a dependent pom, to find transitive dependencies. Those transitive dependencies are added to the dependency list of the current project. A dependency in Maven is more than just a jar. It is a pom file, and this POM may also declare a dependency on other artifacts. These dependent dependencies are called transitive dependencies, and Maven repositories not only store binaries, but also stored these built metadata (metadata) to make transitive dependencies possible. Presents a possible scenario for transitive dependencies.
6: Site builds and reports (site Generation and Reporting)
Another important feature of MAVEN is that it can generate documents and reports. In the simple project directory, run the following command:
MVN site
Life cycle phase. Unlike the default life cycle, it manages code generation, manipulating resources, compiling, packaging, and so on. The site lifecycle only cares about the site content that is processed in the Src/site directory, as well as generating reports. After this command has been run, you will see a Project Web site in the Target/site directory. Load target/site/index.html you will see the basic appearance of the project site. It contains reports that are on the left-hand side of the navigation directory under "Project Reports". It also contains information about the project, dependencies, and related developer information, under "project information." The Web site for simple projects is mostly empty because the POM contains only less information, only project coordinates, names, URLs, and a test dependency. On this site, we will notice that some of the default reports are already accessible, and a report detailing the results of the tests. This unit test report describes the success and failure information for all unit tests in the project. Another report generates a Javadoc for the project API. MAVEN provides a very complete configurable report, such as the Clover Report examines unit test coverage, JXR reports generate HTML source code to reference each other, which is useful during code review, and the PMD report analyzes the source code for various coding problems. The Jdepend report analyzes dependencies between packages in the source code. By configuring those reports in Pom.xml to be included in the build, site reports can be customized. Finally we will find the following files in the project Helloword\target\site directory
Open index.html
Such as
Here are some of the core concepts of maven.
The core concepts in Maven