I am a kite, the public number “ancient kite”, a both depth and breadth of programmers encourage division, a plan to write poetry but write up the code of rural code farmers! Articles will be included in JavaNewBee, and there will be a Java back-end knowledge map, which will cover the path from small white to big cow.
Almost every Java developer has used Maven at some point. If you haven’t, you’re probably using Gradle. If you haven’t used either, it’s a little unusual. By the way, in Gradle’s eyes, Maven is rubbish.
As a Java project management tool, Maven can not only be used as package management, but also a number of plug-ins, which can support the development, packaging, testing, deployment and a series of activities of the entire project.
Package management is the core function. Unless it’s a personal project, we usually get packages from a central repository or Github, but for corporate projects, we usually set up a private Maven server repository within the company or within the team to provide high speed.
Why build a private server
When we rely on a package in a POM file, maven searches the local repository first without special configuration (using maven’s default configuration). If the local repository does not have the package, Maven retrieves it from the central repository. The default address of the central warehouse is repo1.maven.org/maven2, the server is abroad, it can be imagined that the speed is not guaranteed. Sometimes, you pull down an open source project at night, execute Maven package installation, and wake up in the morning. You still haven’t downloaded it.
Of course, there are also corresponding domestic warehouses, such as Ali’s, the speed is still very good.
However, it is common for companies and even teams to set up a private Maven server to ensure stability, and also to put some of the company’s internal base packages or project pass kits on it to improve the flexibility of project development.
For example, the company has a low-level toolkit. If we get this package without private server, we need to run the following command to install it in the local warehouse:
mvn install:install-file
- Dfile = common - util - 0.0.1 - the SNAPSHOT. The jar-DgroupId=com.kite
-DartifactId=common-util
- Dversion = 0.0.1 - the SNAPSHOT-Dpackaging=jar Copy the code
Once the modification is made, the local command also needs to be re-executed, is not very troublesome.
Private server building process
Nexus is used to build Maven server and is arguably the best free tool available. Its website describes it as “the world’s first and only free repository solution.” The latest version is OSS 3.x. Windows, Unix, and OS X are available.
If the latest version is downloaded, it requires more than 4 GB memory and JDK version. The JDK version must be 1.8 at least.
- Download the package, unzip it, and run the command
tar -zxvf nexus-version-unix.tar.gz
Copy the code
-
Go to the nexus-version/bin/ directory and run the./nexus start command
-
Access the default port 8081
Yes, it’s that simple.
If you have a problem and “NOT RECOMMENDED TO RUN AS ROOT” is displayed, you can modify the /bin/nexus file, add RUN_AS_USER= ROOT, and restart.
In addition, if the docker environment, you can directly obtain the image of the Nexus to complete the installation. For example, I searched nexus on my MAC, downloaded it, and started it.
I met the Nexus
When we go to the nexus home page in a browser using the address, it looks something like this:
The default account name is admin and the password is admin123. The administrator can perform other operations, such as managing roles and users, on the Settings page. For example, create a role, give the role administrative rights for a warehouse, and then add people to the role. Specific operations do not do too much introduction, a look to understand.
Administrators can also manage warehouses, but generally the default ones are fine. By default, there are the following repositories:
What do several types mean
Proxy: This is the proxy mode, which is used to represent the central warehouse. For example, if the package we depend on is not available in the local warehouse, we will go to the private server to obtain it. If the private server is not available, we will go to the central warehouse to download the package here, and then download it to the local warehouse.
Hosted: refers to the internal packages in our company or team, and releases of releases (official) and snapshots (usually in beta) are broken down into hosted repositories.
Group: Its purpose is to combine multiple warehouses. Then, we only need to configure the address of this type of warehouse in the project to associate several warehouses combined in it.
For use in projects
Obtaining dependency packages
If you are a general project member, you do not need to publish packages to private servers. You only need to add the following configuration to the POM file.
<repositories>
<repository>
<id>maven-public</id>
<name>maven-public</name>
<url>http://nexus.local:32768/repository/maven-public/</url>
<snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> Copy the code
Where the ID must be the same as the name of your repository, the URL is the private server’s repository address, the one whose type is group, which combines a proxy and two Hosted repositories. Call it “snapshots” and set the enabled value of “snapshots” to true. Snapshots can be described as “snapshots” and “snapshots”.
Release (deploy) package
- If you need to send a package to the warehouse, you need to have permission for the warehouse first. Users without permission cannot publish the package.
Open Maven’s configuration file setting.xml, find the Servers node, and add a server node to it.
The user name and password can also be the administrator account, or the administrator’s newly created account, pharmaceutical can add permission to the warehouse.
<server>
<id>nexus-releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server> <id>nexus-snapshots</id> <username>admin</username> <password>admin123</password> </server> Copy the code
Note the above ID, which will be used later in the POM file.
- Then you need to configure the following in the POM file:
<distributionManagement>
<repository>
<id>nexus-releases</id>
<name>maven-releases</name>
<url>http://nexus.local:8081/repository/maven-releases/</url>
</repository> <snapshotRepository> <id>nexus-snapshots</id> <name>maven-snapshots</name> <url>http://nexus.local:8081/repository/maven-snapshots/</url> </snapshotRepository> </distributionManagement> Copy the code
We see that there is a Repository configured, and then there is a snapshotRepository, which represents the Releases version. Also note that the ID must be the same as the ID of the server in setting. XML, otherwise there will be permission problems. The URL specifies the addresses of Maven-releases and Maven-snapshots, respectively.
If you configure 1.0-snapshot in a project, snapshots with snapshots will be checked and releases releases will be released.
It is also important to note that the package you are publishing should not have parent dependencies. Otherwise there will be problems getting dependencies.
- Finally, run the MVN clean deploy command to publish the package to the private server repository.
After the above configuration and operations, you can complete the package publishing process.
The last
If your team uses its own private server repository internally, this is almost always the case. If there is any problem in the process of use, it can be suspected that the private server is not broken. For example, the Maven private server we used on our team failed.
Strong man wait, first give a praise bar, always white piao, the body can not bear!
I am kite, the official number “ancient kite”. A programmer with both depth and breadth of encouragement teacher, a intended to write poetry but write up the code of rural code farmers! You can follow me now, or you can follow history articles later. Reply “666” have a surprise