一.怎么配置点击deploy就能把jar包直接打到nexus私库上
方式一:在pom文件配置私库地址
1.第一步,在pom文件配置仓库地址,用于 deploy 上传
releases 对应正式版的仓库
snapshots 对应快照版的仓库
如果你打的jar包是以 -SNAPSHOT 结尾的, 那么就会被打到快照版的仓库,否则就会被打到正式版的仓库.
关于两个仓库的详细区别,请看:https://blog.csdn.net/weixin_70280523/article/details/143898595
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    
	...
    <distributionManagement>
        <repository>
            <id>releases</id>
            <url>http://localhost:8081/nexus/content/repositories/releases/</url>
        </repository>
        <snapshotRepository>
            <id>snapshots</id>
            <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
        </snapshotRepository>
    </distributionManagement>
	
	...
	
</project>2.第二步,因为上传 jar 需要认证,还要在 settings.xml 中配置认证信息
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  ...
	
  <servers>
    <server>
      <id>releases</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
    <server>
      <id>snapshots</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
  </servers>
  ...
	
</settings>其中 <server> 的 id 标签值要与 <repository> 以及 <snapshotRepository> 的 id 标签值一致,用户名密码这里为了简单就采用了私服管理员的,以后公司会分配个人账号和密码
3.第三步,运行 mvn deploy 命令,即可将对应模块的 jar 上传至私服,注意我们的 jar 的 version 是 snapshot 结尾的,最后会上传至 snapshots 对应的库.
mvn deploy 命令不仅会在项目生成package包,还会将jar包打到本地仓库,还会将打包的jar包发布到远程仓库(所谓的私服).

方式二:在settings文件里配置仓库地址
在maven的settings.xml中< profiles >节点配置< properties >,并在< properties >指定 < altSnapshotDeploymentRepository > 和< altReleaseDeploymentRepository >
<profiles>
     <profile>
   <id>nexus</id>
      <!-- 配置deploy打包到远程仓库-->
      <properties>
        <altSnapshotDeploymentRepository>
          nexus-snapshots::default::http://127.0.0.1:8081/nexus/content/repositories/snapshots/
        </altSnapshotDeploymentRepository>
        <altReleaseDeploymentRepository>
          nexus-releases::default::http://127.0.0.1:8081/nexus/content/repositories/releases/
        </altReleaseDeploymentRepository>
      </properties>
    </profile>
  </profiles>
  <activeProfiles>
    <activeProfile>nexus</activeProfile>
  </activeProfiles>注:
- nexus-snapshots和 nexus-releases要和maven的配置文件settings.xml文件中的server的id,两者必须保持一致
- 属性altSnapshotDeploymentRepository和altReleaseDeploymentRepository是随maven-release-plugin 2.8版一起引入的。低于2.8版本,执行mvn deploy时,则会报如下错误:
Deployment failed: repository element was not specified in the POM inside distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter解决方案就是在发布的项目中指定一下2.8版本以上的插件,形如下:
<build>
        <plugins>
            <plugin>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.8.2</version>
            </plugin>
        </plugins>
    </build>最后再执行maven的deploy命令进行发布
方式三:通过mvn deploy指定参数 命令
- 方法一:通过-D参数指定altSnapshotDeploymentRepository和altReleaseDeploymentRepository
形如下:
mvn deploy -DskipTests -DaltSnapshotDeploymentRepository=nexus-snapshots::default::https://YOUR_NEXUS_URL/snapshots
-DaltReleaseDeploymentRepository=nexus-releases::default::https://YOUR_NEXUS_URL/releases同理上述命令要执行成功,得确保deploy插件是基于2.8版本以上
- 方法二:通过-D指定要发布的jar的相关信息以及私仓地址,私仓id,私仓id要和settings.xml文件中的server的id保持一致
形如下:
mvn deploy:deploy-file -DskipTests -Dfile=jar包文件地址,绝对路径 -DgroupId=组名 -DartifactId=项目名称 -Dversion=版本号 -Dpackaging=jar -DrepositoryId=私库id(和setting.xml文件中的server的id保持一致) -Durl=私仓地址二.怎么配置从私库下载jar包
方式一:在pom文件配置仓库
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    
	...
    <repositories>
        <!-- 其它第三方的仓库 -->
		
		<!-- 本公司的私服仓库地址 -->
        <repository>
            <id>releases</id> <!-- 此id名称可以随便取 -->
            <url>http://localhost:8081/nexus/content/repositories/releases/</url>
        </repository>
        <repository>
            <id>snapshots</id> <!-- 此id名称可以随便取 -->
            <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
        </repository>
    </repositories>
	
	...
	
</project>方式二:在settings文件配置仓库 (建议此法)
   <profiles> 
<profile>
      <id>xz</id>
      <!-- 配置从远程仓库拉取依赖的远程仓库地址-->
      <repositories>
        <repository>
          <id>re</id> <!-- 此id名称可以随便取 -->
          <url>http://127.0.0.1:8081/nexus/content/repositories/releases/</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
        </repository>
        <repository>
          <id>sn</id> <!-- 此id名称可以随便取 -->
          <url>http://127.0.0.1:8081/nexus/content/repositories/snapshots/</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
        </repository>
      </repositories>
    </profile>
    </profiles>
  <!-- 配置激活profile -->
  <activeProfiles>
    <activeProfile>xz</activeProfile>
  </activeProfiles>上面上个方式都需要在 settings.xml 中配置认证信息
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
 
  ...
	
  <servers>
    <server>
      <id>releases</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
    <server>
      <id>snapshots</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
  </servers>
 
  ...
	
</settings>


















