Use nexus to create maven private server

Nexus is a powerful Maven repository manager that greatly simplifies maintaining your own internal repositories and accessing external repositories. With Nexus you have full control over access and deployment of every Artifact in the repository you maintain in just one place. Nexus is an "out of the box" system that does not require a database, it uses a file system plus Lucene to organize data. Nexus uses ExtJS to develop the interface, uses Restlet to provide complete REST APIs, and integrates with Eclipse through m2eclipse. Nexus supports WebDAV and LDAP secure authentication.

Install Nexus

 

download

There are two kinds of installation packages for Nexus, one is the Bundle package that contains the Jetty container, and the other is the war package that does not contain the Web container. We can download the latest version of the Nexus Bundle from http://www.sonatype.org/downloads/nexus-latest-bundle.zip.

 

Unzip nexus-latest-bundle.zip, you will find two folders. Figure 1:

 

figure 1

The nexus-2.7.2-03 directory contains the files needed to run Nexus. It is necessary for Nexus to run.

 

The sonatype-work directory contains configuration files, log files, repository files, etc. generated by Nexus. When we need to back up Nexus, this directory can be backed up by default.

Install

Enter the directory nexus-2.7.2-03\bin\jsw, you will find that there are many installation environments. My operating system is 64-bit Windows 8, so go to the windows-x86-64 directory. There are the following files, as shown in Figure 2:

 

 

                                               figure 2

 

install-nexus.bat: Install the Nexus service to the Windows system.

 

start-nexus.bat: Start the Nexus service.

 

stop-nexus.bat: Stop the Nexus service.

 

uninstall-nexus.bat: Uninstall Nexus services.

 

Right-click install-nexus.bat "run as administrator" to run the file, right-click start-nexus.bat "run as administrator" to run the file. In "Services", we can see that Nexus is installed and running, Figure 3:

 

 

                                               image 3

 

Before installation, install JDK and configure JAVA_HOME and PATH in environment variables.

Log in

 

Enter in the browser: http://localhost:8081/nexus/ to get the following interface, as shown in Figure 4:

 

 

                                               Figure 4

 

Click the "Log In" button in the upper right corner to log in. The default administrator account password of Nexus is admin/admin123.

Configure Nexus

 

Nexus warehouse classification

 

Nexus contains various types of repository types. On the Nexus interface after logging in, click the "Repositories" link on the left, and you can see Figure 5:

 

 

                                               Figure 5

 

We can see four warehouse types from the above figure: group (repository group), hosted (host), proxy (proxy) and virtual (virtual). Each type of Format has Maven1 or Maven2, here we do not look at Maven1. The Policy of a repository indicates whether the repository is a Release version or a Snapshot version repository.

 

In Figure 5, Nexus lists several default repositories:

 

Public Repositories: Repository group that aggregates all repositories whose policies are Release and provides services through a consistent address.

 

3rd party: A host-type repository with a policy of Release, used to deploy third-party release version artifacts that are not available from public repositories.

 

Apache Snapshots: The proxy repository with the policy of Snapshots is used to proxy the snapshot version artifacts of the Apache Maven repository.

 

Central: This repository acts as a proxy for Maven's central repository. The policy is Release, and only the release version artifacts in the central repository are downloaded and cached.

 

Central M1 shadow: A virtual type repository in maven1 format.

 

Codehaus Snapshots: Proxy repositories that proxy snapshot versions of Codehaus Maven repositories.

 

Release: The host type repository whose policy is Release is used to deploy the release version components within the organization.

 

Snapshots: The policy is the host type repository of Snapshots, which is used to deploy snapshot version components within the organization.

 

Use a picture in "Maven Combat" to explain the classification of warehouses:

 

 

                                          Image 6

 

Private repository configuration

 

Indexing and Component Search for Nexus

 

Click the "Public Repositories" row on the list in Figure 5, and in the "Configuration" below, we can see that the "Ordered Group Repositories" contains the Release, Snapshots, 3rd party, Central and other repositories.

 

In order to build the Maven central library index of Nexus, you first need to set the Maven Cencal proxy repository in Nexus to download the remote index, and change the value of "Download Remote Indexes" from the default value of false to true. Figure 7:

 

 

                              Figure 7

 

After clicking "Save", click the "Scheduled Tasks" link on the left, and the interface shown in Figure 8 will appear, indicating that the Nexus background is downloading the index of the Maven central warehouse.

 

 

                                                   Figure 8

 

If the interface shown in Figure 8 does not appear, right-click on the "Public Repositories" row and click "Update Index".

 

 

                             Figure 9

 

In the "Repositories" interface, select the Browse Index tab, and you can see the tree structure of the contents of the Maven central repository, as shown in Figure 10.

 

 

                                                   Figure 10

 

If the build is still unsuccessful, refer to: http://blog.csdn.net/mtkong/article/details/9377605

 

http://397013586.iteye.com/blog/1558492

 

Enter the "spring" keyword in the search box on the left, and a bunch of spring-related results will appear.

 

If you want to index the host repository and proxy repository. Just right-click on the corresponding repository and select "ReIndex" from the pop-up shortcut menu.

Configure Maven to download artifacts from Nexus

 

On the basis of the above, we can use Nexus private server. There are 2 ways:

 

1. Configure in the project's POM .

 

<repositories>
	<repository>
		<id>local_nexus</id>
		<name>local_nexus</name>
		<url>http://localhost:8081/nexus/content/groups/public/</url>
		<releases>
			<enabled>true</enabled>
		</releases>
		<snapshots>
			<enabled>true</enabled>
		</snapshots>
	</repository>
</repositories>
<pluginRepositories>
	<pluginRepository>
		<id>local_nexus</id>
		<name>local_nexus</name>
		<url>http://localhost:8081/nexus/content/groups/public/</url>
		<releases>
			<enabled>true</enabled>
		</releases>
		<snapshots>
			<enabled>true</enabled>
		</snapshots>
	</pluginRepository>
</pluginRepositories>

 

 

Such a configuration is only valid for the current Maven project. However, in the actual project development, we all think that all Maven projects on the machine can use Maven private service in one configuration, so we need to use the following method.

 

2. Configure the remote warehouse in settings.xml.

 

A profile provided by Maven is an optional set of configurations that can be used to set or override configuration defaults. With profiles, you can customize builds for different environments. Then add the following code to settings.xml:

 

<settings>
  ...
	<profiles>
		<profile>
			<id>local_nexus</id>
			<repositories>
				<repository>
					<id>local_nexus</id>
					<name>local_nexus</name>
					<url>http://localhost:8081/nexus/content/groups/public/</url>
					<releases>
						<enabled>true</enabled>
					</releases>
					<snapshots>
						<enabled>true</enabled>
					</snapshots>
				</repository>
			</repositories>
			<pluginRepositories>
				<pluginRepository>
					<id>local_nexus</id>
					<name>local_nexus</name>
					<url>http://localhost:8081/nexus/content/groups/public/</url>
					<releases>
						<enabled>true</enabled>
					</releases>
					<snapshots>
						<enabled>true</enabled>
					</snapshots>
				</pluginRepository>
			</pluginRepositories>
		</profile>
	</profiles>
	<activeProfiles>
		<activeProfile>local_nexus</activeProfile>
	</activeProfiles>
  ...
</settings>

 

 

In the above configuration, a profile with an id of local_nexus is used . This profile contains the relevant warehouse configuration. At the same time, the activeProfiles element is used in the configuration to activate the nexus profile , so that when the Maven build is executed, the activated profile will The repository configuration is applied to the project.

 

Through the above configuration, we will find that Maven will download components from the central repository in addition to downloading components from Nexus . Since it is a private server, then we only hope that Maven download requests are only through Nexus . We can achieve this by mirroring. You can create an image that matches any repository, and the address of the image is the private server, so that Maven 's component download requests for any repository will be transferred to the private server. Modify the above configuration to the following configuration:

<settings>
  ...
	<mirrors>
		<mirror>
			<id>local_mirror</id>
			<mirrorOf>*</mirrorOf>
			<name>local_mirror</name>
			<url>http://localhost:8081/nexus/content/groups/public/</url>
		</mirror>
	</mirrors>
	 
	<profiles>
		<profile>
			<id>local_nexus</id>
			<repositories>
				<repository>
					<id>local_nexus</id>
					<name>local_nexus</name>
					<url>http://localhost:8081/nexus/content/groups/public/</url>
					<releases>
						<enabled>true</enabled>
					</releases>
					<snapshots>
						<enabled>true</enabled>
					</snapshots>
				</repository>
				<repository>
					<id>central</id>
					<!--This url doesn't work because the mirror is configured-->
					<url>http://repo.maven.apache.org/maven2</url>
					<releases>
						<enabled>true</enabled>
					</releases>
					<snapshots>
						<enabled>true</enabled>
					</snapshots>
				</repository>
			</repositories>
			<pluginRepositories>
				<pluginRepository>
					<id>local_nexus</id>
					<name>local_nexus</name>
					<url>http://localhost:8081/nexus/content/groups/public/</url>
					<releases>
						<enabled>true</enabled>
					</releases>
					<snapshots>
						<enabled>true</enabled>
					</snapshots>
				</pluginRepository>
				<pluginRepository>
					<id>central</id>
					<url>http://repo.maven.apache.org/maven2</url>
					<releases>
						<enabled>true</enabled>
					</releases>
					<snapshots>
						<enabled>true</enabled>
					</snapshots>
				</pluginRepository>
			</pluginRepositories>
		</profile>
	</profiles>
	<activeProfiles>
		<activeProfile>local_nexus</activeProfile>
	</activeProfiles>
  ...
</settings

Deploy components to private server 

We have multiple people in the actual development process, so there are always some common modules or third-party components that cannot be downloaded from the Maven central repository. We need to deploy these artifacts to private servers for other developers to download. Users can configure Maven to automatically deploy artifacts to the Nexus host repository, or manually upload artifacts through the interface.

 

Use Maven to deploy artifacts to Nexus private server

The snapshot version of daily development is deployed to the host warehouse with the strategy of Snapshot in Nexus , and the official project is deployed to the host warehouse with the strategy of Release . The configuration of the POM is as follows:

...
<distributionManagement>
	<repository>
		<id>local_nexus_releases</id>
		<name>core Release Repository</name>
		<url>http://localhost:8081/nexus/content/repositories/releases/</url>
	</repository>
	<snapshotRepository>
		<id>local_nexus_snapshots</id>
		<name>core Snapshots Repository</name>
		<url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
	</snapshotRepository>
</distributionManagement>
...

Nexus' repositories are read-only for anonymous users. In order to be able to deploy the component, we also need to configure the authentication information in settings.xml :

...
<servers>
	<server>
		<id>local_nexus_releases</id>
		<username>admin</username>
		<password>admin123</password>
	</server>
	<server>
		<id>local_nexus_snapshots</id>
		<username>admin</username>
		<password>admin123</password>
	</server>
</servers>
  ...

Among them, the id of the service in the verification information should be consistent with the id of the repository in the POM .

 

Manually deploy third-party components to private servers on the Nexus interface

In addition to deploying our own components to the Nexus private server, we may also deploy third-party components (such as SQLService 's JDBC ) to Nexus . At this time, select a host repository (such as 3rd party ) on the Nexus interface, and then select the Artifact Upload tab at the bottom of the page . Fill in the corresponding Maven coordinates. Then click the " Select Artifact(s) for Upload " button to select the artifacts to be uploaded from the computer, and then click the " Add Artifact " button to add it to the upload list. Finally, click the " Upload Artifact(s) " button at the bottom of the page to upload the artifact to the repository.

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326772310&siteId=291194637