Home > Java > javaTutorial > body text

maven overview and usage

零下一度
Release: 2017-06-25 10:53:08
Original
1699 people have browsed it

01. Maven Summary

First, let me quickly sort out the concept of maven, so that we can quickly establish a more accurate maven application scenario.

Maven is not ant, nor is it make. For the build tools that I have been exposed to before, I need to write some detailed steps, such as: compile project1/src/*.java and other similar statements.

Maven adopts the method of "convention over configuration". Some common development operations and steps have been solidified in Maven, so users no longer need to write those statements.

Maven has built-in support for the development process. It can not only compile, but also package and publish, and can also complete all these steps in one go.

Maven is not Ivy, Dependency management is one of the functions of Maven. Maven adds the concept of scope to the dependency relationship, further refining the division of dependency relationships. .

maven positions itself as a project management tool. It is responsible for managing almost everything in the project development process.

a. Version: Maven has its own version definition and rules;

b. Build: Maven supports many types of applications, and a set of build rules and toolsets are defined for each supported application type;

c. Output management :Maven can manage the products built by the project and add them to the user library. This function can be used for delivery behavior between the project team and other departments;

d. Dependencies: Maven conducts detailed analysis and analysis of the characteristics of dependencies Divide to avoid dependency confusion and mutual contamination during the development process;

e. Documentation and build results:Maven’s site command supports the release of various document information , including various outputs of the build process, javadoc, product documentation, etc.;

f. Project relationship:A large project usually has several small projects or modules Composition, it can be easily managed with maven;

h. Portability management: Maven can output different types of output results for different development scenarios;

02. Maven life cycle

Maven divides the construction of the project into Different life cycles, include: verification, compilation, testing, packaging, and deployment.

All execution actions (goals) in maven need to indicate their execution position in the process. Then when maven executes, these goals will be called in sequence according to the development of the process. kind of processing.

This is also a basic scheduling mechanism of maven. Generally speaking, subsequent processes will depend on previous processes.

Of course, maven also provides configuration files, which can skip certain stages according to user requirements.

03. maven "Convention is better than configuration"

maven in "Convention over configuration" is not completely unmodifiable, they are just default values ​​for some configurations.

But users do not need to modify those agreed contents unless necessary.

The default file storage structure of maven is as follows:

/Project directorypom.xml Configuration file for maven

/src source code directory

/src/main Project source code directory

/src/main/java Project java source code directory

/ src/main/resource Project resource directory

/src/test Unit test directory

/src/test/java

/target Output directory, all outputs are stored in this directory

/target/classes Compiled class file

Each stage of the task knows how to complete its work correctly.

For example, the compile task knows to compile all java files from src/main/java and store its output class files in target/classes.

Adopting the strategy of "convention over configuration" can reduce the workload of modifying configurations and reduce learning costs. More importantly, it introduces unified specifications to the project.

04. maven version specification

              <groupId>org.springframework</groupId>  <artifactId>spring-webmvc</artifactId>  <version>4.3.7.RELEASE</version>
Copy after login

Maven uses 4 elements to uniquely locate a certain output: groupId:artifactId:packaging:version. Such as org.springframework:spring:2.5.

groupId Group, company, group, organization, project, or other group. The convention for a group ID is that it starts with the reverse domain name of the organization that created the project.

artifactId The unique identifier under groupId represents a single project. For example, our tomcat, commons, etc. Do not include a period (.) in artifactId.

version A specific version of a project. Release projects have a fixed version identifier that points to a specific version of the project.

Although the project's packaging format is also an important part of Maven coordinates, it is not part of the project's unique identifier.

The groupId:artifactId:version of a project makes it a unique project; you cannot have a project with the same groupId, artifactId, version identification at the same time.

packaging The type of the project, the default is jar, which describes the output of the project after packaging.

Maven can use several special strings SNAPSHOT, LATEST, RELEASE during version management.

For example, "1.0-SNAPSHOT". The meaning and processing logic of each part are as follows:

SNAPSHOT: is used in the development process, Maven will install or publish this component When expanding the symbol into a date and time value, convert to UTC time.

For example, "1.0-SNAPSHOT" will become 1.0-20100505-141000-1 when it is released at 2:10 pm on May 5, 2010.

LATEST: refers to the latest release of a specific component. This release may be a release version or a snapshot version, depending on which time is the last .

RELEASE: refers to the last release version.

05. Maven dependency management

## Personally, I think dependency management is maven The most attractive feature is that it allows developers to only focus on the direct dependencies of the code.

For example, if we use spring, just add the spring dependency description. As for what external things spring itself depends on, maven can help us.

Any external dependency description contains the following elements: groupId, artifactId, version, scope, type, optional. The first three are required, and their respective meanings are as follows:

The version here can be expressed by interval expressions, such as (2.0,) means >2.0, [2.0,3.0) Indicates 2.0<=ver<3.0; multiple conditions are separated by commas, such as [1,3),[5,7].

Maven believes that the program's external dependencies will change with the stage and application scenario of the program, so the dependencies in Maven are limited by scope.

scope includes the following values:

compile (compile scope) ---> compile is the default scope, compile-scope dependencies are available on all classpaths, and they are also packaged.

provided (provided scope) ---> provided dependency is only used when the JDK or a container has provided the dependency.

For example, if you develop a web application, you need the Servlet API available in the compilation classpath to compile the servlet, but you do not want to include this Servlet API in the packaged WAR.

   runtime(运行时范围 --> runtime 依赖在运行和测试系统的时候需要,但在编译的时候不需要。

   比如你可能在编译的时候只需要 JDBC API JAR,而只有在运行的时候才需要 JDBC 驱动实现。

   test(测试范围)--> test 范围依赖 在一般的 编译和运行时都不需要,它们只有在测试编译和测试运行阶段可用。

   system(系统范围) --> system 范围依赖与 provided 类似,但是你必须显式的提供一个对于本地系统中 JAR 文件的路径。

    <dependency> <groupId>org.wltea</groupId> <artifactId>analyzer</artifactId> <version>2012_u6</version> <scope>system</scope> <systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/analyzer-2012_u6.jar</systemPath> </dependency>
Copy after login

   注意该范围是不推荐使用的(你应该一直尽量去从公共或定制的Maven仓库中引用依赖)。

   type 一般在pom引用依赖时候出现,其他时候不用, optional 是否可选依赖。

   依赖也可以是可选的,比如我们代码中没有任何cache依赖,但是hibernate可能要配置cache,所以该cache的依赖就是可选的。

06. maven 多项目管理

   maven的多项目管理也是非常强大的。一般来说,maven要求同一个工程的所有子项目都放置到同一个目录下,每一个子目录代表一个项目,比如

   总项目/ pom.xml 总项目的 pom 配置文件

   子项目1/pom.xml 子项目1的 pom 文件

   子项目2/ pom.xml 子项目2的 pom 文件

   按照这种格式存放,就是继承方式,所有具体子项目的pom.xml都会继承总项目pom的内容,取值为子项目pom内容优先。

   要设置继承方式,首先要在总项目的pom中加入如下配置

<modules>   <module>simple-weather</module>   <module>simple-webapp</module> </modules>
Copy after login

   其次在每个子项目中加入

<parent>   <groupId>org.sonatype.mavenbook.ch06</groupId>   <artifactId>simple-parent</artifactId>   <version>1.0</version> </parent>
Copy after login

   当然,继承不是唯一的配置文件共用方式,maven还支持引用方式。引用pom的方式更简单,在依赖中加入一个type为pom的依赖即可。

  <dependency>      <groupId>org.sonatype.mavenbook</groupId>      <artifactId>persistence-deps</artifactId>      <version>1.0</version>      <type>pom</type>   </dependency>
Copy after login

07. maven 内置属性

  用户可以在maven中定义一些属性,然后在其他地方用${xxx}进行引用。比如:

 value1
Copy after login

   maven 提供了三个隐式的变量,用来访问系统环境变量、POM信息和 maven 的 settings:

   env 暴露操作系统的环境变量,比如 env.PATH

   project 暴露 POM 中的内容,用点号(.)的路径来引用POM元素的值,比如 ${project.artifactId}。另外,java的系统属性比如user.dir等,也暴露在这里。 

   settings 暴露 maven 的 settings 的信息,也可以用点号(.)来引用。

08. maven profile

   profile 是 maven 的一个重要特性,它可以让 maven 能够自动适应外部的环境变化。

   比如同一个项目,在linux下编译linux的版本,在win下编译win的版本等。

   一个项目可以设置多个 profile,也可以在同一时间设置多个 profile 被激活(active)的。

   自动激活的 profile 的条件可以是各种各样的设定条件,组合放置在 activation 节点中,也可以通过命令行直接指定。

   profile 包含的其他配置内容可以覆盖掉 pom 定义的相应值。

   如果认为 profile 设置比较复杂,可以将所有的 profiles 内容移动到专门的 profiles.xml 文件中,不过记得和 pom.xml 放在一起。

09. maven 配置文件

   maven的主执行程序为 mvn.bat,linux下为mvn.sh,这两个程序都很简单,它们的共同用途就是收集一些参数,然后用 java.exe来运行maven的Main函数。

   maven 同样需要有配置文件,名字叫做 settings.xml,它放在两个地方,一个是 maven 安装目录的conf目录下,对所有使用该 maven 的用户都起作用。

   我们称为主配置文件,另外一个放在 %USERPROFILE%/.m2/settings.xml 下,我们成为用户配置文件,只对当前用户有效,且可以覆盖主配置文件的参数内容。

   还有就是项目级别的配置信息了,它存放在每一个 maven 管理的项目目录下,叫 pom.xml,主要用于配置项目相关的一些内容。

   当然,如果有必要用户也可以在 pom 中写一些配置,覆盖住配置文件和用户配置文件的设置参数内容。

   一般来说,settings文件配置的是比如repository库路径之类的全局信息,具体可以参考官方网站的文章。

10. maven 本地安装和中央库发布

   在maven中一般都会用到安装库文件的功能,一则是我们常用的hibernate要使用jmx库,但是因为sun的license限制,所以无法将其直接包含在repository中。

   所以我们使用mvn命令把jar安装到我们本地的repository中

 mvn install:install-file -DgroupId=com.sun.jdmk -DartifactId=jmxtools -Dversion=1.2.1 -Dpackaging=jar -Dfile=/path/to/file
Copy after login

   如果我们想把它安装到公司的repository中,需要使用命令

mvn deploy:deploy-file -DgroupId=com.sun.jdmk -DartifactId=jmxtools -Dversion=1.2.1 -Dpackaging=jar -Dfile=/path/to/file -Durl= -DrepositoryId=release-repo
Copy after login

   对于我们的工程输出,如果需要放置到公司的repository中的话,可以通过配置 pom 来实现

<distributionManagement> 
   <repository> 
      <id>mycompany-repository</id> 
      <name>MyCompany Repository</name> 
      <url>scp://repository.mycompany.com/repository/maven2</url> 
   </repository> </distributionManagement>
Copy after login

11. maven 常用命令

1. 创建Maven的普通java项目: 
   mvn archetype:create -DgroupId=packageName -DartifactId=projectName 
2. 创建Maven的Web项目: 
   mvn archetype:create -DgroupId=packageName -DartifactId=webappName -DarchetypeArtifactId=maven-archetype-webapp 
3. 编译源代码: mvn compile 
4. 编译测试代码:mvn test-compile 
5. 运行测试:mvn test 
6. 产生site:mvn site 
7. 打包:mvn package 8. 在本地Repository中安装jar:mvn install 
9. 清除产生的项目:mvn clean 
10. 生成eclipse项目:mvn eclipse:eclipse 
11. 生成idea项目:mvn idea:idea 
12. 组合使用goal命令,如只打包不测试:mvn -Dtest package 13. 编译测试的内容:mvn test-compile 
14. 只打jar包: mvn jar:jar 
15. 只测试而不编译,也不测试编译:mvn test -skipping compile -skipping test-compile 
( -skipping 的灵活运用,当然也可以用于其他组合命令) 
16. 清除eclipse的一些系统设置:mvn eclipse:clean
Copy after login

 

The above is the detailed content of maven overview and usage. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!