Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The use of maven+nexus

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Maven introduction:

Maven is the next open source and free project of the apache Software Foundation. It is a cross-platform project management tool. Maven uses a concept called Project Object Model (POM) to manage the project. All the project configuration information is defined in a file called POM.xml. It mainly serves the project construction based on Java platform, dependency management and project information management. The dependent environment of the code in the project can be solved automatically in the process of code deployment. The dependent environment needs to be downloaded from the maven image warehouse, which is usually slow. Nexus (private server) will be set up within the company in the enterprise, and the dependent environment in the construction project will be saved to the nexus server to improve access speed.

The POM project object model, which is the basic unit of work of the Maven project, is an XML file that contains basic information about the project, describing how the project is built, declaring project dependencies, and so on.

When performing a task or target, Maven looks for 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-in, execution target, project build profile, project version, project development list, related mailing list information

Mavne function:

The Java project of mavne model has a reasonable jar management mechanism for source code, unit test code, resources, jar packages, etc., and directory planning to solve the problems of dependency, version inconsistency and version conflict between projects.

Application scenario: understanding from the perspective of operation and maintenance

1. The jar package in the original project must be manually copied and pasted into the WEB-INF/lib project, but with maven, the jar package can only be saved in the warehouse. Projects that need to use only need to refer to this file, and do not need to copy it into the project repeatedly.

2. 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 Internet. First of all, find it in the local warehouse, and download it on the Internet if you can't find it.

3. In the original project, other jar packages that a jar package depended on had to be imported manually, and maven automatically imported the dependent jar packages.

4. The original project a project is a project, and with the help of maven, a project can be divided into multiple projects

The project construction process includes [clean up project] → [compile project] → [test project] → [generate test report] → [packaged project] → [deployment project]. These six steps are the complete construction process of a project.

About the warehouse.

Local warehouse

Maven downloads the project dependent components (jar package) remotely to a local directory for management. The default warehouse for each computer is in the "user home directory / .m2 / repository".

Third-party warehouse

The third-party warehouse, also known as the internal central warehouse, has also become a private server

Private service: it is generally set up by the company itself and is only shared within the company. It can be used not only as an internal component of the company to collaborate and archive, but also as a public class library image cache, reducing the frequency of external access and downloads (using private servers to reduce access to the central warehouse)

Central warehouse

Maven has built-in remote public repositories: http://repo1.maven.org/maven2, a public repository maintained by maven itself, contains a large number of common class libraries and contains most of the world's popular open source project artifacts, currently dominated by Java. If the jar package since the project is not available in the local warehouse, it will be downloaded from the central warehouse by default.

Installation of Maven depends on the jdk1.8 version

24 cd / usr/src/ 25 mkdir tomcat create the directory where the tomcat package is placed 26 cd tomcat/ 27 rz upload the tomcat related installation package

29 cd.. 30 mkdir macven+nexus create the directory where the tomcat package is placed 31 cd macven+nexus/ 32 rz upload macven+nexus related installation package 33 ls

35 cd.. / tomcat/ 36 rpm-qa | grep jdk views linux's jdk package

40 rpm-e java-1.7.0-openjdk-headless-1.7.0.111-2.6.7.8.el7.x86_64 java-1.8.0-openjdk-headless-- nodeps removes the native jdk environment 41 tar zxf jdk-8u211-linux-x64.tar.gz-C / usr/local/ unzips the jdk installation package to / usr/local 42 vim / etc/profile Modify the environment variable to add export JAVA_HOME=/usr/local/jdk1.8.0_211export JRE_HOME=/usr/local/jdk1.8.0_211/jreexport CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jarexport PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH 43 source / etc/profile on the last line to refresh the environment variable 44 java-version to view the version number

47 cd.. / macven+nexus/ 48 tar zxf apache-maven-3.6.1-bin.tar.gz 49 mv apache-maven-3.6.1 / usr/local/maven 50 vim / etc/profile modify the environment variable to add export MAVEN_HOME=/usr/local/mavenexport PATH=$PATH:$MAVEN_HOME/bin 51 source / etc/profile on the last line to refresh the environment variable

Test whether maven is installed successfully

52 mvn-v

Build the first project test

55 mkdir / tmp/testdir 56 cd / tmp/testdir/ 57 mvn archetype:generate-DgroupId=com.wuhao.maven.quickstart-DartifactIdk=bdqnapp-DarchetypeArtifactId=maven-archetype-quickstart create a maven project (Java type), enter to confirm if there is interaction, and add-DinteractiveMode=false to cancel the interaction process command explanation: archetype:generate: create a simple java project archetype, that is, a prototype, is a Maven plug-in, to be exact, a project template, its task is to create a project structure according to the template. We will use the quickstart prototype plug-in to create a simple java application. -DgroupId: organization name, reverse of company URL (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 to use interactive mode or not

Parameter interpretation of POM file

[root@localhost bdqnapp] # cat pom.xmlproject: root, which is the constraint information to add some root elements to Project modelversion: specify the version number of the current maven model grouopId: should be the company name or organization name. Generally speaking, groupID consists of three parts, each of which is divided by "." Separation, the first part is the purpose of the project, such as com for business, org for non-commercial for-profit organizations, the second part is the company name, such as tentxun/baidu/alibaba, and the third part is your project name artifactId: it can be thought of as the project name built by maven, for example, your project has subprojects. You can use "project name-subproject naming method packaging: specify the generated format (jar/war/rar/pom/ear) version: version number, SNAPSHOT means snapshot, indicating that the project is still under development and is an unstable version of name: the name of the project, the documentation generated by Maven uses url: the url,Maven of the project home page produces documents with dependencies tags: specify that the development build (jar package) JUnit is a unit test framework for the Java language.

Local warehouse directory structure

[root@localhost ~] # cd .m2 / repository/ installation dependent location [root@localhost repository] # lsantlr commons-codec commons-io jdom orgasm commons-collections commons-lang net

Maven file configuration

Maven setting.xml exists in two places.

1. Installation directory / usr/local/apache-maven-3.6.0/conf/settings.xml

2. User's directory / root/.m2/settings.xml

Among them, the installation directory configuration is also called global configuration, and the user directory configuration is called user configuration. If both exist, their content will be merged, and user-wide settings.xml will take precedence.

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

Common configuration modification

1. Modify the storage path of the local warehouse. The default value is / root/.m2/repository, which can be changed to the desired directory in localrepository in settings.xml.

2. If the build server cannot be connected to the remote warehouse due to network failure or security problems, you need to set the offline attribute to true in offline mode. Default is false.

3. Modify the central warehouse server, modify the notes in the configuration file, add or modify the image address.

Use Aliyun warehouse address

Vim / usr/local/maven/conf/settings.xml # 160Adding alimaven aliyun maven http://maven.aliyun.com/nexus/content/groups/public/ centralmirror is a mirror, which mainly provides a convenient way to switch remote repository addresses. Central is the id of the warehouse to be replaced. Represent the original warehouse. If you fill in *, all warehouses will be replaced.

Build the first project test

64 mkdir / xgp 65 cd / xgp/ 66 mvn archetype:generate-DgroupId=com.wuhao.maven.quickstart-DartifactIdk=bdqnapp-DarchetypeArtifactId=maven-archetype-quickstart

Common Maven commands and demos

68 cd bdqnapp/

69 ls

Compilation: mvn compile

73 ls target/ 74 cat target/classes/com/wuhao/maven/quickstart/App.class 75 cat src/main/java/com/wuhao/maven/quickstart/A 76 cat src/main/java/com/wuhao/maven/quickstart/App.java

/ target packaged output directory, such as packaged jar or war files

/ target/classes compilation output directory

/ target/test-classes test compilation output directory

79 mvn clean 80 mvn package 81 ls target/ 83 mvn install

Test: mvn test

Cleanup: mvn clean

Packaging: mvn package

Install mvn install

Upload the compressed file jar package or war package to the local warehouse

86 ls / root/.m2/repository/com/wuhao/maven/quickstart/bdqnapp/1.0-SNAPSHOT/

Publish: mvn deploy

Upload the package to the private server. There is no private server environment for the time being. This module will be supplemented later.

Command life cycle

Maven is based on the central concept of build lifecycle, which means that the process of building and distributing specific projects is clearly defined.

For example, if you use the maven install command directly to upload the project to the local warehouse, all the previous steps will be performed automatically, such as compiling and packaging the source code, etc.

Build a web project and type out the war package

88 cd.. 89 mvn archetype:generate-DgroupId=cn.bdqn.bdqnweb-DartifactId=bdqnweb-DarchetypeArtifactId=maven-archetype-webapp-DinteractiveMode=false 90 ls 91 cd bdqnweb/ 92 cat src/main/webapp/index.jsp 93 mvn package

Directory structure

Pack the war bag

Tomcat runs the war package, builds the tomcat, places the war package in the root directory of the tomcat page, and starts the tomcat access test

94 cd / usr/src/tomcat/ 95 tar zxf apache-tomcat-8.5.35.tar.gz 96 mv apache-tomcat-8.5.35 / usr/local/tomcat 97 / usr/local/tomcat/bin/startup.sh 98 netstat-anpt | grep 8080 99 systemctl stop firewalld 100cd / xgp/bdqnweb/ 101ls 102cd tar 103cd target/ 104cp bdqnweb/ usr/local/tomcat/webapps/ 105ls 106cp bdqnweb.war / usr/local/tomcat/webapps/ 107pwd

Nexus introduction

Nexus is the Maven warehouse manager, if you use Maven, you can download the required components (artifact) from the Maven central warehouse, but this is usually not a good practice. You should set up a Maven warehouse server locally to agent the remote warehouse while maintaining the local warehouse to save bandwidth and time. Nexus can meet this need. In addition, it also provides powerful warehouse management function, component search function, it is based on REST, friendly UI is an extjs REST client, it takes up less memory, based on a simple file system rather than a database. These advantages make it increasingly 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 repositories: local and remote.

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

Private server is a special remote warehouse set up in the local area network, which aims to represent the remote warehouse and deploy third-party components. After having the private server, when the Maven needs to download the components, it directly requests the private server, and if it exists on the private server, it downloads it to the local warehouse; otherwise, the private server requests an external remote warehouse to download the components to the private server, and then provides it to the local warehouse for download.

Jboss is an open source application server java. Net refers to the java repository and. Net repository

Functions of Nexus:

1. Save the bandwidth of external network. A large number of repeated requests for external repositories will consume bandwidth. Using private servers to proxy external repositories can eliminate external repeated component downloads and reduce the pressure on bandwidth.

2. Accelerate the construction of Maven. Speed up Maven build. Constantly connecting to the external repository is time-consuming, and Maven constantly checks the data in the remote warehouse while performing the build. With private servers, Maven only checks the data of the local area network to improve the speed of construction.

3. Deploy third-party components. Deploy third-party components. When a component is not available from any external remote warehouse. Once the private server is established, these artifacts can be deployed to the private server for use by internal Maven projects.

4. Improve the stability and strengthen the control. Improve stability and enhance control. Maven builds are highly dependent on remote repositories, so when the network is unstable, Maven builds can become unstable or even impossible to build. Private server caches a large number of builds, and Maven can run normally even if there is no network for the time being.

5. Reduce the load of the central warehouse. Reduce the load of the central warehouse. The use of private services can avoid many repeated downloads to the central warehouse and reduce the pressure on the central warehouse.

Install nexus:

108 cd 109mkdir / usr/local/nexus 110cd / usr/src/macven+nexus/ 1111tar zxf nexus-3.17.0-01-unix.tar.gz-C / usr/local/nexus/ 113chown-R nexus/ usr/local/nexus/ 114cd / usr/local/nexus/ 115lsnexus-3.17.0-01 / for the application directory sonatype-work/ is the working directory Store the image repository running 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/nexus-default.propertiesNexus default port is 8081, this version cannot be started by root users Would you like to report the following error 117 ln-s / usr/local/nexus/nexus-3.17.0-01/bin/nexus / usr/local/bin/ 118 nexus start

[root@localhost nexus] # su nexus [nexus@localhost nexus] $nexus startStarting nexus [nexus@localhost nexus] $netstat-anpt | grep 8081 (No info could be read for "- p": geteuid () = 1001 but you should be root.) tcp 00 0.0.0.0nexus@localhost nexus 8081 0.0.0.0nexus@localhost nexus * LISTEN-

Browsers access http://192.168.1.40:8081

Cat / usr/local/nexus/sonatype-work/nexus3/admin.password View password copy to password box

And then take the next step all the way.

Create roles and users

Default warehouse type:

Group (Warehouse Group Type): also known as Group Warehouse, which is used to facilitate developers and set up their own warehouse.

Hosted (host type): release repository for internal projects (repositories released by internal developers)

Proxy (agent type): a warehouse that looks for data from a remote central warehouse (you can click on the Configuration page of the corresponding warehouse to sign the value of the Remote Storage Location attribute, that is, the path of the remote warehouse being proxied)

Virtual (virtual type): virtual warehouse (this is basically not needed)

Warehouse types under Public Repositories

3rd party: a component repository of a third-party release version that is not available from a public warehouse, that is, a repository that the third party depends on. This data is usually downloaded and released by insiders.

Apache Snapshots: a component repository that uses the snapshot version of the proxy ApacheMaven repository

Central: the repository used to represent the release artifacts in the maven central repository

Entral M1 shadow: a component image repository for providing released versions in M1 format in a central repository

Codehaus Snapshots: a repository of snapshot version artifacts used to proxy CodehausMaven repositories

Releases: the release repository of the release module in the internal module, which is used to deploy and manage the host type repository of the internal release artifacts; release is the release version

Snapshots: a repository for publishing internal SNAPSHOT modules to deploy and manage internal snapshot version artifacts; snapshots is a snapshot version, that is, an unstable version

Enable the repeat distribution permission of release

If you need to release the version repeatedly in the development, you need to open the corresponding permission setting allow redeploy of the release type repository.

Set up agent warehouse (Aliyun maven warehouse)

Click the create repository at the bottom of the page to complete the creation, and you can see that Ali Cloud Library has been added.

Add the added Ali Cloud proxy to the default group

Click the save at the bottom of the page and click in again to see the current nexus address.

Common usage scenarios

Key points:

Release distribution warehouse (nexus has been established by default: maven-releasees)

Snapshot Test Center Snapshot Warehouse (nexus established by default: maven-snapshots)

Central Central Warehouse (nexus established by default: maven-central)

About groups: groups can be established in nexus to bring different types of repositories together (nexus has been established by default: maven-public)

Scenario 1: customize the new proxy type repository, configure different call addresses in the maven configuration, or join the custom newly created proxy repository into a group, and call an address in the maven configuration.

Scenario 2: use the established proxy repository of maven and configure the maven call address with the established group (maven-public)

Scenario 1 and scenario 2 are essentially unchanged, and can be configured according to the company's development habits and requirements.

Mavne configures nexus private server to set up nuxus private server at cost in remote warehouse.

Go back to the configuration file path of maven to modify the main configuration file

123 cd / usr/local/maven/conf/ 125 vim settings.xml nexus-bdqnconf * nexus bdqnconf http://192.168.1.40:8081/repository/maven-public/ # 171! [] (https://s1.51cto.com/images/blog/201911/19/1a7ae62c2562ac9aa968d4b9b5d003dd.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90, Type_ZmFuZ3poZW5naGVpdGk=) # 289 bdqnconf nexus http://192.168.1.40:8081/repository/maven-public/ true true nexus http:/ / 192.168.1.40:8081/repository/maven-public/ true true activate the profile above The content must be id # 299 bdqnconf of profile above.

Create a maven project for testing, first delete the cache of the local repository

127cd / xgp/ 128rm-rf / root/.m2/repository/* 129mvn archetype:generate-DgroupId=cn.bdqn.bdqnweb-DartifactId=bdqnweb-DarchetypeArtifactId=maven-archetype-webapp-DinteractiveMode=false

Enter the existing bdqnweb clear cache first

142 cd / xgp/bdqnweb/ 144 mvn clean

Package the project and go back to nexus to see if there are cache dependent packages

147 mvn package

Configure authentication to upload the packaged project to nexus, but not to configure authentication

[root@localhost bdqnweb] # vim / usr/local/maven/conf/settings.xml # 133 test-nexus xgpdev 1234.com

[root@localhost bdqnweb] # vim / xgp/bdqnweb/pom.xml # 23 test-nexus (the id here needs to be the same as the id in the setting configuration file) Nexus Snapshot http://192.168.1.40:8081/repository/maven-snapshots/ test-nexus Nexus releases http://192. 168.83.3:8081/repository/maven-releases/

Go back to nexus to see if the upload is successful.

This is the beta version with a timestamp.

# if the following error occurs during deployment, this error is due to permission issues. First, check whether the account password is written correctly when configuring settings.xml. Second, check whether the id tag in settings.xml is the same as the id tag in pom.xml file. If the difference is not the same, you will report an error of 401.

Upload to maven-releases Warehouse

If you want to pack release, you can remove the SNAPSHOT logo from the pom file.

Change this line of 1.0-SNAPSHOT to 1.0.

[root@localhost bdqnweb] # vim / xgp/bdqnweb/pom.xml

1.0 # 7

Upload the maven package again, and you can see it in the terminal and upload it to the releases warehouse.

[root@localhost bdqnweb] # mvn deploy

Upload the third party maven package to the nexus private server

Maven add third-party warehouse address (global configuration)

[root@localhost ~] # vim / usr/local/maven/conf/settings.xml # 294 add 3rdnexus http://192.168.1.40:8081/repository/3rd/ # (just copied) true true add a 3rd Certification # 138 add 3rdnexus xgpdev 1234.com

[root@localhost target] # mv / usr/src/macven+nexus/aliyun-sdk-oss-2.6.1.jar.

[root@localhost target] # mvn deploy:deploy-file-DgroupId=com.aliyun.oss-DartifactId=aliyun-sdk-oss-Dversion=2.6.1-Dpackaging=jar-Dfile=/xgp/bdqnweb/target/aliyun-sdk-oss-2.6.1.jar-Durl= http://192.168.1.40:8081/repository/3rd/-DrepositoryId=3rdnexus

Browser view

The experiment is over

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report