Maven repository

5        Maven repository introduction

5.1 Introduction     

Maven repositories are used to store artifacts. Maven repositories mainly include local repositories and remote repositories. When Maven needs an artifact, it will first fetch it from the local warehouse. Only when the corresponding artifact does not exist in the local warehouse will it download the corresponding artifact from the remote warehouse and save it in the local warehouse, and obtain all the artifacts from the local warehouse. required workpiece. The default local repository location is " .m2/repository " under the user's home directory. We can define the location of the local repository through localRepository in the settings.xml file under the Maven installation directory . The default remote repository is " http://repo.maven.apache.org/maven2/ ".

Here I would like to state a question. Maven officials do not advocate that when we use Maven , we also submit some jar packages that the project depends on to svn and other version control systems for version control. This is mainly for the following reasons:

1) We have used Maven to manage our dependent jar packages, then these jar packages will be stored in the local warehouse, we do not need to save a copy of the dependent jar package for each project, which will waste a lot of disk space, It also goes against the original intention of Maven 's design.      

2) When we do not submit the dependent jar package to the version control system, it means that the capacity of our project will be relatively small, which brings us the advantage that when we check out the project time will be relatively faster.      

3)       Jar packages generally do not need to be version controlled, because they generally do not change much, and we rarely change a jar package.

Sometimes due to security or speed reasons, we are not allowed to download dependent jar packages directly from the Internet. At this time, the internal warehouse comes out. We can download artifacts from this internal repository, and we can also publish artifacts to this repository. The concept of this internal warehouse is equivalent to that the company manages a set of artifact libraries by itself, and can freely publish the company's own artifacts in this artifact library for everyone to share.

5.2 Install the third-party jar package to the local warehouse     

Sometimes we may need to install the local third-party jar package to our Maven local repository to build our Maven project. Maven provides us with an instruction that can easily help us achieve this function.

Cmd code   Favorite code
  1. mvn install:install-file -Dfile=<filePath> -DgroupId=<groupId> -DartifactId=<artifactId> -Dversion=<version> -Dpackaging=<packagingType>  

 

       The parameter file indicates the local path of the third-party jar package to be installed;

       The parameter groupId is used to define the groupId after the jar package is installed ;

       The parameter artifactId is used to define the artifactId after the jar package is installed ;

       The parameter version is used to define the version of the jar package after installation;

       The parameter packaging is used to define the packaging type after the jar package is installed.

       For example, now I want to install " D:\develop\lib\mysql-connector-java-5.1.12-bin.jar " on my computer to my Maven local repository, then I can run the following command in the command window to To achieve this:

       mvn install:install-file -Dfile=D:\develop\lib\mysql-connector-5.1.12-bin.jar -DgroupId=mysql -DartifactId=mysql -Dversion=5.1.12 -Dpackaging=jar

       After that, in other Maven projects, we can add the reference of mysql-connector-5.1.12-bin.jar defined here according to the defined groupId , artifactId , version and packaging type .

5.3 Deploying third-party jar packages to remote warehouses     

The third-party jar package installed in the local warehouse can only be used locally, so that others cannot access it. If it needs to be accessible to others, we need to deploy it to our remote repository. We can use the following Maven command to deploy a third-party jar package to the specified remote repository.

Cmd code   Favorite code
  1. mvn deploy:deploy-file  
  2.  -Dfile=<filePath>  
  3.  -DgroupId=<groupId>  
  4.  -DartifactId=<artifactId>  
  5.  -Dversion=<version>  
  6.  -Dpackaging=<packagingType>  
  7.  -DrepositoryId=<repositoryId>  
  8.  -Durl=<urlOfTheRepositoryToDeploy>  

 

The parameters for deploying a third-party jar package to a remote repository are similar to those for installing a third-party jar package to a local repository, but it has two more parameters, one is repositoryId and url . repositoryId represents the id of the remote repository to be deployed to, which is defined in settings.xml ; url represents the url of the remote repository to be deployed to .

By default, third-party jars deployed with deploy:deploy-file will generate a generic pom . If this pom does not need to be generated during deployment , we can add the parameter " -DgeneratePom=false " when using this directive .

If we need to use an existing pom when deploying a third-party jar to a remote warehouse using deploy:deploy-file , we need to add the parameter " -DpomFile=<pomFilePath> " when using this command . Such as:

Cmd code   Favorite code
  1. mvn deploy:deploy-file  
  2.  -Dfile=<filePath>  
  3.  -DpomFile=<pomFilePath>  
  4.  -DrepositoryId=<repositoryId>  
  5.  -Durl=<urlOfTheRepositoryToDeploy>  

 

Attentive readers may have discovered that we did not specify the groupId , artifactId , version and packaging parameters when using the parameter pomFile . This is because when we specify pomFile , these parameters can be obtained from the specified pom file.

 

When we need to deploy a source jar package, packaging should be specified as java-source , and generatePom should be specified as false .

 

Guess you like

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