Deploy Maven and Nexus

Blog Outline:
1. Introduction to Maven
1) Maven's functions:
2) Maven usage scenarios (from a maintenance perspective):
3) Maven Warehouse
II. Deployment of Maven
1) Configure the jdk environment
2) Deploy Maven
3. Deploy Nexus Private Service
1) Introduction to Nexus
2) Deploy Nexus
4. Maven configures nexus private services and sets up cost-effective Nexus private services for remote warehouses
1) Modify maven's master profile
2) Create a maven project for testing
3) Configure certification to upload packaged items to nexus
4) Upload to maven-release repository
5) Upload 3rd party maven

1. Introduction to Maven

Maven, the next free open source project belonging to the apache Software Foundation, is a cross-platform project management tool. Maven uses a concept called Project Object Model (POM) to manage projects. All project configuration information is defined in a file called POM.xml.Mainly serves Java platform-based project construction, dependency management and project information management, can automatically resolve the dependency environment of code in the project during the code deployment process, need to download the dependency environment from maven's mirror warehouse, usually slower download, enterprise will set up nexus (private service) within the company, save the dependency environment in the construction project to neXus server, improve access speed.

The POM project object model is the basic unit of work for the Maven project, an XML file that contains basic information about the project, describing how the project is built, declaring project dependencies, and so on.
When a task or target is executed, Maven looks for the POM in the current directory.Read the POM, get the required configuration information, and then execute the target.

The following configurations can be specified in POM: project dependency, plug-ins, execution goals, project build profile, project version, project development list, related mailing list information.

1) Maven's functions:

1) 1. The Java project of mavne model has specification and directory planning for source code, unit test code, resources, jar package, etc.
2) Resolve dependencies, version inconsistencies, version conflicts among projects;
3) Reasonable jar management mechanism;

2) Maven usage scenarios (from a maintenance perspective):

1) The jar package in the original project must be manually copied and pasted into the WEB-INF/lib project. With the help of maven, the jar package can only be saved in the warehouse. If there are projects that need to be used, only this file needs to be referenced and not duplicated in the project.
2) All the jar packages needed in the original project are downloaded in advance, while maven will automatically download the required jar packages when connected to the network.First find it in the local warehouse and download it online if you can't find it.
3) Other jar packages that a jar package depends on in the original project must be imported manually, and maven will automatically import the dependent jar packages;
4) The original project is a project, and with the help of maven, a project can be divided into several projects;

The project construction process includes the following steps: [Clean up Project] [Compile Project] [Test Project] [Generate Test Report] [Package Project] [Deploy Project]. These six steps are the complete construction process of a project.

3) Maven Warehouse

In Maven's technical terminology, a warehouse is a location where a Maven warehouse is a dependent third-party library in a project. The location of this library is called a warehouse.In Maven, any dependency, plug-in, or project-built output can be called a component.

The Maven repository helps us manage components, primarily Jar, where all JAR files (WAR, ZIP, POM, and so on) are placed.

There are three types of Maven warehouses: local, central, and remote.

1) Local warehouse

Maven's local repository, which is not created after Maven is installed, was created the first time the Maven command was executed.

When Maven is running, any components Maven needs are obtained directly from the local repository.If the local warehouse does not exist, it will first attempt to download components from the remote warehouse to the local warehouse, then use components from the local warehouse.

By default, whether Linux or Windows, each user has a warehouse directory with a path named.m2/respository/under his or her user directory.

2) Central warehouse

The Maven Central Warehouse is a warehouse provided by the Maven community that contains a large number of commonly used libraries.

maven has a built-in remote public warehouse: .

The central repository contains the vast majority of popular open source Java components, as well as source, author, SCM, information, license information, and so on.In general, simple Java project-dependent components can be downloaded here.

3) Third-party warehouses

Third-party warehouses are also known as internal central warehouses, also known as private services.

Private service: Generally set up by the company itself, only for internal sharing.It can be used both as an internal corporate component for collaboration and archiving, as well as as as as a public library mirror cache to reduce the frequency of external access and downloads (using private services reduces access to the central warehouse).

For more information, you can walk through this document: Maven Tutorial

II. Deployment of Maven

Installing Maven relies on a java environment, so deploy the jdk environment!

Note: All source packages used in this post can be found in the network link provided by me download

1) Configure the jdk environment

[root@maven ~]#  Rpm-qa | grep JDK //Query native default JDK environment
[root@maven ~]# yum -y remove java-1.7.0-openjdk-headless-
[root@maven ~]# yum -y remove java-1.8.0-openjdk-headless-
//Unload the original jdk-headless package
[root@maven ~]# Tar zxf jdk-8u211-linux-x64.tar.gz-C/usr/local//Unzip the provided JDK package
[root@maven ~]# Vim/etc/profile//Write system environment variables, adding the following at the end
        export JAVA_HOME=/usr/local/jdk1.8.0_211                     //Specify Java installation path
        export JRE_HOME=/usr/local/jdk1.8.0_211/jre                  //Java runtime environment
        export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar              //Declare a class in Java that is a property of a type of transaction
        export PATH=$JAVA_HOME/bin/:$JRE_HOME/bin/:$PATH             //Add Defined Variables to System Environment Variables
[root@maven ~]# Source/etc/profile//reload system environment variable file
[root@maven ~]#Java-version //Query supported Java version information
java version "1.8.0_211"                              //Meet Requirements
Java(TM) SE Runtime Environment (build 1.8.0_211-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode)

2) Deploy Maven

[root@maven ~]# wget
#You can use apache-maven-3.6.1-bin.tar.gz directly from your network disk
[root@maven ~]# tar zxf apache-maven-3.6.3-bin.tar.gz -C /usr/src
[root@maven ~]# mv /usr/src/apache-maven-3.6.3/ /usr/local/maven
[root@maven ~]# vim /etc/profile           #Add the following
export MAVEN_HOME=/usr/local/maven
[root@maven ~]# source /etc/profile
[root@maven ~]# mvn -v        #Deployment succeeded when the following appears (because this is a binary deployment)
Apache Maven 3.6.3 (cecedd343002696d0abb0b32b541b8a6ba2883f)
Maven home: /usr/local/maven
Java version: 1.8.0_211, vendor: Oracle Corporation, runtime: /usr/local/jdk1.8.0_211/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-862.el7.x86_64", arch: "amd64", family: "unix"

1) Configure and use Aliyun warehouse address

Because the address of maven's official website is used.Download is slow, so change directly to the Maven warehouse address provided by Aliyun!

[root@maven ~]# vim /usr/local/maven/conf/settings.xml
#Go to 158 lines and fill in
            <name>aliyun maven</name>
  </mirrors>          #Must be written inside this label
#Mirrors are mirror s, which provide a convenient way to switch remote warehouse addresses
#The id of the warehouse to be replaced is in <mirrorOf>central</mirrorOf>Act on the original warehouse, if * indicates replacement of all warehouses   

2) Build the project for testing

[root@maven ~]# mkdir /tmp/testdir
[root@maven ~]# cd /tmp/testdir/
#Execute the following command to test
[root@maven testdir]# mvn archetype:generate -DgroupId=com.lzj.maven.quickstart -DartifactIdk=testapp -DarchetypeArtifactId=maven-archetype-quickstart
#Create a maven project (Java type) with -DinteractiveMode=false to cancel the interaction process
                     ...........................#Omit some content
Define value for property 'artifactId': testapp      #You need to specify the project name manually, you can customize it, and then you can keep the default!
[INFO] BUILD SUCCESS        #When this line of prompts appears, the build is successful
#The above command is interpreted as follows:
# archetype:generate: Create a simple java project;
# Archtype is a prototype, a Maven plug-in, or exactly a project template, whose task is to create a project structure from the template.
#Create a simple java application using the quickstart prototype plug-in
# -DgroupId: Organization name, rewriting of company web address (reverse domain name) + project name;
# -DartifactIdk: Project name or module name;
# - DarchetypeArtifactId: Specify ArchetypeId, maven-archetype-quickstart, to create a simple Java application;
# -DinteractiveMode: Whether interactive mode is used.

3) Interpretation of generated POM file parameters

Content generated by current directory:

[root@maven testdir]# ls   #After successful execution of the above command, a directory named after the project name is generated in the current directory
[root@maven testdir]# cd testapp/      #Enter the generated directory
[root@maven testapp]# ls                #View files generated in directory
pom.xml  src
[root@maven testapp]# cat pom.xml         #Explanation of pom file parameters
project: Root, this is right Project Add constraint information for some root elements
modelversion: Specify the current maven Version number of model
grouopId: It should be the name of the company or organization.generally speaking groupID There are three parts, each with a "."Separation, the first part is the purpose of the project, such as for business com,For noncommercial profit organizations, these are org,The second part is the company name, such as tentxun/baidu/alibaba,The third part is your project name
artifactId: Can be considered maven The name of the project you are building, for example, if you have subprojects in your project, you can use the Project Name-Naming Subprojects
packaging:  Specify the generated format ( jar/war/rar/pom/ear)
version: Version number, SNAPSHOT Meaning snapshot, indicating that the project is still under development and is an unstable version
name: Name of project, Maven Generated document uses
url: Project Home Page url,Maven Generated document uses
dependencies Tag: Specify the development build ( jar Package)
JUnit Is a Java Unit Test Framework for Language

Local warehouses are also generated under the user hosting directory:

[root@Maven testdir]# ls ~/.m2/repository/
antlr  asm  commons-codec  commons-collections  commons-io  commons-lang  jdom  net  org

4) Maven Profile

Maven's profile name is: set.xml.It is stored in two places:
1)/usr/local/maven/conf/settings.xml #Installed directory;
2) ~/.m2/settings.xml #user's home directory;

Installation directory configuration is also a global configuration, and user directory configuration is called user configuration.If both exist, their content will be merged and user-scoped settings.xml will take precedence.

There is no configuration file in the default user directory. You can copy settings.xml from the Maven installation directory to the user directory.

Common configuration modifications for the settings.xml file are as follows:
1) Modify the local warehouse storage path. The default value is ~/.m2/repository, which can be modified to its desired directory in settings.xml;
2) If the build server cannot connect to the remote warehouse due to network failure or security issues, set the offline property to true and default to false in offline mode.
3) Modify the central warehouse server, modify the notes in the configuration file, add or modify the mirror address;
4) Set proxy address, modify proxy label in configuration file, define proxy server, and implement maven proxy.

Since the central warehouse server is abroad and downloads are slow when used in China, it is recommended that domestic users use the Aliyun warehouse address.

5) Common Maven Commands

[root@maven testapp]# pwd      #Verify that the current directory is under the project you built earlier
[root@maven testapp]# mvn compile   #Compile (during compilation, you can see that the warehouse in Ali Cloud is used)
[INFO] BUILD SUCCESS      #Successful execution is indicated as long as the line is seen in the last few lines of output information
#All future commands are the same, and as long as the last few lines have the above information, execution is successful
[root@maven testapp]# ls    #You can see that a new directory target is generated under the directory
pom.xml  src  target
# target: Package the output directory, such as a packaged jar or war file;
# target/classes: Compile output directory;
[root@maven testapp]# mvn test          #Test Command
[root@maven testapp]# ls target/test-classes/
# target/test-classes: test compilation output directory
[root@maven testapp]# mvn clean   #Cleanup command (that is, delete the directories generated by compilation and testing)
[root@maven testapp]# ls      # The target directory is no longer there
pom.xml  src
[root@maven testapp]# mvn package       #tar
[root@maven testapp]# ls target/     #Will generate a jar package
classes         surefire-reports
maven-archiver  testapp-1.0-SNAPSHOT.jar
maven-status    test-classes
[root@maven testapp]# mvn install         #Installation command (for uploading compressed file jar or war packages to the local repository)
[root@maven testapp]# ls ~/.m2/repository/com/lzj/maven/quickstart/testapp/1.0-SNAPSHOT/
#View Uploaded Packages
maven-metadata-local.xml  testapp-1.0-SNAPSHOT.jar
_remote.repositories      testapp-1.0-SNAPSHOT.pom
# Using the maven install command directly to upload the project to the local repository,
# Then all the previous steps will be executed automatically, such as source code compilation, packaging, and so on.
[root@maven testapp]# mvn deploy      #Publish (upload package to private service, I don't have private service here, so it will fail)

6) Life cycle of mvn commands

maven is based on the central concept of building a life cycle, which means that the process of building and distributing a specific project is clearly defined as follows:
Rough life cycle diagram:

Detailed life cycle diagram:

7) Build a web project, make a war package, and deploy it to tomcat

[root@maven testdir]# mvn archetype:generate -DgroupId=cn.test.testweb -DartifactId=testweb -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
#Build a web project
#The resulting directory structure is as follows:
[root@maven testdir]# ls testweb/
pom.xml  src
[root@maven testdir]# cat testweb/src/main/webapp/index.jsp 
<h2>Hello World!</h2>
[root@maven testdir]# cd testweb/   #Enter the project directory
[root@maven testweb]# mvn package         #Package as a war package
[root@maven testweb]# ls target/      #A war package will be generated in this directory
classes  maven-archiver  testweb  testweb.war
#Move the generated war package to Tomcat's web apps and it will automatically unzip.
#You can then access the war package generated by the Tomcat test
[root@maven testweb]# wget
[root@maven testweb]# tar zxf apache-tomcat-9.0.31.tar.gz 
[root@maven testweb]# mv apache-tomcat-9.0.31 /usr/local/tomcat        #Install tomcat
[root@maven testweb]# mv target/testweb.war /usr/local/tomcat/webapps/ #Move to the Tomcat page root directory
[root@maven testweb]# /usr/local/tomcat/bin/    #Start Tomcat
[root@maven testweb]# ls /usr/local/tomcat/webapps/      #View, the war package is automatically decompressed
docs      host-manager  ROOT     testweb.war
examples  manager       testweb

Client access to the war package moved to the root directory of the Tomcat Web page, as shown in the following figure:

3. Deploy Nexus Private Service

1) Introduction to Nexus

Nexus is the Maven Warehouse Manager. If you use Maven, you can download the required artifact s from the Maven Central Warehouse, but this is usually not a good practice. You should set up a Maven Warehouse Server locally and maintain the local warehouse while proxying the remote warehouse to save bandwidth and time, which Nexus can do.In addition, he provides a powerful warehouse management function, component search function, which is based on REST and a friendly UI is an extjs REST client, which takes up less memory and is based on a simple file system rather than a database.These advantages make it the most popular Maven warehouse manager.

Nexus is not the core concept of Maven, it is just a special Maven repository derived from it.For Maven, there are only two types of warehouses: local and remote.

The local warehouse is the local warehouse address that we configured in maven's setting.xml.Since the original local repository is empty, Maven must know at least one remote repository available to download the required components when executing the Maven command.The central warehouse is the default remote warehouse for maven.

Private service is a special kind of remote warehouse which is set up in the local area network. The purpose is to proxy the remote warehouse and deploy third-party components.With private service, when Maven needs to download a component, request it directly, and download it to the local warehouse if it exists. Otherwise, private service requests an external remote warehouse, downloads the component to the private service, and then provides it to the local warehouse for download.

Nexus features:
1) Save the bandwidth of the external network.A large number of repeated requests to external warehouses consume bandwidth. Using private service to proxy external warehouses, you can eliminate external duplicate component downloads and reduce bandwidth pressure.
2) Accelerate Maven construction.Accelerate Maven build.Connecting to requests for external warehouses is time consuming, and Maven keeps checking data from remote warehouses while performing the build.With private services, Maven only checks the data in the LAN to speed up the build.
3) Deploy third-party components.Deploy third-party components.When a component is not available from any external remote warehouse.Once private services are established, these components can be deployed to private services for internal Maven projects.
4) Improve stability and control.Improve stability and control.Maven builds are highly dependent on remote warehouses, so when the network is unstable, Maven builds become unstable or even impossible to build.Private service caches are heavily built so that Maven can function properly even if there is no network for the time being.
5) Lower the load on the central warehouse.Lower the load on the central warehouse.Using private services can avoid many duplicate downloads to the central warehouse and reduce the pressure on the central warehouse.

2) Deploy Nexus

This software is slowly downloaded from the official website and can be obtained from the webdisk link at the beginning of the blog post!

[root@maven ~]# mkdir /usr/local/nexus
[root@maven ~]# tar zxf nexus-3.17.0-01-unix.tar.gz -C /usr/local/nexus/
#To start nexus, you must use the nexus user. Do not use users with high privileges, such as root, or the startup will fail
[root@maven ~]# useradd nexus
[root@maven ~]# chown -R nexus:nexus /usr/local/nexus/
[root@maven ~]# cd /usr/local/nexus/
[root@maven nexus]# ls
nexus-3.17.0-01      #This is the application directory
sonatype-work       #This is the working directory for the mirror warehouse
#Run memory and working directory nexus-3.17.0-01/bin/nexus.vmoptions (modify the corresponding fields)
#Run listening address and port nexus-3.17.0-01/etc/
[root@maven nexus]# ln -s /usr/local/nexus/nexus-3.17.0-01/bin/nexus /usr/local/bin/
#Create Command Soft Connection
#Switch to the nexus user and start the nexus service. If you use the root user, the start will fail because of the high privileges
[root@maven nexus]# su nexus
[nexus@maven nexus]$ nexus start      #Start nexus
Starting nexus
[root@maven nexus]# netstat -anpt | grep 8081      #Default listen on port 8081
tcp        0      0  *               LISTEN      61881/java 

Launch successful, browser access:

[root@maven ~]# cat /usr/local/nexus/sonatype-work/nexus3/admin.password
da8b24ed-8bd2-42c5-864f-858216a21cf4       #Copy the password information you see

1) Create roles


2) Create user


3) View the default warehouse type


Default warehouse type description:

  • Group (warehouse group type): also known as group warehouse, for the convenience of developers, their own set of warehouses
  • hosted: the publishing repository for internal projects (the repository that internal developers publish to store);
  • proxy: Find the warehouse of data from the remote central warehouse (you can click on the Configuration page of the corresponding warehouse to sign the value of the Remote Storage Location property, which is the path of the remote warehouse being proxied);
  • Virtual (virtual type): virtual warehouse (this is rarely needed);

Warehouse type under Public Repositories:

  • 3rd party:: A third-party released version of a component warehouse that cannot be obtained from a public warehouse, that is, a third-party dependent warehouse, this data is usually downloaded by an internal person and posted on their own;
  • Apache Snapshots: Component repository using proxy Apache Maven repository snapshot version
  • Central:: The repository used to proxy the release of version components in the maven central repository
  • entral M1 shadow:: Component mirror repository for delivering a released version of M1 format in the central repository
  • Codehaus Snapshots:: The repository used to proxy the snapshot version components of the Codehaus Maven repository
  • Releases:: The release repository of the release module in the internal module to deploy the host type repository that manages the internal release component; release is the release version;
  • Snapshots: A repository that publishes internal SNAPSHOT modules to deploy a host-type repository that manages internal snapshot version components; snapshots are snapshot versions, i.e. unstable versions;

4) Restriction on recurrence of release s

Repeat releases are required in development, and the corresponding permissions setting allow redeploy is required to open the release type repository.The following:

5) Set up agent warehouse (using Aliyun maven warehouse)


URL of Ali Cloud Warehouse:

6) Add the added Aliyun Proxy to the default group


After saving, refresh the page again to see the current Nexus address:

Common usage scenarios for the above configurations are described below:

  • Release repository (nexus is built by default: maven-release ees);
  • Snapshot test center snapshot warehouse (nexus is built by default: maven-snapshots);
  • Central warehouse (nexus is built by default: maven-central);

About Groups: Groups can be created in nexus to bring different types of warehouses together (nexus is built by default: maven-public)

  • Scenario 1: Customize the new proxy type warehouse, configure different invocation addresses in the maven configuration, or unify the custom new proxy warehouse into a group and invoke an address in the maven configuration;
  • Scenario 2: Use the proxy repository that Maven has already established, and configure the Maven call address using the maven-public group that has already been established;
    Scenario 1 and Scenario 2 are essentially unchanged and can be configured according to the company's development habits and needs.

4. Maven configures nexus private services and sets up cost-effective Nexus private services for remote warehouses

1) Modify maven's master profile

[root@maven ~]# vim /usr/local/maven/conf/settings.xml 
..............................   #Omit some content    
      <name>nexus testconf</name>      
#The URL above is the one you see on nexus     
             ..........................#Omit some content
  #Jump to around 260 lines and write the following



  </profiles>       #Need to add above this label!
             ..........................#Omit some content  
    <activeProfile>testconf</activeProfile>    #The value here must correspond to the ID of the profile above before it can be activated
</settings>          #Write the <activeProfiles>field above the settings field

2) Create a maven project for testing

You need to first turn on the permission to write anonymously to nexus as follows:

[root@maven ~]# rm -rf ~/.m2/         #Delete local warehouse cache first
[root@maven ~]# cd /tmp/testdir/
[root@maven testdir]# mvn archetype:generate -DgroupId=cn.ljz.ljzweb -DartifactId=lzjweb -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
#Building the project, you can see that the nexus private service address was used during the construction process.
[root@maven testdir]# cd lzjweb/      #Enter the post-build project
[root@maven lzjweb]# mvn package        #Package the project
[root@maven lzjweb]# ls                 #After packaging, the target directory is generated
pom.xml  src  target

View from a web page:

3) Configure certification to upload packaged items to nexus

[root@maven lzjweb]# vim  /usr/local/maven/conf/settings.xml
#Search for "server" field
    -->         #Locate the line and write the following, about 124 lines
      <id>test-nexus</id>     #Custom ID
      <username>testdevops</username>   #User name ID number that was just added to nexus's web page
      <password>123456</password>     #User name password
#Modify the pod.xml file in the project        
[root@maven lzjweb]# pwd
[root@maven lzjweb]# ls
pom.xml  src  target
[root@maven ljzweb]# vim pom.xml    #Edit Profile
      .....................#Omit some content, the following is added manually
            <id>test-nexus</id>     #The ID here must match the ID configured for authentication in the settings file above
            <name>Nexus Snapshot</name>
                        #Above is the revised URL, which can be viewed in the web interface of nexus
            <name>Nexus releases</name>
                        #Above is the URL of the release version, which can also be seen in Repositories on the web interface
</project>           #Add above the last line

View from a web page:

If an error occurs during deployment, first check that the account password of the configuration file set.xml is the same as the user password created by nexus. Then check that the id tag in the set.xml is the same as the id tag in the pom.xml file. The difference will result in a 401 (no permission) error.

4) Upload to maven-release repository

[root@maven lzjweb]# vim /tmp/testdir/lzjweb/pom.xml 
..................          #Omit some content
    #Remove the "SNAPSHOT" from the above line and change it as follows:
[root@maven lzjweb]# mvn deploy      #Upload again

View from a web page:

5) Upload third-party maven package to nexus private service


Maven Add Third Party Warehouse Address (Global Configuration)

[root@maven lzjweb]# vim /usr/local/maven/conf/settings.xml 
#In the previous profiles (close label is almost 293 lines), fill in the following
  </profiles>             #Add to this Profiles tab (293 lines before adding)
#Add another authentication to the previous authentication field
    </server>            #This line was previously added, add the following again
      <id>3rdnexus</id>    #This ID corresponds to the ID in the profile
[root@maven testdir]# ll aliyun-sdk-oss-2.6.1.jar 
-rw-r--r-- 1 root root 470115 10 January 1017:43 aliyun-sdk-oss-2.6.1.jar
[root@maven testdir]# mvn deploy:deploy-file -DgroupId=com.aliyun.oss -DartifactId=aliyun-sdk-oss -Dversion=2.6.1 -Dpackaging=jar -Dfile=/tmp/testdir/aliyun-sdk-oss-2.6.1.jar -Durl= -DrepositoryId=3rdnexus
#Building projects based on local jar packages      

View the uploaded jar package on the web page:

To the end of this article, thank you for reading -----------

Tags: Linux Maven nexus xml Java

Posted on Wed, 19 Feb 2020 14:52:04 -0500 by hejgelato