Master Maven PW

Foreword

In Java EE development, we rely on third-party libraries using Maven build tools to manage major projects and other projects within the company depend on the service. Maven PW is therefore essential link in this paper, the three main aspects of Maven PW are introduced:

  • What is Maven PW
  • How to build Maven PW
  • How to use Maven PW

This paper face readers should use Maven experience, to understand some basic concepts of Maven, if not familiar with can see Resources to learn at the end of the text.

What is Maven PW

Before introducing Maven PW, we must first know what is under the Maven repository.

Maven repository: Maven unified storage for all Maven project dependencies, plug-ins, and places the uploaded project.

And Maven repository is also divided into two categories: local and remote warehouse warehouse.

  • Local warehouse, that is, after our local computer to install Maven, in the configuration file setting.xml in localRepositorythe need to specify the directory that folder.
  • Remote repository, is to specify an external URL in the network (including LAN) Java library of specialized storage, server plug-ins Maven central repository of ,, for example, is a typical remote repository, which contains the majority of the world of open source Java library , there are many other similar public libraries

The PW mentioned in this article, is another special remote repository, it is usually set up on a LAN server, a proxy for all external remote repository, and can accept uploads and access to internal projects.

These warehouses have what use is it? When Maven project relies on a number of other projects Java libraries, Maven will first look in the local repository, if the corresponding library exists, it directly; if there is no local repository, Maven will go to a remote warehouse find, find the corresponding Java library after downloaded to the local warehouse re-use, so that the next time you need Java library cache dependency libraries can be used directly in a local warehouse, eliminating the need for repeated lookups over the network and download time for Maven plug-in is the same process.

Typically, the default Maven central repository is a remote warehouse, and when the introduction of the PW, the local repository search ends when not dependent libraries, you start with PW Find warehouse needed to be found, yet to find it, and finally go to the center Find warehouse, the specific process may refer to the following chart:

Here we look at the role of PW which:

  • Network access, saving external network bandwidth.
  • Once downloaded outside the network, all network users can only download PW cache, accelerate Maven project build.
  • It allows uploading and downloading private library, and not externally accessible, more secure.
  • Reduce the external network elements, providing stability to build the project.
  • Facilitate internal service projects depend on references, complete source code without the need of other items.

Here at last add a small dot on the role of specific description: When we have two independent Maven projects, such as order services and member services, such as order service project requires Member Services, Member Services API package dependent by PW can API package to members of the Build service to upload, download and order service direct reference member on the PW service API packages can be, so there is no need to import Member services Code, we do not care about the specific implementation of service members, played inside services mild references to the role description can be found below.

Build Maven PW

Knowing Maven PW, we further study. We begin to build a Maven PW. Here we use the most popular open source Maven repository management software - Nexus, Maven PW to quickly build, build traditional way to install the Nexus official website to download the open source version of the Nexus OSS: https: //www.sonatype.com/nexus -repository-oss. The paper will use the Docker installed Nexus, not only quick and easy, and takes less machine resources.

If you are not familiar with the students Docker, you can see 10 minutes Docker quickly master the necessary basics to learn about, the simple use of Docker is also very easy.

Download Nexus Mirror

Docker instructions on using terminal console download Docker image provided by the official Nexus:

When you see the following figure result, it means the image has been downloaded, you can docker imagesview it.

image-20190825165531217

Start Nexus container

Start using the instructions below Nexus container:

Here Docker parameter input instruction is briefly described:

  • -d It represents the container to run in the background.

  • -p 8081:8081It represents an open internal port 8081 (the latter of 8081) when the container starts mapping host port 8081, i.e., through localhost:8081access to the service provided by the container Nexus.

  • -v /Users/One/Desktop/Nexus/nexus-data:/nexus-dataRepresents the inside of the container /var/nexus-datamounted to the specified directory the current host, it should be noted that, -voutside the designated path must be full path.
  • --restart=always Relatively simple means that when docker service starts, the container also followed start.

After executing the above command, wait a moment, access http://localhost:8081/, see the corresponding Nexus backstage, says PW installation is complete, it is not very simple.

Container startup process, due to the different machine configurations, the situation may appear slow start, just be patient.

After the startup is complete, we can view the file Nexus container runs produced under the mounted local directory.

Configuring Maven PW

PW after installation, we must first log in operation, click the Sign In button left corner of the page, there will be just the same as the figure below:

Description of Nexus default login account is admin, and the password in the file /nexus-data/admin.passwordunder, we simply check in the local configuration file to mount the directory.

After a successful login After entering the default account password, Nexus will be forced to change the admin password, and set the basic access rights, after the completion of the formal entry into the Nexus PW background.

Here a brief description of the interface elements:

  1. The default browser interface, you can view the search of the warehouse components, as well as upload.
  2. PW is used to manage the configuration program.
  3. View account information, allows you to modify the password.

PW program into the configuration interface, we need to make a few adjustments to the default configuration.

Acting path central warehouse setting

The proxy configuration repository path PW in central warehouse is updated with Ali cloud warehouse proxy address provided: https: //maven.aliyun.com/repository/central way you can more quickly access relies on a central warehouse and needed plug-ins.

New custom warehouse

Click Create repository, and then select the typemaven2(hosted)

Then enter the name of the warehouse private-release, and choose Release Version policy in a column indicates a storage warehouse is dependent on the components of the official release, then Deployment policy column select Allow redeploy, to allow the deployment and update components on the PW.

Finally, click on the blue button - Create repositoryafter, you can see a list of the warehouse in the warehouse of the custom, with the release warehouse, then we operate in the same way to add a snapshot warehouse, just in Version policy for the Snapshot column can be adjusted.

Add Role

Nexus default only two roles: nx-anonymousand nx-adminpermissions only the former view, which, under normal circumstances, we need to create a positive role for developers to administrator privileges. Click Create Roleto add a Role ID for the developercustom role, and only add a custom repository usage rights, except delete operation.

After saving, the new role on the display of the list, with the role, you can associate users, permissions are assigned.

image-20190825190139940

Add user

Similarly with the authority, the default user only two: adminand anonymouswe also need to create user objects that belong to developers. Click Create local user, fill in the required information a user name, password, etc. After that, the role of our previous association created, and save it.

After the user is created, we can log in with the new PW users to view the contents of the corresponding rights, for example, only view and search custom PW warehouse we use the new user logs on, can operate.

As a result we have created their own PW warehouse, after the configuration is complete, developers can use the Maven project development.

Use Maven PW

With PW and for the development of the account, we need to associate local Maven profile setting.xml.

Set server account information:

Set PW warehouse address :( address here, you can page through the warehouse on stage after the Nexus cpoyAuto-Replication button)

Maven project to deploy PW

Once configured, we can create a Maven project, Maven PW package to the next attempt. Using the IDE to quickly generate Maven project, add POM file distributionManagementnode, and specifies the address and the corresponding id PW warehouse, as follows:

Finally, whenever the deployment command mvn clean deploydeployment or by IDE plug-ins when the console appears BUILD SUCCESS, it indicates below Similarly deployed.

As can be seen from the log, upload all our projects under private- snapshot warehouse, then we can private- snapshot warehouse on the PW site to view uploaded jar.

If the following prompt deployment: With Access denied toit indicates lack of user rights setting.xml configuration can not deploy the jar up to the corresponding PW warehouse.

Note that, for a Maven project, if there are "SNAPSHOT" the word version of the project, it said that in the current development version, Maven will be posted to the snapshotRepositorynode corresponding to address up. Otherwise, Maven think that this is a release version, the program will be released to repositorythe node corresponding address. Since version for sample project 1.0.0-SNAPSHOT, the final project uploaded to private-snapshotthis Snapshot type of warehouse.

Then if other projects to rely on the jar, only the coordinates corresponding to the need to introduce in its POM file.

The third-party jar package deployed to the Maven PW

Here comes a third party jar package, instead of the usual open source library mentioned, more of a jar packets from other third-party systems, because the project needs, the way to use the project to import inconvenient, the better approach is to Upload the jar third-party system package manually to the Maven PW, the project needs to use to pull directly from PW warehouse.

This implement also more convenient manner, it is divided into two: the visual interface operation and the command line.

Visual operation interface: that is, after the stage into the Nexus, execute permissions to upload must be uploaded to the can, generally operate an administrator account, go after the upload page, select the warehouse, the local project designated to be uploaded, you finish customizing your coordinates information can be saved.

Command line: The following commands can be entered directly in the terminal:

  • -DgroupId and -DartifactId constitute coordinates of the jar pom.xml package, i.e., corresponding to dependent groupIdandartifactId
  • - Dfilerepresents the absolute path of the need to upload the jar package
  • -Durl url precise address for the PW warehouse
  • -DrepositoryId PW warehouse is defined id
  • -Dversion specifies the version number
  • -Dpackaging manner specified packaging

When the console log occurs BUILD SUCCESSwhen information indicates Packaging Success, if a failure occurs pack, most likely Maven configuration file serveris insufficient user rights under the elements led to the need for users Nexus backstage roles assigned upload rights.

Epilogue

In Java enterprise project development, establish and maintain PW is an essential step in the use of Maven, Maven of this paper briefly, to quickly set up and use to further understand the Maven PW, pave the way for the follow-up of micro-enterprise services architecture.

Recommended Reading

Reference material

  • Maven entry, read this enough: https: //juejin.im/post/5a4a5e2bf265da4322418d7f#heading-2
  • Maven combat: https: //book.douban.com/subject/5345682/

  • PW maven repository command to upload the jar package: https://www.jianshu.com/p/ccd7b643624c
  • What is a Maven Repository:https://medium.com/@chris.shellenbarger/what-is-a-maven-repository-6fa22991fad9

Guess you like

Origin www.cnblogs.com/one12138/p/11426451.html