maven本地仓库--各种好玩的配置

目录

前提

1.自定义配置本地仓库目录

2.设置maven依赖的jdk版本

3.使用指定的阿里云maven中央仓库

4.配置私有仓库(假如所有的maven项目都使用私服,例如nexus3)

5.使用本地仓库的jar包,禁止从远端(有可能是外网的中央仓库,有可能是私服nexus3)下载

6.将本地jar包导入本地仓库

7.将本地jar包导入私服nexus的maven库中

8.将本地项目发布到Nexus私服


前提

下载并解压maven客户端apache-maven-3.6.3-bin.zip

1.自定义配置本地仓库目录

打开apache-maven-3.6.3\conf\settings.xml文件,查找内容如下:

<!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->

去掉注释,并修改成:

<localRepository>D:/maven/local_repos</localRepository>

2.设置maven依赖的jdk版本

打开apache-maven-3.6.3\conf\settings.xml文件,查找内容如下:

<!-- 
<profile>
  <id>jdk-1.4</id>

  <activation>
	<jdk>1.4</jdk>
  </activation>

  <repositories>
	<repository>
	  <id>jdk14</id>
	  <name>Repository for JDK 1.4 builds</name>
	  <url>http://www.myhost.com/maven/jdk14</url>
	  <layout>default</layout>
	  <snapshotPolicy>always</snapshotPolicy>
	</repository>
  </repositories>
</profile>
-->

去掉注释,并修改成:

<profile>
	<id>jdk-1.8</id>    
	<activation>    
		<activeByDefault>true</activeByDefault>    
		<jdk>1.8</jdk>    
	</activation>
	<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>
</profile>

3.使用指定的阿里云maven中央仓库

打开apache-maven-3.6.3\conf\settings.xml文件,查找内容如下:

<!-- mirror
 | Specifies a repository mirror site to use instead of a given repository. The repository that
 | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
 | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
 |
<mirror>
  <id>mirrorId</id>
  <mirrorOf>repositoryId</mirrorOf>
  <name>Human Readable Name for this Mirror.</name>
  <url>http://my.repository.com/repo/path</url>
</mirror>
 -->

去掉注释,并修改成:

<mirror>
  <id>alimven</id>
  <mirrorOf>central</mirrorOf>
  <name>aliyun maven</name>
  <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>

4.配置私有仓库(假如所有的maven项目都使用私服,例如nexus3)

打开apache-maven-3.6.3\conf\settings.xml文件,

1.查找内容如下:

<!-- mirror
 | Specifies a repository mirror site to use instead of a given repository. The repository that
 | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
 | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
 |
<mirror>
  <id>mirrorId</id>
  <mirrorOf>repositoryId</mirrorOf>
  <name>Human Readable Name for this Mirror.</name>
  <url>http://my.repository.com/repo/path</url>
</mirror>
 -->

去掉注释,并修改成:

<mirror>
  <id>nexus_release</id>
  <name>nexus maven</name>
  <url>http://ip地址:8081/repository/子目录/</url>
  <mirrorOf>*</mirrorOf>        
</mirror>

2.然后,查找内容如下:

<!-- 
<server>
  <id>siteServer</id>
  <privateKey>/path/to/private/key</privateKey>
  <passphrase>optional; leave empty if not used.</passphrase>
</server>
-->

去掉注释,并修改成:

<server>
  <id>nexus_release</id>
  <username>登录私服的用户名</username>
  <password>登录私服的密码</password>
</server>

3.然后,查找内容如下:

<!--
     | Here is another profile, activated by the system property 'target-env' with a value of 'dev',
     | which provides a specific path to the Tomcat instance. To use this, your plugin configuration
     | might hypothetically look like:
     |
     | ...
     | <plugin>
     |   <groupId>org.myco.myplugins</groupId>
     |   <artifactId>myplugin</artifactId>
     |
     |   <configuration>
     |     <tomcatLocation>${tomcatPath}</tomcatLocation>
     |   </configuration>
     | </plugin>
     | ...
     |
     | NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to
     |       anything, you could just leave off the <value/> inside the activation-property.
     |
    <profile>
      <id>env-dev</id>

      <activation>
        <property>
          <name>target-env</name>
          <value>dev</value>
        </property>
      </activation>

      <properties>
        <tomcatPath>/path/to/tomcat/instance</tomcatPath>
      </properties>
    </profile>
    -->

去掉注释,并修改成:

<profile>
   <id>nexus_profile</id>
   <repositories>
	 <repository>
	     <id>nexus</id>
	      <url>http://私服ip地址:8081/repository/子目录/</url>
	      <releases>
                <enabled>true</enabled>
          </releases>
	      <snapshots>
              <enabled>true</enabled>
          </snapshots>
	 </repository>
   </repositories>
   <pluginRepositories>
       <pluginRepository>
	       <id>nexus</id>
	       <url>http://私服ip地址:8081/repository/子目录/</url>
	       <releases>
		       <enabled>true</enabled>
	       </releases>
	       <snapshots>
		       <enabled>true</enabled>
	       </snapshots>
	   </pluginRepository>
    </pluginRepositories>
</profile>

4.最后,查找内容如下:

<!-- activeProfiles
   | List of profiles that are active for all builds.
   |
  <activeProfiles>
    <activeProfile>alwaysActiveProfile</activeProfile>
    <activeProfile>anotherAlwaysActiveProfile</activeProfile>
  </activeProfiles>
  -->

去掉注释,并修改成:

<activeProfiles>
    <activeProfile>nexus_profile</activeProfile>
</activeProfiles>

5.使用本地仓库的jar包,禁止从远端(有可能是外网的中央仓库,有可能是私服nexus3)下载

打开apache-maven-3.6.3\conf\settings.xml文件,查找内容如下:

<!-- offline
   | Determines whether maven should attempt to connect to the network when executing a build.
   | This will have an effect on artifact downloads, artifact deployment, and others.
   |
   | Default: false
  <offline>false</offline>
  -->

去掉注释,并修改成:

<offline>true</offline>

6.将本地jar包导入本地仓库

mvn install:install-file -DgroupId=jar包的GID -DartifactId=jar包的AID -Dversion=Jar包的版本 -Dpackaging=jar -Dfile=你本地jar包的目录

例子:
mvn install:install-file -DgroupId=org.springframework -DartifactId=spring-webmvc -Dversion=3.0.5.RELEASE -Dpackaging=jar -Dfile=D:/spring.jar

7.将本地jar包导入私服nexus的maven库中

举个例子,前提是记得检查一下maven的类型是hosted类型,如果是group类型,需要进一步确定包含的hosted类型的依赖库是哪个,其中-Durl的路径是hosted类型的依赖库

mvn deploy:deploy-file -DgroupId=com.shanhy -DartifactId=shanhy-web-core -Dversion=1.1.0-SNAPSHOT -Dpackaging=jar -Dfile=shanhy-web-core-5.1.17-SNAPSHOT.jar -Durl=http://admin:admin123@127.0.0.1:8081/repository/maven-snapshots/

还可以指定文件格式是.pom文件进行导入,如果这个jar本来依赖了其他的库(可以打开jar包在META-INF最里面可以找到pom.xml文件),仅使用上面的命令,资源库中对应jar包的pom文件不会出现依赖的jar包。

-DpomFile=shanhy-web-core-5.1.17-SNAPSHOT.pom

8.将本地项目发布到Nexus私服

1.查看当前正在使用的settings.xml

mvn help:effective-settings

2.pom.xml文件中加入如下配置

<!--使用分发上传将项目打成jar包,上传到nexus私服上-->
<distributionManagement>
	<!--发布版本仓库-->
	<repository>
		<!--nexus服务器中用户名:在settings.xml中和<server>的id一致-->
		<id>releases</id>
		<!--自定义名称-->
		<name>RELEASES PUBLISH</name>
		<!--仓库地址-->
		<url>http://xx.xx.xx.xx:xxxx/repository/maven-releases/</url>
	</repository>
	<!--快照版本仓库-->
	<snapshotRepository>
		<!--nexus服务器中用户名:在settings.xml中和<server>的id一致-->
		<id>snapshots</id>
		<!--自定义名称-->
		<name>SNAPSHOTS PUBLISH</name>
		<!--仓库地址-->
		<url>http://xx.xx.xx.xx:xxxx/repository/maven-snapshots/</url>
	</snapshotRepository>
</distributionManagement>

3.在settings.xml文件中加入如下配置

 <server>
    <id>releases</id>
    <username>私服的用户名</username>	
    <password>私服的密码</password>
 </server>
 <server>
    <id>snapshots</id>
    <username>私服的用户名</username>
    <password>私服的密码</password>
 </server>

4.将本地项目发布到Nexus私服

mvn clean deploy

5.发生如下错误可能是配置的账号信息有误的原因

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project demo-childA: Failed to deploy artifacts: Could not transfer artifact com.ecp:demo-childA:jar:1.0-20190625.082808-
1 from/to maven-snapshots (http://xx.xxx.xx.xx:xxxx/repository/maven-snapshots/): Failed to transfer file http://xx.xxx.xx.xx:xxxx/repository/maven-snapshots/com/ecp/demo-childA/1.0-SNAPSHOT/demo-childA-1.0-20190625.082808-1
.jar with status code 401 -> [Help 1]

可以使用:mvn help:effective-settings命令查看settings.xml配置文件查看配置信息

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

彼岸花@开

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值