Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

Writing in the front

Maven, a tool we’ve all been exposed to before learning about frameworks, feels like it’s positioned just like Git, just convenient for us to develop? Naturally, a lot of Little apes are stuck with Maven in the master stage, using it to build, package, and import JARS. In fact, Maven has many powerful places, such as multi-module development, private server, etc. We also need to master the actual development, often multi-module co-development, especially we mentioned before the Dubbo distributed development, multi-module development is the most important!

Traditional project development

Guide the jar package

  • Since be guide jar package, we are in the local Windows development, and the actual project is put into a Linux server, if we simply put project into jars, depend on the file is also a jar package, but the two systems because the environment is different, direct throw jar package (he won’t be recompiled to run), come out of the results may not be the same

The getByte method of String, for example, yields different results on both systems, and will eventually be biased if it is not recompiled and run

Maven development

The jar package (SpringBoot)

The application code needs to be placed in boot-INF /classes; Then the other jar files that the application depends on need to be placed in the boot-INF /lib directory. When this jar is run as a standardalone program (without being placed in the container), SpringBoot will be generated in the meta-inf/MANIFEST. The MF inside will be Main – Class set to org. Springframework.. The boot loader. JarLauncher, The JarLauncher class creates spring’s own ClassLoader: LaunchedURLClassLoader: LaunchedURLClassLoader: LaunchedURLClassLoader: LaunchedURLClassLoader: LaunchedURLClassLoader: LaunchedURLClassLoader: LaunchedURLClassLoader: LaunchedURLClassLoader: LaunchedURLClassLoader Then invoke the main method inside the class.

  • Reference: Jane Book

War file (Tomcat)

The dependent JAR package is placed in the Web-INF lib directory

Maven concept

POM

Project Object Model

  • Manage each project as an object

Maven role

Project construction and management

Provides a set of methods for automating the construction of projects. It’s versatile, compatible, and cross-platform

  • Including package, compile, test, run a set of operations down, so that you can easily test in the development environment and other functions

A build is process-oriented, that is, the steps that complete the compilation, testing, running, packaging, deployment, and so on of the project code. Maven supports the following builds: 1. Clean up, remove the compiled stuff from the previous project, and prepare the new compiled code. 2. Compile, compile the program source code into execution code, Java-class file

  • In bulk, Maven can compile hundreds of files into classes at the same time.
  • Javac is different. Javac compiles one file at a time.

3. Test. Maven performs test code to verify that your functionality is correct. 4. Report, generate test results file, test passed or not. 5. Pack your project’s class files, configuration files, and other resources into a compressed file.

  • The zip file is the result file of the project. Usually for Java programs, the zip file has a JAR extension.
  • For Web applications, the zip file extension is.war

6. Install the jar, war file generated in 5 into the local repository for other files to use 7. Deploy, install the program can be executed.

The life cycle down here is actually pretty good

Dependency management

  • Handle jar package conflicts

Uniform development structure – Convention over configuration

That is to say, do not specify the code that can be configured, and do not configure the rules that can be agreed in advance. This reduces labor and prevents mistakes.

  • Maven didn’t actually impose constraints on our project’s file structure, but because it did so well, it became a universal structure

coordinates

warehouse

Private server (examples below)

Download order??

First in the local warehouse to find, can not find the private server to find, if the private server does not have, will go to the central warehouse to download the private server and send to the local warehouse

It’s going to go down locally anyway, it’s just a matter of where do you send it to locally?

  • There is a question here, will the private server not go to the central warehouse to the private server? I below configuration ali cloud and private server, it seems that the private server is not directly from ali cloud direct download to the local warehouse, will not be through private server? Hope big guy can correct once!

Figure source m.yisu.com/zixun/30991…

Mirror Warehouse Configuration

mirrorOf

Which warehouse to mirror

Manual maven

Maven project directory structure

Build commands

Maven build commands start with MVN, followed by functional parameters, and you can execute multiple commands at once, separated by Spaces

clean

Delete the compiled file and delete the target file directory

install

Install your current project into a local repository, creating a JAR package for other projects to reference the classes in

package

I will compile and then CLEAN

Dependency management

Depend on the transfer

Dependencies are transitive, including direct and indirect transfer. Direct delivery: Dependencies established by dependency configuration in the current project (A uses B, A and B are direct delivery) Indirect delivery: If A depends on B, and B depends on C, then A and C are indirect delivery

The principle of conflict

Path first

The shallower the layer, the higher the priority

Statement is preferred

At the same level, the first declaration is higher

Different versions of the same resource are configured in the same POM

The rear configuration covers the front

Optional dependence

Eliminate dependence on

(1) Manually exclude dependencies, and 2 can directly include 3 entire dependencies, and then exclude specific dependencies

Maven Helper is a plugin for idea to eliminate dependencies

Open the POM file, and you can choose to switch the viewing mode below

Right-click and select the Exclude that you want to Exclude

(3) Version lock –(to be added to the instance, take Dubbo)

Dubbo’s GitHub document applies this

<properties>
    <spring-boot.version>2.3.0. RELEASE</spring-boot.version>
    <dubbo.version>2.7.8</dubbo.version>
</properties>
    
<dependencyManagement>
    <dependencies>
        <! -- Spring Boot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring-boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <! -- Apache Dubbo -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-bom</artifactId>
            <version>${dubbo.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
      
    </dependencies>
</dependencyManagement>

<dependencies>
    <! -- Dubbo Spring Boot Starter -->
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>2.7.8</version>
    </dependency>    
</dependencies>
Copy the code

dependencyManagement

  • The version of the JAR package is managed through its element, allowing dependencies to be referenced directly in the subproject without listing the version number. Maven moves up the parent-child hierarchy until it finds a project that has elements, and then it uses the version number specified in this dependencyManagement element.

Management universality

  • We don’t need to declare a version for each subproject, just manage it uniformly in the top-level parent class.

scalability

  • If a subproject needs another version number, you only need to declare one version number in dependencies. A subclass uses the version number declared by the subclass and does not inherit from its parent.

Depend on the range

Transitivity (Understood)

Life cycle and plug-ins

Inheritance (SpringBoot is a good example)

role

Inheritance makes it possible to use the same configuration as the parent project in the child project (similar to Java)

Make way

Declare the parent project coordinates and corresponding positions in the child project

<! Define the parent project of the project -->
<parent>
  <groupId>     </groupId>
  <artifactId>    </artifactId>
  <version>    </version>
  <! -- Fill in the POM file of the parent project -->
  <relativePath>Parent project POM file address</relativePath>
</parent>
Copy the code

Such as SpringBoot

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.6</version>
        <relativePath/> <! -- lookup parent from repository -->
</parent>
Copy the code

Define dependency management in the parent project

Manage versions and resolve dependency conflicts in subprojects (version locking mentioned above)

<! Declare dependency management here -->
<dependencyManagement>
  <! -- Specific dependencies -->
  <dependencies>
    <dependency>
      <groupId>     </groupId>
      <artifactId>     </artifactId>
      <version>   </version>
    </dependency>
  </dependencies>
</dependencyManagement>
Copy the code

Use inherited dependencies: Define dependencies in the child project without declaring the dependency version. The version refers to the version of dependencies in the parent project

<dependencies>
  <dependency>
    <groupId> </groupId>
    <artifactId> </artifactId>
  </dependency>
</dependencies>
Copy the code

The aggregation

  • A lot of our actual development is multi-module development, where we separate the business into a single module, for exampleDaoModule,ServiceThe module
    • If the Dao module changes, what about all other modules that depend on the Dao module? Does he know? Is there a way to get them to update at the same time instead of individually? And that’s where our aggregation comes in

Create an empty module

Set the package type to POM

<packaging>pom</packaging>
Copy the code

Aggregation of the module

The effect

Private server warehouse classification

  • Hosted repository hosted: Refers to the internal packages of our company or team, and releases are broken into releases (official releases) and snapshots (beta releases).

Save resources that are not directly available from a central repository, such as our own projects or third-party non-open source projects such as Oracle (note copyright issues).

  • Proxy warehouse proxy: used to proxy the central warehouse, for example, we rely on the package in the local warehouse, we will go to the private server to obtain, if the private server does not have, we will go to the central warehouse to download the package to the private server, and then download to the local warehouse;
  • Warehouse group: to combine multiple warehouses together, we only need to configure this type of warehouse address in the project, and then we can associate several warehouses combined in it.

** Fast access to Nexus private server

Private servers role

Sometimes we are multi-machine development, and we want to reference the module JAR package on the other machine, at this time, the other party simply released to the local repository on the other computer, we still can not access it. This time you need a middleman private server, to store some need to share resources

Nexus Download and Install

  • The first thing we need to do is download the Nexus for our private server
  • After downloading, change the startup port in etc (default 8081)

  • Go to the bin directory
  • vim nexus
  • Enter /run_as_user to find run_as_root and press I to enter the editing mode

I’m going to change this to falseStart the Nexus as user root

  • Press ESC to exit the editing mode and enter :wq to save the configuration and exit
  • Finally./nexus start starts
  • Access the port number you set

The default account is admin

The first login will prompt you to look for a password in a specific location

Just look for it in that directory

You can change the default password later

Access to private server repository (manual upload)- not recommended

  • At the top, you can switch the view. On the left, you can browse the warehouse. On the right, you can add your own warehouse in the management layer

Here we create a host repository

Modify the snapshot version/distribution

Go to Maven-public and include the one we just created

Go to the browse page to find our warehouse and upload manually

Idea Environment Upload

Two places need to be configured

  • One is how does the local repository interact with the private server – corresponding to the default general setting file
  • One is the specific warehouse to which our project will be published to the private server — corresponding to the configuration in the poM file of the current project

Upload jar authentication configuration

Maven authentication is required to upload jar packages. Maven authentication is configured under the Servers TAB in settings. XML or in maven’s own Settings.

The server ID is the same as the repository ID configured under distrubtionManagement in your project, otherwise the audit will fail.

<! -- Configure access to the server -->
    <server>
        <id>nexus-Melo</id>
        <username>admin</username>
        <password>admin</password>
    </server>
    <server>
          <id>Melo-Release</id>
          <username>admin</username>
          <password>admin</password>
    </server>
    <server>
          <id>Melo-Snapshots</id>
          <username>admin</username>
          <password>admin</password>
    </server>
Copy the code

The id here corresponds to the server.id information configured in setting. XML, and the name is optional

<! -- Private server release Management -->
    <distributionManagement>
        <repository>
          	 <! -- id must be the same as the id specified in setting -->
            <id>Melo-Release</id>
             <! Go to nexus website to find the warehouse copy URL -->  
            <url>http://ip: port /repository/ melo-release /</url>
        </repository>
        <snapshotRepository>
            <id>Melo-Snapshots</id>
            <url>http://ip port /repository/ melo-snapshot /</url>
        </snapshotRepository>
    </distributionManagement>
Copy the code

Upload the effect

Idea environment obtained from private server (using Aliyun image + private server)

  • Configure global setting.xml and all our projects will be found in private servers

It is recommended to use the built-in Maven-public repository group and include the repository we created ourselves

Be careful not to include the proxy class repository in our group! In this way, the ali Cloud image we configured will be invalid

Unless you manually change the warehouse address of the proxy, but I don’t think it is recommended. For details, see “Other implementation methods” in the following article.

Configure server- access to the server

Contains two parts of the configuration for pull and upload, both of which require permissions

<! -- Configure access to the server -->
    <! Select id, id, id from mirror, id from mirror, id from mirror, id from mirror
    <server>
        <id>nexus-Melo</id>
        <username>Your private server account</username>
        <password>Your private password</password>
    </server>
<! <distributionManagement> <distributionManagement> <distributionManagement>
    <server>
          <id>Melo-Release</id>
          <username>Your private server account</username>
          <password>Your private password</password>
    </server>
    <server>
          <id>Melo-Snapshots</id>
          <username>Your private server account</username>
          <password>Your private password</password>
    </server>
Copy the code

Configuration mirror

<! -- Ali Cloud Private server, take the central warehouse -->
    <mirror>
      <id>alimaven</id>
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<! -- Access the central warehouse to use Ali Cloud image -->
      <mirrorOf>central</mirrorOf>
    </mirror>
<! -- Maven private server -->
    <mirror>
      <id>nexus-Melo</id>
      <! -- Mirror name -->
      <name>nexus-Melo</name>
      
      <! -- Mirror in addition to the central warehouse warehouse (if there is a local or local priority, not to find private server, private server will first download through Ali cloud to private server, and then send to the local)-->
      <mirrorOf>! central</mirrorOf>
      <! -- The URL of the image. The build system will use this URL in preference to the default server URL. Maven-public = maven-public = maven-public = maven-public
      <url>http://ip address: port /repository/maven-public/</url>
    </mirror>
Copy the code

Extension –mirrorOf

* means all! Said the

,! Repo1: Look for external: : in a repository other than REpo1! Internal. Repo, * the opposite

– * * extensionmirror

  • By default, multiple configurations are configuredmirrorIn this case, only the first one takes effect.
    • When you can’t connect, you go to the last one; The desired effect is that when a jar package does not exist in the first mirror, Maven will search the second mirror for downloads, but Maven will not do that!

The correct action is to configure multiple profiles under the Profiles node and activate!!!! after configuration

The configuration profile

<! -- Aliyun -->
   <profile>
          <id>aliyun</id>
          <repositories>
              <repository>
                  <id>aliyun</id>
                  <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
                  <releases>
                      <enabled>true</enabled>
                  </releases>
                  <snapshots>
                      <enabled>true</enabled>
                      <updatePolicy>always</updatePolicy>
                  </snapshots>
              </repository>
          </repositories>
   </profile>

<! -- Private server -->
  <profile>
<! Select * from 'id' where id = '0';
      <id>nexus-pr</id>
      <! -- Remote repository list -->
      <repositories>
        <repository>
                 <! Select * from warehouse where id = >
          <id>nexus-Melo</id>
          <name>nexus-Melo</name>
          <! -- virtual URL form, pointing to the image URL-->
          <url>http://ip address: port /repository/maven-public/</url>
          <layout>default</layout>
          <! Releases the releases version from this repository.
          <releases>
            <enabled>true</enabled>
          </releases>
          <! -- Indicates that the snapshot version of the component can be downloaded from this repository -->
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
        </repository>
      </repositories>
     <! -- There is no need for custom plugins yet -->
            <! -- Plugin repository list -->
<! -- <pluginRepositories>-->
<! -- <pluginRepository>-->
<! -- &lt; ! &ndash; The warehouse to be pulled up. &gt; -->
<! -- <id>nexus-Melo</id>-->
<! -- <name>nexus-Melo</name>-->
<! - < url > http://114.132.235.87:9001/repository/maven-public/ < / url > -- >
<! -- <layout>default</layout>-->
<! -- <snapshots>-->
<! -- <enabled>true</enabled>-->
<! -- </snapshots>-->
<! -- <releases>-->
<! -- <enabled>true</enabled>-->
<! -- </releases>-->
<! -- </pluginRepository>-->
<! -- </pluginRepositories>-->
    </profile>

Copy the code

To activate the profile!!!!!

				<! Id of profile defined above!! -->
<! -- Activate Alicloud -->
      <activeProfile>aliyun</activeProfile>
<! - to activate the nexus - >
      <activeProfile>nexus-pr</activeProfile>
      <activeProfile>JDK - 1.8 -</activeProfile>
Copy the code

Realize the effect of coexistence of Ali cloud and private service

Those central warehouse have files, will go ali cloud image to download, if it is our custom JAR package (central warehouse does not have), will go to our private server to download!

Another way to do it

  • In fact, we can also directly create a proxy warehouse in the private server, let him proxy Ali cloud mirror, but in fact, there is a problem, is the proxy warehouse itself does not allow us to customize the upload components, so overall consideration or the above self-configuration method is better

Write in the last

  • The Maven piece, the concrete aggregation, the inheritance examples, should be permeated when it comes to really using distributed development. And the use of private service, on many machines is very important!