1
About
Maven
I recently learned some knowledge about Maven, and I feel that this tool is quite useful. I would like to sum up it to prevent forgetting. Maven is a software project management tool that manages project building, reporting, and documentation through a description. It contains a project object model, a set of standard sets, a project lifecycle, and a dependency management system ), and the logic used to run the plug-in (GOAL) defined in the lifecycle phase (phase. When you use Maven, you use a clearly defined project object model to describe your project. Then Maven can apply the cross-cutting logic, these logics come from a group of shared (or custom) plug-ins. Project Home Address: http://maven.apache.org/
2
, Configuration file
The main configuration files of maven2 include pom. xml and settings. xml.
2.1 Pom. xml
We know that Maven projects, dependencies, build configurations, and components are all objects to be modeled and presented. These objects are described through the Pom. xml file. This pom tells Maven what type of project it is processing, how to modify the default behavior to generate output from the source code, and so on. This file is a descriptive statement of a Maven project. It is also a "map" that you need to understand when building a Maven project ".
It contains the following basic items:
- Poject is a top-level element of POM. xml.
- Modelversion: This element indicates that the POM uses the object model of that version. The version of this model often changes, but this change aims to make the model more stable.
- The groupid element indicates the unique identifier of the organization or team that creates the project, which is also a key identifier of the project. We recommend that you use the complete domain name of the organization or team. For example, org. Apache. Maven. plugins is the groupid defined for Maven plug-ins.
- Artifactid indicates the basic name of the main product of the project. If the main product of a project is a jar file and the secondary product is a source code package, artifactid is also used as part of the name of the secondary product. A typical product name uses the format <artifactid>-<version>. <extension> (for example, myapp-1.0.jar ).
- Packaging indicates the type of the product (for example, jar, war, and ear ). This element not only indicates the type of the output product, but also indicates part of the lifecycle of the project construction process. The default value of packaging is jar.
- The version element indicates the product version generated by this project. MAVEN has gone a long way to help developers manage the version number. In the future, you will often see snapshot in a version, this indicates that a project is still under development.
- The name element indicates the name displayed for this project. This is often used in Maven documents.
- The URL operator specifies where the project site can be found. This is often used in Maven documents.
- The desription element provides the basic description of this project. This is also commonly used in Maven documentation.
The above are just some basic items in POM. xml. For the complete introduction of POM. XML elements, see:
Http://maven.apache.org/maven-model/maven.html
2.2 settings. xml
In maven2, the configuration uses settings. XML, which replaces the original project. properties and build. properties. There are two levels of configuration in maven2:
- User-level, for operating system login users. Generally, it is in $ home/. m2/. For Windows users, the directory is c: \ Documents ents and Settings \ User Name \. m2 \ Settings. xml.
- Global level: Generally, in % m2_home %/CONF/settings. XML, m2_home is the name of the environment variable in the root directory of maven2.
You can configure settings. XML, such as local repository and proxy.
3
, Directory structure
The standard directory structure of maven2 is as follows:
You can use directory templates to make Pom. XML more concise. Because maven2 has predefined related actions based on the default directory, without manual intervention. Take the resources directory as an example:
- Src/main/resources, responsible for managing the resources of the project owner. After using maven2 to execute compile, all files and subdirectories in this directory will be copied to the target/classes Directory, which provides convenience for future packaging.
- Src/test/resources, responsible for managing the resources for project testing. After using maven2 to execute test-compile, all files and subdirectories in this directory will be copied to the target/test-classes directory to prepare for subsequent tests.
4
, Installation
Maven2
The steps for installing maven2 are very simple: first download the corresponding software package from the official Maven website http://maven.apache.org/currently Maven 2.0.4; then decompress the package and set the environment variable m2_home = maven2 to the decompress the installation directory; finally, add % m2_home %/bin to path to facilitate Maven to run in any directory. Check whether the installation is complete. Open the DOS window and enter MVN-V. If the following information appears, maven2 is successfully installed:
X:> MVN-V
Maven version2.0.4
5
Common running commands
The running command of maven2 is MVN, and MVN-h can be used to obtain related help information. Common scenarios:
- Create a Maven project: MVN archetype: Create
- Compile source code: MVN compile
- Compile the test code: MVN test-compile
- Run the test: MVN Test
- Site: MVN site
- Package: MVN package
- Install jar: MVN install in local repository
- Clear the generated project: MVN clean
6
, Project development
6.1Create a project
Generate project layout: MVN archetype: Create-dgroupid = com. oreilly-dartifactid = My-app
After creating a project, we can add code to the project and use all the new skills of Maven. Note that the following command must be run in the directory where the Pom. xml file is located.
-MVN test: run the unit test in the application.
-MVN package: generate a jar file based on the project for use when dependent on this project.
-MVN install: add the JAR file of the project to the library,
-MVN site: the website that generates project-related information
-MVN clean: clears the generated results in the target directory.
-MVN Eclipse: generate an Eclipse project file
6.2Introduction of lifecycle
Maven2 has a clear concept of life cycle and provides corresponding commands, making the project build clearer and clearer. Main lifecycle phases:
- Verify that the project is correct and all required resources are available.
- Compile the source code of the project.
- Test-compile: compile the project test code.
- Test: Use the compiled test code to test the compiled source code.
- Package: A published format, such as jar, which packages compiled source code.
- Integration-test: process and release packages in an environment where the integration test can run.
- Verify, run any check to verify whether the package is valid and meets quality standards.
- Install the package in the local repository, which can be used by other projects as dependencies.
- Deploy is executed in the integrated or released environment. The final version package is copied to a remote repository, so that other developers or projects can be shared.
- Generate-sources to generate any additional source code required by the application, such as XDoclet.
If you want to compile the project, enter MVN Compile directly. For other stages, you can do so. There is a dependency between stages, for example, test depends on test-compile. When executing MVN test, the MVN test-compile command is run first, and then the MVN test command is run.
7
, Dependency management
To add a dependency to a project, you must add the dependency to the Pom. xml file. The next time you run Maven, it will obtain the dependency from the ibiblio repository and add the dependency to the Project Build path. The biggest headache in Maven is that Sun's JAR file cannot be obtained from the maven repository. This problem is caused by Sun's license restrictions in its code. There are two solutions to this problem: one is to download the code and install it in your local repository, and the other is to make an external declaration, and point this declaration to the location of the dependency in the file system. To change the path of the maven repository, simply edit the settings. xml file under the conf folder in the installation directory.
Using dependencies in Maven is simple. Let's see how to add a dependency to the above Pom. xml file.
<Dependency>
<Groupid> quartz </groupid>
<Artifactid> quartz </artifactid>
<Version> 1.5.1 </version>
<Scope> compile </scope>
</Dependency>
Note the use of the scope parameter, which tells the stage at which Maven dependencies are required. When JUnit is used, we set the scope parameter value to test to tell Maven that this dependency is only required in the test phase, not the resources required during runtime. The following describes the scope parameter values:
-Compile: default value. Indicates the resources required for all tasks.
-Test: resources required to run all test cases
-Runtime: indicates the resource required for running.
-Provided: resources required for some JDK or Application Server classpath
8
Manual Installation
Jar
Package to local repository
How to deal with the troublesome sun jar packages and those jar packages that need but cannot be found in the remote repository? We must use Maven to manually install these jar packages to a local repository. For example, we will install the jar package of the Java activation framework. First, we must download the jar package from the sun site, and then import it to the local repository using Maven. You can also install the missing jar package to ibiblio according to the instructions in the maven upload Resource Guide.
MVN install: Install-file-dfile = Activation. Jar
-Dgroupid = javax. Activation-dartifactid = Activation
-Dversion = 1.0-dpackaging = jar
9
, Configure the Repository
It is inconvenient for every developer of the project to configure the repository in the conf directory. Therefore, Maven can view multiple repositories at the same time and configure them all in the Pom. xml file. Let's take a look at an example that shows how to use multiple repositories for applications. In the following excerpt from the Pom. xml file, we set two repositories for Maven to find dependencies. Ibiblio has always been the default repository, And we have added planet mirror as the backup repository. We can also use the local web server used by the team as the second repository.
<Repositories>
<Repository>
<ID> ibiblio </ID>
<Name> ibiblio </Name>
<URL> http://www.ibiblio.org/maven/ </URL>
</Repository>
<Repository>
<ID> planetmirror </ID>
<Name> planet mirror </Name>
<URL> http://public.planetmirror.com/pub/maven/ </URL>
</Repository>
</Repositories>
10
, Use
Pom. xml
Parent file to build multiple projects
A common practice for software companies is to build multiple projects into major products. Maintaining dependency links and building the entire product at one time can be a challenge, but using Maven makes it easy. If you create a pom. xml parent file pointing to another sub-module, MAVEN will process the entire Build Process for you. It analyzes the Pom. xml files of each sub-module and builds projects based on the dependency sequence of these sub-modules. If each project explicitly specifies their dependencies, the placement order of sub-modules in the parent file will not be affected. However, for other developers, it is best to ensure that the sub-modules are placed in the same order as the parent file of POM. XML and the order in which you expect the sub-projects to be built. The following is an example.
The main Pom. xml file is as follows:
<Project>
<Modelversion> 4.0.0 </modelversion>
<Groupid> com. oreilly </groupid>
<Version> 1.0-Snapshot </version>
<Artifactid> my-app </artifactid>
<Packaging> pom </packaging>
<Modules>
<Module> common </module>
<Module> utilities </module>
<Module> application </module>
<Module> webapplication </module>
</Modules>
</Project>
We need to ensure that the webapplication sub-module contains all three jar packages, so we need to declare these jar packages as dependencies. In this example, the utilities project depends on the common project, so you must add a dependency on the common project in the utilities project. The same applies to application submodules, because they depend on common and utilities projects, and utilities also relies on common. If there are 60 sub-modules in this example and they all depend on each other, it will make it difficult for new developers to figure out which projects depend on other projects, so this is exactly the requirement to ensure pom. the reason why the project placement sequence in the XML parent file needs to be cleared.
The following are the dependencies of the utility module:
<Dependencies>
<Dependency>
<Groupid> com. oreilly </groupid>
<Artifactid> common </artifactid>
<Version> 1.0-Snapshot </version>
</Dependency>
</Dependencies>
Now, we only need to add an element to the Pom. xml file of each sub-module to indicate that they are part of a logical build:
<Parent>
<Groupid> com. oreilly </groupid>
<Artifactid> my-app </artifactid>
<Version> 1.0-Snapshot </version>
</Parent>
In the same directory where the Pom. xml parent file is located, there are project directories: Common, utilities, application, and webapplication. When we run the MVN package command in this directory, these projects are built in the order of dependency.
11
, Plug-ins and reports
Maven2.0 has a large number of plug-ins available for use. Unfortunately, due to Maven rewriting, The maven1.0 plug-in cannot be used in 2.0. Despite this, there are still some plug-ins available for maven2.0. The following example of plug-in configuration in the Pom. xml file is obtained directly from the maven2.0 website. This plug-in is used to configure compilation options.
<Plugins>
<Plugin>
<Groupid> org. Apache. Maven. plugins </groupid>
<Artifactid> Maven-compiler-plugin </artifactid>
<Configuration>
<Sources> 1.5 </source>
<Target> 1.5 </Target>
</Configuration>
</Plugin>
</Plugins>
The Maven report plug-in can be used to generate different reports. These reports are generated when you use the MVN site command to generate a project site. The following example shows how to use the <reporting> element to configure one of these plug-ins.
<Reporting>
<Plugins>
<Plugin>
<Groupid> org. Apache. Maven. plugins </groupid>
<Artifactid> Maven-project-Info-Reports-plugin </artifactid>
</Plugin>
</Plugins>
</Reporting>
12
, Summary
Maven2.0 has many practical features and has excellent tasks. The most commendable part of Maven is the use of standard directory structure and deployment. This allows developers to adapt to different projects, and do not need to learn anything new about the structure, nor to master special commands to build the structure. Maven can be implemented through a pure script. In terms of documentation, because the project site build tool is used, you can view the current status of all development after the project build is complete.