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's central servers are all placed abroad. Sometimes we can't access the external network. There are also his mirror warehouses in China, so that we can access them better.

apache-maven-3.3.9\conf\settings.xml This file is configured with mirror information, you can configure the path of the domestic mirror file, so that you can access faster and better.

<mirror>

      <id>mirrorId</id>

      <mirrorOf>repositoryId</mirrorOf>

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

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

    </mirror>

 

The above can configure the mirror warehouse

 

  • Change the location of the repository:

The resources downloaded by maven from the remote central repository are placed in the directory C:\Users\Administrator\.m2\repository by default. For security, generally do not choose to put them in the C drive. You can modify the directory of the local warehouse and specify it to be safer. In addition, the setting.xml file also needs to be saved in another copy, so that the configuration file does not need to be re-modified when the version is updated.

 

6. maven plugin installation and create maven project

If the computer is installed with eclipse4.0 or myeclipse, there is no need to install the Maven plug-in.

Check if the plugin is installed in your eclipse:

eclipse > > preferences to see if there is a Maven option.

Eclipse runs on jre by default, and the maven plugin requires jdk support.

step:

  1. Download the maven plugin, unzip it and put it in the dropins of eclipse or update it online (http://m2eclipse.sonatype.org/sites/m2e , m2e-extras is an extension)
  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=326941971&siteId=291194637