Use git repository to build maven private server

The jar written by yourself must be published to the maven repository if you want to distribute it through maven. If we don't have our own private server, we can use a compromise method to achieve it.

When the maven client pulls the components from the maven warehouse, it actually pulls the corresponding warehouse address through the http protocol, the metadata file in the corresponding directory, and the jar file can be found and downloaded to the local warehouse by querying the required version. This completes the pulling of dependent components.

That is to say, we can use any http server to deploy components according to a certain directory structure, so as to use it as a private server.

Neither does the http server? Well, let me tell you quietly, almost all git services support the function of accessing specified files directly through http. (Click the Raw button in the web interface to jump to this address)

This article is based on the most popular (Sweet potato must be very happy to see it) git service - code cloud as an example to demonstrate how to create your own private server.

Create repository

First log in to the code cloud and create a project:

名称: mvnrepo
是否公开:公开

Fill in the rest as you like, and then click the "Create" button to complete the creation.

It must be public, otherwise maven can't use it as a repository.

Then your project path should be: https://gitee.com/{your domain name}/mvnrepo

For example, my warehouse address is: https://gitee.com/pollyduan/mvnrepo.git

Pull the repository to this machine

First, clarify the location where this git project is pulled to this machine, remember, it is useful. For example, I pull to /data/git/.

cd /data/git
git clone https://gitee.com/pollyduan/mvnrepo.git

At this point, the project directory is:/data/git/mvnrepo

Configure a local temporary repository

To modify the address of the local maven warehouse, you only need to modify the address of the local warehouse in settings.xml. Note that there are two configuration files to be changed, and maven will search once, and the last one shall prevail, so the last one should be modified.

$M2_HOME/conf/settings.xml
~/.m2/settings.xml

Change the warehouse address to:

<localRepository>/data/git/mvnrepo</localRepository>

Install the jar you want to publish to the local repository

If you want to install an existing jar directly:

mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=10.2.0.5 -Dpackaging=jar -Dfile=ojdbc14.jar

If you want to package and install the maven project:

mvn install

Note that at this time, the dependencies of the project and some dependencies of maven will also be placed in the current temporary warehouse.

Organize local temporary warehouse

If your directory is very clear, for example, the packages I want to publish are all in com.pollyduan, you don't need to clean up, but be careful not to submit them to the git server.

Otherwise, you need to manually delete all unnecessary packages.

After you understand here, submit a commit and push it to the remote warehouse.

cd /data/git/mvnrepo
echo '# 写点什么吧' >README.md
git add README.md
git add com/pollyduan
git commit -m '初始化仓库'
git push

Get private server address

Go back to the web site of the gitee repository, such as: https://gitee.com/pollyduan/mvnrepo

Click the README.mdfile , and then click the 原始数据button, and a new window will pop up to display the file.

Copy the address in your browser:

https://gitee.com/pollyduan/mvnrepo/raw/master/README.md

/README.mdRemove the in this address , it is your private server address:

https://gitee.com/pollyduan/mvnrepo/raw/master

handsome.

Use private server in maven repository

Since there are only the jars we want to publish in our private server, there are no other jars, so don't use this private server as a global mirror, remember to remember.

Add the following configuration to the next level of the project element in the pom file:

<repositories>
  <repository>
    <id>mvnrepo</id>
    <name>mvn repository</name>
    <url>https://gitee.com/pollyduan/mvnrepo/raw/master</url>
  </repository>
</repositories>

Now you can happily use private servers.

If you need to distribute the project to others, it can also easily get your dependencies.

Easter eggs

Code (ke) cloud (bie) no (dang) enemy (zhen).

Well, I have to say something long-winded: it is recommended settings.xmlto change the local warehouse address in . Otherwise, the management of this private server project may be a hassle in the future.

Don't really use the code cloud as a recycle bin.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324537194&siteId=291194637