IntelliJ Idea14 create a multi-module Maven project, multiple inheritance, hot deployment configuration summary (c)

The role of the pom.xml repositories, pluginRepository of

pom.xmlRole repositories label is: used to configure the remote repository maven project. Examples are as follows:

  <repositories>
        <repository>
            <id>nexus</id>
            <name>Nexus Repository</name>
            <url>远程仓库地址</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>  <!--snapshots默认是关闭的,需要开启  -->
            </snapshots>
        </repository>
    </repositories>

According to the above configuration is achieved maven project configuration of the remote repository, however, such a configuration, if we create a project, you need to configure a lot of trouble. The above can be configured in a remote repository maven of setting.xml inside. This can be achieved only configured once

pom.xmlThe pluginRepositoryrole of the label is: used to configure the remote repository maven plugin. Examples are as follows:

<pluginRepositories>
    <pluginRepository>
        <id>nexus</id>
        <name>Team Nexus Repository</name>
        <url>远程仓库地址</url>
    </pluginRepository>
</pluginRepositories>

Maven and Nexus, snapshot snapshot repository and release issued library

Dependency management, by specifying coordinates in the form Pom jar into the project. That this process, how to go through a process it? Where to find the jar? Where to download the jar into?

This problem will shun down, you know maven and nexus of the relationship.

Where to find the jar? Project used jar and stored? This leads to the concept of a warehouse, maven repository to the unified management by the various components. Maven warehouse is divided into local and remote warehouse warehouse.

When Maven to find members based on the coordinates, it first looks at the local repository, if local repository presence of this component is used directly; if the local repository does not exist for this component, or need to see if a newer component version, Maven will go to a remote warehouse to find , after the discovery of components needed, and then downloaded to the local warehouse use.

Here, and the answer will come out.

First, Nexus is a remote warehouse, according to the section of the introduction, we already know the role of remote warehouse. In the remote repository, the default is the central warehouse, central warehouse is the core of Maven comes with a remote repository. Then use a central warehouse does not have it, why do we want to install Nexus?

From a practical point of view of our development projects:

1. Some can not be downloaded from an external warehouse components, such as internal project also deployed to PW, in order to rely on for other projects.

  1. To save bandwidth and time, set up a private warehouse server in the LAN, with its agents all external remote repository. When the local Maven project members need to download, go to PW request, if PW not, go to a remote warehouse request, warehouse downloaded from remote member, the member cache on private servers. In this way, there is no timely temporary Internet link, due to the large number of components, PW has been cached, the entire project can still be used normally. At the same time, also reduces the load on the central warehouse.
    Nexus is just a PW.

Use distributionManagement in pom.xml project package upload to the nexus PW

1, pom.xml file to add the node distributionManagement

<!-- 使用分发管理将本项目打成jar包,直接上传到指定服务器 -->
  <distributionManagement>
    <!--正式版本-->
    <repository>
        <!-- nexus服务器中用户名:在settings.xml中<server>的id-->
        <id>yang</id>
        <!-- 这个名称自己定义 -->
        <name>Release repository</name>
        <url>http://192.168.1.105:8081/repository/yang/</url>
    </repository>
    <!--快照
    <snapshotRepository>
        <id>nexus-snapshots</id>
        <name>Snapshots repository</name>
        <url>http://192.168.1.105/repository/yang/</url>
    </snapshotRepository>-->
  </distributionManagement>

<Repository> under the node <id> id server corresponding to the file setting.xml

    <!--maven连接nexus需要验证用户名和密码-->
    <server>
      <id>yang</id>
       <username>admin</username>
       <password>admin123</password>
     </server>

Upload the official version, pom.xml file version can not have SNAPSHOT, snapshot version only

  <groupId>com.yang</groupId>
  <artifactId>shade-plugin</artifactId>
  <version>0.0.1</version>
  <!--  <version>0.0.1-SNAPSHOT</version> -->

2, package execution upload
right-click pom.xml file, run As - Maven build ... open box below.
Here Insert Picture Description
Here Insert Picture Description


snapshot snapshot repository and release issued library

In use maven process, we often have a lot of public libraries in an unstable state in the development phase, at any time to modify and publish, probably will be released once a day, the face of bug, or even a day to release N times. We know that, maven dependency management is based on version management, for the artifact released state, if the version number is the same, even if the component of our internal mirror server than the local new, maven will not take the initiative to download. If we are based on the official release version in the development stage for dependency management, you encounter this problem, you need to upgrade the version number of the components can be so obviously does not meet the requirements and the actual situation. However, if it is based on a snapshot version, then the problem is solved self-heating and natural, while maven is ready for us it all.

maven中的仓库分为两种,snapshot快照仓库和release发布仓库。snapshot快照仓库用于保存开发过程中的不稳定版本,release正式仓库则是用来保存稳定的发行版本。定义一个组件/模块为快照版本,只需要在pom文件中在该模块的版本号后加上-SNAPSHOT即可(注意这里必须是大写),如下:

<groupId>cc.mzone</groupId>
<artifactId>m1</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>jar</packaging>

maven会根据模块的版本号(pom文件中的version)中是否带有-SNAPSHOT来判断是快照版本还是正式版本。如果是快照版本,那么在mvn deploy时会自动发布到快照版本库中,而使用快照版本的模块,在不更改版本号的情况下,直接编译打包时,maven会自动从镜像服务器上下载最新的快照版本。如果是正式发布版本,那么在mvn deploy时会自动发布到正式版本库中,而使用正式版本的模块,在不更改版本号的情况下,编译打包时如果本地已经存在该版本的模块则不会主动去镜像服务器上下载。

所以,我们在开发阶段,可以将公用库的版本设置为快照版本,而被依赖组件则引用快照版本进行开发,在公用库的快照版本更新后,我们也不需要修改pom文件提示版本号来下载新的版本,直接mvn执行相关编译、打包命令即可重新下载最新的快照库了,从而也方便了我们进行开发。

接下来要介绍的是如何在项目中应用snapshot和release库,应用snapshot和release库达到不同环境下发布不同的版本的目的,首先看一个pom文件的定义:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>net.aty.mybatis</groupId>
    <artifactId>mybatis-demo</artifactId>
    <packaging>jar</packaging>
    <version>${project.release.version}</version>
    <name>mybatis-demo</name>
    <url>http://maven.apache.org</url>
    
    <properties>
        <project.release.version>0.1-SNAPSHOT</project.release.version>
    </properties>
    
 
    <profiles>
        <profile>
            <id>release</id>
        <properties>
            <project.release.version>0.1</project.release.version>
        </properties>
        </profile>
    </profiles>
    
    
    <!--定义snapshots库和releases库的nexus地址-->
    <distributionManagement>
        <repository>
            <id>nexus-releases</id>
            <url>
                http://172.17.103.59:8081/nexus/content/repositories/releases/
            </url>
        </repository>
        <snapshotRepository>
            <id>nexus-snapshots</id>
            <url>
                http://172.17.103.59:8081/nexus/content/repositories/snapshots/
            </url>
        </snapshotRepository>
    </distributionManagement>
 
</project>

首先我们看到pom文件中version的定义是采用占位符的形式,这样的好处是可以根据不同的profile来替换版本信息,比如maven默认是使用0.1-SNAPSHOT作为该模块的版本。

1、如果在发布时使用mvn deploy -P release 的命令,那么会自动使用0.1作为发布版本,那么根据maven处理snapshot和release的规则,由于版本号后不带-SNAPSHOT故当成是正式发布版本,会被发布到release仓库;

2. If using mvn deploy command is issued, it will use the default version 0.1-SNAPSHOT, this time will be considered maven snapshot versions, will be automatically posted to the snapshot repository.

Configured in distributionManagement segment is the address snapshot snapshot repository and release issued libraries, I have here is the use of nexus as a mirror server. For the main repository id and url configuration, after the configuration is completed can be released by mvn deploy, of course, if your mirror server requires a username and password, you also need to do the following configuration in the maven settings.xml file :

<server>
  <id>nexus-releases</id>
  <username>admin</username>
  <password>admin123</password>
</server>
 
<server>
  <id>nexus-snapshots</id>
  <username>admin</username>
  <password>admin123</password>
</server>

Note that the configuration of the server's id must correspond pom file repository id distributionManagement consistent, maven will find a user name and password to log in and upload documents released under id when dealing released.

Here we have the flexibility to switch by defining profile in the publication snapshot snapshot version and the official version of the release, you can also use the profile being dependent components are defined using the snapshot repository in the development phase, functions official library in the release phase, only You need to override the default attribute values ​​to different properties of the profile.
Here Insert Picture Description
Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/both-eyes/p/10965486.html