经常遇到这种情况:需要将不是maven项目的jar部署到私服中,便于使用,此时最简单的方式就是直接将jar部署上去。
一、部署已有jar包到私服仓库
命令详情如下:
mvn deploy:deploy-file \
--settings=/Applications/apache-maven-3.6.0/conf/settings.xml \
-DgroupId=rrrrrrr \
-DartifactId=tttttttttt \
-Dversion=x.y.z \
-Dpackaging=jar \
-Dfile=/***-x.y.z.jar \
## settings中设置的那个server的id一致(可以在pom中distributionManagement配置代替,但是此方式下一般无pom,故需要指明)
-DrepositoryId=nexus-releases \
## settings中配置的那个id的url(与repositoryId搭配使用)
-Durl=http://{ip}:{port}/repository/maven-releases
命令中的最后两项
-DrepositoryId=central-releases \
-Durl=http://{ip}:{port}/repository/maven-releases
是代表setting中server块(指定对应repoId的账号密码)下的匹配url的
贴出settings.xml中相关配置
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<!--
| This is the configuration file for Maven. It can be specified at two levels:
|
| 1. User Level. This settings.xml file provides configuration for a single user,
| and is normally provided in ${user.home}/.m2/settings.xml.
|
| NOTE: This location can be overridden with the CLI option:
|
| -s /path/to/user/settings.xml
|
| 2. Global Level. This settings.xml file provides configuration for all Maven
| users on a machine (assuming they're all using the same Maven
| installation). It's normally provided in
| ${maven.conf}/settings.xml.
|
| NOTE: This location can be overridden with the CLI option:
|
| -gs /path/to/global/settings.xml
|
| The sections in this sample file are intended to give you a running start at
| getting the most out of your Maven installation. Where appropriate, the default
| values (values used when the setting is not specified) are provided.
|
|-->
<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">
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
-->
<localRepository>/root/.m2/repository</localRepository>
<profiles>
<profile>
<id>JDK11</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>11</jdk>
</activation>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.compilerVersion>11</maven.compiler.compilerVersion>
</properties>
</profile>
<profile>
<id>JDK1.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>
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>Megvii_Central</id>
<url>NEXUS_ADDRESS/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>Megvii_Releases</id>
<url>NEXUS_ADDRESS/repository/maven-releases/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<!-- 插件仓库 -->
<pluginRepositories>
<pluginRepository>
<id>Megvii_Central</id>
<url>NEXUS_ADDRESS/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
<properties>
<altSnapshotDeploymentRepository>mcd-nexus-snapshots::default::https://localhost/repository/maven-snapshots/</altSnapshotDeploymentRepository>
<altReleaseDeploymentRepository>mcd-nexus-releases::default::https://localhost/repository/maven-releases/</altReleaseDeploymentRepository>
</properties>
</profile>
<!-- sonar插件相关属性 -->
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<sonar.host.url>
http://localhost:9000
</sonar.host.url>
</properties>
</profile>
</profiles>
<servers>
<server>
<id>nexus-releases</id>
<username>maven-deploy</username>
<password>adming123</password>
</server>
<server>
<id>nexus-snapshots</id>
<username>maven-deploy</username>
<password>qwerty</password>
</server>
</servers>
<!--添加id,激活profile。 -->
<activeProfiles>
<activeProfile>nexus</activeProfile>
<activeProfile>sonar</activeProfile>
<activeProfile>JDK1.8</activeProfile>
</activeProfiles>
<pluginGroups>
<pluginGroup>org.sonarsource.scanner.maven</pluginGroup>
</pluginGroups>
</settings>
二、maven项目部署
如果是该项目是maven项目,即具备pom,则在该项目pom中增加如下配置“distributionManagement”即可(那就直接在pom目录下执行 mvn deploy)
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>x.y.z</groupId>
<artifactId>tt</artifactId>
<packaging>pom</packaging>
...
<distributionManagement>
<repository>
<id>nexus-releases</id>
<url>https://localhost/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<url>https://localhost/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
</project>