maven enterprise multi-module project

refer to:

http://www.cnblogs.com/quanyongan/category/471332.html  (maven essay series)

http://m.blog.csdn.net/blog/index?username=u012562943&categoryid=6037062  (maven learning summary)

http://canann.iteye.com/blog/2095951  (Maven Best Practices: Dividing Modules)

 

1. The concept of Maven

Maven is a software project management tool based on the Project Object Model (POM), which can manage the construction, reporting and documentation of a project through a small piece of descriptive information.

Simply put, Maven can help us manage projects more efficiently.

To download Maven, you need to visit: maven.apache.org

 Maven3.3 version requires the support of JDK1.7 or above.

After downloading, unzip to view the corresponding directory:

 View the bin directory: (bin directory contains the running script of mvn)

 

The bin directory contains the run scripts for mvn. This script is invoked by typing mvn on the windows command line.

m2.conf This is a configuration file.

View the boot directory: (the boot directory contains a framework for a class loader)

 

The boot directory contains a framework for the class loader that Maven uses to load its own class libraries.

View the conf directory: (conf is the configuration file directory)

 

conf is the configuration file directory, such as setting.xml that we will often use in the future

In the lib directory: (including the class libraries that Maven usually uses, in addition to its own, it also includes third-party dependent class libraries)

 

2. maven environment variable configuration

Environment variables are divided into user variables and system variables.

If your computer is used by multiple people, then in order not to affect others, you should set it in user variables. (User variables: variables that belong to each user of the computer.)

If your computer is private, you can set it in system variables. (System variables: variables common to all computer users.)

  • M2_HOME: The variable value is set to the maven installation directory;
  • path: set to %M2_HOME%\bin;

How to verify whether the environment variable is configured successfully?

Open cmd command prompt dos: enter mvn -v

If you can view the relevant version information, it means that the environment variable configuration is successful.

 

 

Note:

The value of groupId is the package name of the project. The
value of artifactId is the module name. It is generally recommended to use the project name
. The value of version is the version name, which is generally snapshot.

A small example of building with maven:
1: Use the maven convention to build the directory structure
2: Build the project according to the convention directory structure
3: Write simple test code
4: Configure the pom configuration file
5: Run the basic maven command to manage the corresponding small example
6: Solve mistakes in building the project according to the command prompts, and understand what each command actually does.

 

3. Maven common build commands

  • mvn -v : View Maven version
  • mvn compile: compile
  • mvn test: test
  • mvn package: package
  • mvn  clean : Clear the target directory generated by maven (the target stores the compiled bytecode files and test reports of the project)
  • mvn  install : install the generated jar into the local repository

The process of maven compiling code:
1. Execute the mvn compile command to compile the java code file
2. If it is found that other package files (jar packages) are referenced in the code
3. First go back to the core configuration file pom.xml file to find the corresponding configuration dependencies
4. According to the configuration dependencies, first go to the local warehouse to find the jar package resources.
5. If it does not exist locally, it will go to the network to find the maven central warehouse.

 

4. maven environment variable configuration

 

There are two ways to create a directory skeleton.

Maven provides us with the archetype plugin for creating Maven-compliant directory skeletons.

According to Maven regulations, to be in:

  • mvn archetype:generate: Prompt to enter version after running the command, groupId, artifactId, version, package
  • mvn archetype:generate -DgroupId:Organization name, reverse company domain name + project name-DartifactId: project name-module name-Dversion:version-Dpackage:package name. One-time creation.

 

5. maven coordinates and repository

  • The concept of coordinates: components

The identifier that uniquely identifies a component in the Maven world generally consists of three parts: groupId/artifactId/version. In addition, the author recommends that the artifactId and the project name should be consistent, which is clearer and easier to find.

  • The concept of warehouse:

A place to put various dependencies. There are two types of repositories: local repositories and remote repositories.

If the local warehouse cannot find the component we need, it will go to the central warehouse to find it, and after finding it, it will be downloaded to the local warehouse

C:\apache-maven-3.3.9\lib\maven-model-builder-3.3.9\org\apache\maven\model\pom-4.0.0.xml - a super pom provided by Maven for all of us The pom will record the global central warehouse recorded in this pom, where almost all the resources of open source projects - jar packages are placed in the central warehouse.

 

  •  Mirror repository:

Maven的中央服务器都是放置在国外的,有时我们不能访问外网,国内也有他的镜像仓库,这样就能更好的访问了。

apache-maven-3.3.9\conf\settings.xml这个文件中就配置着镜像的信息,可以配置国内的镜像文件的路径,这样就能更快更好的访问了。

<mirror>

      <id>mirrorId</id>

      <mirrorOf>repositoryId</mirrorOf>

      <name>Human Readable Name for this Mirror.</name>

      <url>http://my.repository.com/repo/path</url>

    </mirror>

 

以上可以配置镜像仓库

 

  • 更改仓库的位置:

maven从远程中央库下载的资源默认放在了C:\Users\Administrator\.m2\repository这个目录,为了安全性,一般不会选择放在c盘下可以修改本地仓库的目录,指定到更安全的目录下,另外,setting.xml这个文件也需要另存一份,这是为了更新版本的时候不用重新修改这个配置文件。

 

6. maven插件安装及创建maven项目

如果电脑安装的是eclipse4.0或者myeclipse,就不用再安装Maven插件了。

查看自己的eclipse是否安装插件:

eclipse 》》preferences ,查看是否有Maven选项。

eclipse默认运行在jre上,而maven插件需要jdk的支持。

步骤:

  1. 下载maven插件,解压放至eclipse的dropins中或在线更新(http://m2eclipse.sonatype.org/sites/m2e , m2e-extras为扩展 )
  2. 在eclipse.ini中添加jdk
    -vm
    C:\Program Files\Java\jdk1.7.0_75\bin\javaw.exe
  3. 修改eclipse-选项中maven的maven目录和setting位置。
  4. 创建maven项目
  5. run as -->build as -->在goals中complile-->run
  6. 若报-Dmaven.multiModuleProjectDirectory 错误,则在选项-->java-->installed JRES中配置 -Dmaven.multiModuleProjectDirectory=$M2_HOME

 

7. maven生命周期及插件

  • 完整的项目构建过程包括:

    清理、编译、测试、打包、集成测试、验证、部署。

这几个步骤对应的都有插件来完成。

  • Maven生命周期:clean、compile、test、package、install
  • Maven的生命周期分为三个独立的生命周期(每一个生命周期有分为若干个阶段,其中defaul是最核心的一个):
    • clean:清理项目
    • default:构建项目
    • site: 生成项目站点

 

  • clean生命周期包含的阶段:
    pre-clean : 执行清理前的工作
    clean : 清理上一次构建生成的所有文件
    post-clean : 执行清理后的文件
  • default构建项目(最核心)

   default生命周期包含的阶段比较多,比较常用的就是compile、test、package、install

  • site生成项目站点:

    - pre-site work to be done before generating the project site
    - site to generate the project's site documentation
    - post-site work to be done after the project site is generated
    - site-deploy to publish the generated site to the server

Note that the commands corresponding to Maven's life cycle are executed sequentially. If there is no artificial order to execute them one by one, then Maven will automatically run by default.

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326941944&siteId=291194637