Nexus (maven warehouse) builds and configures maven local private warehouse

The role of a private server, why use a private server?

For intranet access, the intranet team uses a service cache to save external network bandwidth.
Accelerates the construction of Maven projects in microservice development, speeds up teamwork, and improves work efficiency.
It allows uploading and downloading of private libraries, and is protected from external access, which is safe and stable.
Facilitate dependent references of internal project services without requiring the full source code of other projects.

1.1 Nexus download

Quickly build maven private server

Official website address: Download Repository OSS   but the speed is extremely slow;

It is recommended to use Baidu cloud disk

The link shared by the editor: https://pan.baidu.com/s/1Wgcv_A6YzWuC1NUAhshUcg   Extraction code: arei

1.2 Build and configure nexus service

 1. Unzip the downloaded nexus-3.31.1-01-win64.zip compressed package and there will be two folders after decompression

2. Go to the folder nexus-3.31.1-01\bin directory

3. Open the command window and execute nexus.exe /install nexus to install it into the Windows service

Because I have already installed the service, these two lines will appear when I execute nexus.exe /install nexus again

Service is already stopped.
Installed service 'nexus'.

4. Use nexus.exe /start or /stop to turn on and off the service 

After the service starts, the default port is 8081 Access address: http://localhost:8081/#browse/welcome

 Here I changed the port to 8088 because of the port conflict, the following will be 8088

To modify the port, you can edit the default configuration file of nexus-3.31.1-01\etc\nexus-default.properties

 5. Login Click sign in in the upper right corner to log in. The default user name is admin

 The first login password will be randomly generated in a password file in the \nexus\sonatype-work\nexus3 directory

Log in to change the password 

Two use private server

2.1 Private server introduction

hosted, local storage. Provides the local private library function
proxy like the official warehouse, and provides the type of proxy for other warehouses,
group type, which can combine multiple warehouses to provide services for one address.
maven-central: maven central library, defaults from https://repo1.maven. org/maven2/pull jar
maven-releases: private library package release version jar
maven-snapshots: private library snapshot (debug version) jar
maven-public: warehouse grouping, combine the above three warehouses together to provide external services, locally Used in maven basic configuration settings.xml.
Hosted has three methods: Releases, Snapshot, Mixed
Releases: generally released Jar package
Snapshot: unreleased version
Mixed: mixed

2.2 After logging in, a setting icon will appear on the left, click to configure the warehouse

Click Create repository to create a proxy warehouse

 Configure Alibaba Cloud Mirror Agent

 For details on Alibaba Cloud's mirrored warehouse services, see: Warehouse Services

Create a hosted local repository to store the release version jar

 

 Finally double-click maven-public in the list 

Set Alibaba Cloud mirror to be used first

Three use private servers in Maven

1. Install and configure maven 

Official website download: Maven – Download Apache Maven

 

 2. Unzip the apache-maven-3.8.5-bin.zip file to the specified directory. I unzip it to the path of D:\Program Files

Configure environment variables

 Check maven configuration  

3. Set maven to configure the settings.xml file under conf

<?xml version="1.0" encoding="UTF-8"?>

<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd">

    <!-- 构建系统本地仓库的路径 -->
  <localRepository>D://repo</localRepository>

  <pluginGroups>
    <!-- pluginGroup
     | Specifies a further group identifier to use for plugin lookup.
    <pluginGroup>com.your.plugins</pluginGroup>
    -->
  </pluginGroups>

  <!-- proxies
   | This is a list of proxies which can be used on this machine to connect to the network.
   | Unless otherwise specified (by system property or command-line switch), the first proxy
   | specification in this list marked as active will be used.
   |-->
  <proxies>
 
  </proxies>

  <!-- servers
   | This is a list of authentication profiles, keyed by the server-id used within the system.
   | Authentication profiles can be used whenever maven must make a connection to a remote server.
   |-->
  <servers>
    <!-- 配置本地仓库访问私服的权限  nexus的 登录用户名密码 -->
    <server>
        <id>maven-releases</id>
        <username>admin</username>
        <password>admin</password>
    </server>
    <server>
        <id>maven-snapshots</id>
        <username>admin</username>
        <password>admin</password>
    </server>
  
  </servers>

  <mirrors>
   
     <!-- 配置本地仓库资源来源 -->
    <mirror>
        <id>maven-public</id>
        <mirrorOf>*</mirrorOf>
        <url>http://localhost:8088/repository/maven-public/</url>
    </mirror>
  </mirrors>

  <profiles>

      <!-- 属性列表配置 -->
      <profile>
        <id>my-profile</id>
        <properties>
          <maven.compiler.source>1.8</maven.compiler.source>  
          <maven.compiler.target>1.8</maven.compiler.target>  
          <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
        </properties>
        <!-- 远程仓库列表 maven用来填充构建系统本地仓库所使用的一组远程仓库 -->
        <repositories>
          <repository>
            <id>maven-central</id>
            <url>http://localhost:8088/repository/maven-central/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
          </repository>

          <repository>
            <id>maven-releases</id>
            <url>http://localhost:8088/repository/maven-releases/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
          </repository>

          <repository>
            <id>maven-snapshots</id>
            <url>http://localhost:8088/repository/maven-snapshots/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
          </repository>
      </repositories>

    <pluginRepositories>
      <pluginRepository>
        <id>maven-public</id>
        <url>http://localhost:8088/repository/maven-public</url>
      </pluginRepository>
    </pluginRepositories>
    
    </profile>
  </profiles>
  
  <activeProfiles>
   <activeProfile>my-profile</activeProfile>     
  </activeProfiles>

</settings>

 4. Configuration is required for project deployment and release: Add distributionManagement configuration in pom.xml

<!--  maven仓库配置 deploy时可推送到配置的仓库中	-->
	<distributionManagement>
		<repository>
			<id>maven-releases</id>
			<name>Nexus Releases Repository Pro</name>
			<url>http://localhost:8088/repository/maven-releases/</url>
		</repository>

		<snapshotRepository>
			<id>maven-snapshots</id>
			<name>Nexus Snapshots Repository Pro</name>
			<url>http://localhost:8088/repository/maven-snapshots/</url>
		</snapshotRepository>
	</distributionManagement>

If you use pom.xml to download dependency configuration in the project, you can use:

If the maven used in the project has been configured in settings.xml, there is no need to configure this item

    <repositories>
		<repository>
			<id>repository</id>
			<url>http://localhost:8088/repository/maven-public/</url>
			<releases>
				<enabled>true</enabled>
			</releases>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>
	<pluginRepositories>
		<pluginRepository>
			<id>central</id>
			<url>http://localhost:8088/repository/maven-central/</url>
			<releases>
				<enabled>true</enabled>
			</releases>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</pluginRepository>
	</pluginRepositories>

 5. It can be packaged, deployed and released directly in the project 

   Maven command introduction:

The package command completes project compilation, unit testing, and packaging functions, but it does not deploy the executable jar package (war package or other form of package) to the local maven warehouse and remote maven private server warehouse


The install command completes project compilation, unit testing, and packaging functions, and at the same time deploys the executable jar package (war package or other form of package) to the local maven warehouse 3, but does not deploy to the remote maven private server warehouse


The deploy command completes project compilation, unit testing, and packaging functions, and at the same time deploys the executable jar package (war package or other form of package) to the local maven warehouse and remote maven private server warehouse

If the maven configured by yourself has been unable to be compiled by idea, you can put the settings.xml configuration file directly under the default maven configuration file of idea for replacement

 
 

 

6. Test deploy in idea to package and deploy your project:

If a MojoExecutionException error occurs 

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project demo: Deployment failed: repository element was not specified in the POM inside distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
[ERROR] 
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <args> -rf :demo
 

Please carefully check whether the distributionManagement configuration in your pom.xml is correct.

7. Pack again and all appear SUCCESS, indicating that they have been successfully deployed to the private server warehouse

Log in to your nexus private server to view the deployed jar

All in Congratulations on your successful Congratulations  

Guess you like

Origin blog.csdn.net/weixin_42599091/article/details/124981288