Maven is enough to read this one

Maven

1. Maven skill tree

Maven knowledge structure

2. Understand project management tools

2.1 The reason for the birth of project management tools

  • jar package is hard to find
  • jar package dependency problem
  • jar package version conflict
  • jar is inconvenient to manage
  • Various project structures
  • There are various ways to control the life cycle of a project

2.2 What are the project management tools

  • Ant
  • Maven
  • Gradle

3. What is Maven

Official website portal

Official Operation Manual

3.1 The formation of Maven

  • Maven was originally created as a tool to simplify Jakarta Turbinethe build of a web project developed with the framework .过程
  • When it comes to project construction, Apache Antthe build script tool was popular in the past; the popularity of Ant represented that it was very useful at the time, but it had two fatal shortcomings:
    • There are several projects, each with their own Ant build file, all slightly different
    • Unable to manage dependencies
  • In order to solve the above requirements, Maven was born like this:
    • Building on the capabilities of Ant, build a tool that can be used to build and manage any Java-based project, providing:
      • Standard Method (Object Model POM)
      • Clear definition (structure) of project composition
      • A simple way to publish project information (deployment)
      • A way to share JARs (Central Repository) between multiple projects

3.2 Apache AntComparisonApache Maven Project

Apache Ant: Software Build Tool

Apache Maven Project: Software Project Management and Understanding Tool

  • Use Project Object Model to manage software projects
  • More implicit rules built in to make building files easier
  • Built-in dependency management and Repository for dependency management and unified storage
  • Built-in software build lifecycle

3.3 Maven goals

The main goal of Maven is to allow developers to understand any Java-based project in the shortest possible time. To achieve this goal, Maven addresses several areas of concern:

  • Simplify the build process
  • Provides a unified build system
  • Provide high-quality project information
  • Encourage better development practices

3.4 Summary

  • Maven is a management tool for managing Java projects, mainly managing construction, packaging, and deployment (full life cycle management)
  • Maven manages software projects through the object model-Project Object Model (pom)
  • Maven's project structure has some explicit and implicit rules

4 Maven download, installation and configuration

4.1 Download

Official website download page portal

4.2 Installation

1. Unzip the tar.gz folder directly to the directory you specify (where you want to put it, don't name it in Chinese)

2. Configure environment variables: MAVEN_HOME,PATH

MAVEN_HOME:maven存放路径

PATH:%MAVEN_HOME%/bin

3. Open the command window to test whether the configuration is successful:mvn -v

Apache Maven 3.8.3 (ff8e977a158738155dc465c6a97ffaf31982d739)
Maven home: /Users/nan/DevelopmentEnvironment/apache-maven-3.8.3
Java version: 1.8.0_191, vendor: Oracle Corporation,
runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "mac os x", version: "10.16", arch: "x86_64", family: "mac"

4. mavenCreate a new folder in the installation path repositoryand modify ./apache-maven-3.8.3/conf/setting.xmlthe default warehouse location

Used to replace the default .m2folder to avoid occupying system disk resources

<localRepository>repository的路径</localRepository>

5. Configuration setting.xml, replace the official maven image of the external network with the domestic Alibaba Cloud image

<mirror>
  <id>alimaven</id>
  <name>aliyun maven</name>
  <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
  <mirrorOf>central</mirrorOf>        
</mirror>

6. Configure global & custom IDEA development tools

Do not enter the project after opening IDEA, then open preferences->Build->Build Tools->Mavenimage-20211026180910423

5 Build a maven project

insert image description here
insert image description here
insert image description here
insert image description here
insert image description here

6 Four Features of Maven

6.1 Dependency management system

Dependency: Your project introduces those jar packages, whether open source or not, these jars are the dependencies of your project, and your project depends on them to be used, such as jdbc, fastjson, jetty, slf4j, etc.

The original dependency was introduced: Initially, we need to find these things on the Internet, then download them, copy them into the project, and then build them into the project.

Now how does Maven introduce dependencies: Maven's dependency management is equivalent to making a public platform. The jar package provider hosts the download address on this platform, and users directly download all kinds of dependencies in a unified place to find dependencies. , manage dependencies and other tedious steps maven has done for you, you just need to determine which thing and version you want to use, and hit it directly through the coordinate address provided by maven. The coordinate format is as follows:

<!-- <dependencies>声明:是叫引入的依赖的,都得搁这个标签里面 -->
<dependencies>
    <!-- <dependency>声明引入A依赖 -->
	<dependency>
    <!-- <groupId>
		官方解释:公司或者组织的唯一标志
		人话:就是咱们建项目时的包名,人家叫com.talent.core你叫com.balala.666;com.balala.666其实也是你存储的文件夹的层		 级,这个要是写的不对直接就找不着这个依赖了
		-->
		<groupId>com.talent.core</groupId>
    <!-- <artifactId>
		官方解释:项目的唯一ID,一个groupId下面可能多个项目,就是靠artifactId来区分的
		人话:你就理解成你上人家那个包路径下下载jar包去,但人家的项目是个多模块的,你得确定子项目名,进去之后才能看见你要的jar
		-->
		<artifactId>Anyonelse-core</artifactId>
        <!-- <version>
				见名知意,这个就是你要下这个jar的那个版本的,有时候新的版本可能去掉了一些原有的功能,你上来就找18的小姑娘有可能受不了 
			  -->
        <version>${anyonelse_version}</version>
    
    <!-- <dependency>声明引入B依赖 -->
    <dependency>
		<groupId>com.talent.core</groupId>
		<artifactId>Anyonelse-core</artifactId>
		<version>${anyonelse_version}</version>
	</dependency>
</dependencies>

Summarize

  • It turned out that it took you 10 years to run a procedure to go to 1W places. Now an APP on a mobile phone can do everything in 1 hour.
  • It turned out that I had to build one in the project libraries, then paste the jar I bought from everywhere, and then right-click to import it.
  • Ever since Maven! Mom no longer has to worry that I can't find the jar package and the project has a bunch of jars and the project is too large

image-20211026184252703

img

6.2 Multi-module build

The business of commercial projects is often complex and complex. At this time, we may perform module-level stratification at the architectural level according to controller, model, services, impl, mapper; , a module that sleeps, a module that shits

Maven also supports this multi-module structure, just define a set of aggregated POM (object model), and declare the relationship between father and son in the configuration. At this time, the son will automatically inherit all the configuration items of his father; The configuration of the son relationship is written as follows:

<!-- <modules>声明被这个pom托管的项目是爹,并且我在下面写上了我每个儿子的名字 -->
<modules>
    <!-- <module>声明儿子都有谁 -->
	  <module>PRODUCT-LES</module>
    <module>PRODUCT-LES1</module>
    <module>PRODUCT-LES2</module>
    <module>...</module>
</modules>

Summarize

<modules>The label clearly declares the father-son relationship, and the son no longer has to worry about not knowing where to ask for money.

6.3 Unified project structure

There are many kinds of development tools, such as eclipse, MyEclipse, spring, idea...; different development tools have their own directory structure and some files, configurations, etc., so when maven was not used before, when a project was developed with eclipse When you want to run it with other tools, there will be all kinds of strange problems and uncomfortable batches, so maven restricts the structure of the project managed by him; the structure of the Maven project is as follows:

image-20211017175309517

my-app            -- 项目名
|-- src           -- 资源总目录
|    |-- main     -- 和src基本作用差不多
|    |   `-- java
|    |       `-- com              -- 包路径 节点1
|    |           `-- MyCompany    -- 包路径 节点2
|    |               `-- app      -- 包路径 节点3
|    |                  `-- controller -- 接口
|    |                  `-- pojo      -- 实体类
|    |                  `-- services  -- 接口类
|    |                      `-- impl  -- 实现类
|    |                  `-- mapper    -- 持久层
|    |                      `-- App.java        -- 启动类
|    |   `-- resource       -- 放配置文件的
|    |   `-- mapper         -- 这是前端部分
|    |     `-- beanMapper.xml         -- 这是前端部分
|    |   `-- webapp         -- 这是前端部分
|    |       `-- WEB-INF    -- WEB-INF是Java的WEB应用的安全目录
|    `-- test     -- 测试用例,干掉
|        `-- java
|            `-- com
|                `-- mycompany
|                    `-- app
|                        `-- AppTest.java
`-- pom.xml       -- 对象模型配置文件

6.4 Consistent build model and plugin mechanism

When writing a JavaWeb project, a server like tomcat needs to be used, and we can introduce the server in the form of a plug-in. For example, if you need to use the jetty server, you can import the jetty server through the following configuration.

<plugin>
	<groupId>org.mortbay.jetty</groupId>
	<artifactId>maven-jetty-plugin</artifactId>
	<version>6.1.25</version>
	<configuration>
		<scanIntervalSeconds>10</scanIntervalSeconds>
		<contextPath>/test</contextPath>
	</configuration>
</plugin>

7. POM file analysis

8. Build a private serverNexus

8.1 nexus introduction

Nexus is just a kind of private server. Private server is a remote warehouse that is the same as the central warehouse, but it is a remote warehouse built by itself.

  • Used to store some components that cannot be downloaded from external repositories. For example, internal projects can also be deployed to private servers for use by other dependent projects
  • In order to save bandwidth and time, set up a private warehouse server in the local area network and use it to proxy all external remote warehouses

img

8.2 Why use

  • Save extra network bandwidth
  • Create a local internal public warehouse

8.3 Install & Uninstall

Version/charged/free: Nexus Repository Manager warehouse management has 2 versions, professional version and oss ​​version, oss version is free, professional version is charged, we use oss version.

  • Nexus relies on Java environment

Official website portal

Official download portal

Docker Hub

Official installation documentation

Install

  • tar.gzUnzip the downloaded package to the specified directory
  • Windows systems need to configure environment variables
    • Path:nexus/bin
  • ./nexus startstart up
    • start
    • stop
    • restart
    • force-reload
  • Access to http://localhost:8081verify whether the startup is successful
    • The address where the default username and password are stored: /Users/nan/Downloads/latest-unix/sonatype-work/nexus3/admin.password
    • root:admin
    • pwd: in the admin.password file
  • Change the default password as prompted

image-20211101180141504

8.4 Warehouse classification & usage details

hosted (host warehouse)
  • Save resources (jars) that are not in the central repository
    • self-developed package
    • Third-party but not open source packages
proxy (proxy warehouse)
  • Proxy remote warehouse, access other public warehouses through nuxus, such as maven official, jboos...
warehouse group
  • Group multiple repositories into a group to simplify configuration
  • The warehouse group cannot save resources and belongs to the design warehouse

Add host repository

The warehouse is divided into release version and snapshot version

image-20211101181551211

image-20211101181629389

Add your own repository to the repository group

[External link image transfer failed, the origin site may have anti-leech mechanism, it is recommended to save the image and upload it directly (img-tzz995qh-1644558461948) (/Users/nan/Library/Application Support/typora-user-images/image-20211101181804313 .png)]

Manually upload the package

image-20211101182030350

image-20211101182244150

For link private server download

  • Configure the local Maven configuration file
    • add server
    • add mirror
    • The specific configuration is as follows
<!-- 配置访问服务器的权限,用户名和密码 -->
<servers>
 <!-- 其中一个需要权限访问的私服的名称、用户名、密码-->
<server>
	<id>nexus-zhgc</id>
	<username>aaa</username>
	<password>bbb</password>
</server>
</servers>
<mirrors>
    <mirror>
        <id>nexus-zhgc</id>
        <mirrorOf>central</mirrorOf>
        <url>http://localhost:8081/repository/maven-public/</url>
    </mirror>
</mirrors>
<profile>
 <id>default_profile</id>
 <repositories>
   <!--包含需要连接到远程仓库的信息 -->
   <repository>
     <!--远程仓库唯一标识 -->
     <id>zhanglonghao_repo</id>
     <!--远程仓库名称 -->
     <name>zhanglonghao_repo</name>
     <!--如何处理远程仓库里发布版本的下载 -->
     <releases>
       <!--true或者false表示该仓库是否为下载某种类型构件(发布版,快照版)开启。 -->
       <enabled>true</enabled>
       <!--该元素指定更新发生的频率。Maven会比较本地POM和远程POM的时间戳。这里的选项是:always(一直),daily(默认,每日),interval:X(这里X是以分钟为单位的时间间隔),或者never(从不)。 -->
            <updatePolicy>never</updatePolicy>
            <!--当Maven验证构件校验文件失败时该怎么做-ignore(忽略),fail(失败),或者warn(警告)。 -->
            <checksumPolicy>warn</checksumPolicy>
          </releases>
        <!--如何处理远程仓库里快照版本的下载。有了releases和snapshots这两组配置,POM就可以在每个单独的仓库中,为每种类型的构件采取不同的策略。例如,可能有人会决定只为开发目的开启对快照版本下载的支持。参见repositories/repository/releases元素 -->
        <snapshots>
          <!--true或者false表示该仓库是否为下载某种类型构件(发布版,快照版)开启。 -->
          <enabled>true</enabled>
          <!--该元素指定更新发生的频率。Maven会比较本地POM和远程POM的时间戳。这里的选项是:always(一直),daily(默认,每日),interval:X(这里X是以分钟为单位的时间间隔),或者never(从不)。 -->
          <updatePolicy>always</updatePolicy>
          <!--当Maven验证构件校验文件失败时该怎么做-ignore(忽略),fail(失败),或者warn(警告)。 -->
          <checksumPolicy>warn</checksumPolicy>
        </snapshots>
        <!--远程仓库URL,按protocol://hostname/path形式 -->
        <url>http://maven.zhanglonghao.work:8081/nexus/content/groups/public</url>
        <!--用于定位和排序构件的仓库布局类型-可以是default(默认)或者legacy(遗留)。Maven 2为其仓库提供了一个默认的布局;然而,Maven 1.x有一种不同的布局。我们可以使用该元素指定布局是default(默认)还是legacy(遗留)。 -->
        <layout>default</layout>
      </repository>
    </repositories>
    
    <pluginRepositories>  
      <pluginRepository>  
        <id>maven-net-cn</id>  
        <name>Maven China Mirror</name>  
        <url>http://maven.zhanglonghao.work:8081/nexus/content/groups/public</url>  
        <releases>  
          <enabled>true</enabled>  
        </releases>  
        <snapshots>  
          <enabled>true</enabled>  
        </snapshots>      
      </pluginRepository>  
    </pluginRepositories> 

  </profile>
</profiles>



<!-- activeProfiles
 | List of profiles that are active for all builds.
 -->
<activeProfiles>
  <activeProfile>default_profile</activeProfile>
</activeProfiles>

8.5 Automatically upload resources to private server

Required when uploading resources

  • The package prepared by your own project should be transferred to the host warehouse in the private server
  • Add the following configuration to the corresponding pom
<!-- 说明将通过本pom管理的项目打好的包上传到私服中那个宿主仓库-->	
<distributionManagement>
  	<!-- 稳定版本用这个-->	
		<repository>
      <!-- 要和私服中宿主仓库名、setting中配置的名称一致-->	
			<id>zhgc-release</id>
      <!-- 仓库地址-->	
			<url>http://10.10.1.104:8931/repository/zhgc-release/</url>
		</repository>
  	<!-- 快照版本用这个-->	
		<snapshotRepository>
			<id>zhgc-snapshots</id>
			<url>http://10.10.1.104:8931/repository/zhgc-snapshots/</url>
		</snapshotRepository>
	</distributionManagement>

image-20211101184842779

9 Maven has used common errors & solutions

Guess you like

Origin blog.csdn.net/qq_37768368/article/details/122878924