目录
nexus API:
https://help.sonatype.com/repomanager3/integrations/rest-and-integration-api/components-api
deploy:deploy-file
https://maven.apache.org/plugins/maven-deploy-plugin/deploy-file-mojo.html
1.需要手动上传的权限,默认SNAPSHOT只有管理员有upload权限
curl -v -u name:password -X POST 'https://nexus.com/service/rest/v1/components?repository=test-snapshots' -F maven2.groupId=test -F maven2.artifactId=test -F maven2.version=0.0.3.test-SNAPSHOT -F maven2.asset1='test' -F maven2.asset1.extension=aar -F maven2.asset2='test/classes.jar' -F maven2.asset2.extension=jar -F maven2.asset2.classifier=source
2.本地要修改/usr/local/apache-maven-3.8.3/conf/settings.xml,配置账号密码仓权限等,注意repositoryId与文件中server的id要一致
<?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">
<!--
localRepository是设置本地仓库地址的。默认位置为: ${user.home}/.m2/repository/
Windows 参考格式:<localRepository>d:/repository</localRepository>
Linux 参考格式:<localRepository>$HOME/repository</localRepository>
-->
<!-- <localRepository>${user.home}\.m2\repository\</localRepository> -->
<!--每个pluginGroup元素都包含一个groupId,当你在命令行中没有提供插件的groupid时,将会使用该列表。
这个列表自动包含org.apache.maven.plugins和org.codehaus.mojo-->
<pluginGroups>
</pluginGroups>
<mirrors>
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<name>public</name>
<url>https://nexus.com/repository/maven-public</url>
</mirror>
<mirror>
<id>iot</id>
<mirrorOf>central</mirrorOf>
<name>iot public</name>
<url>https://nexus.com/repository/test-public</url>
</mirror>
</mirrors>
<!--POM中的repositories和distributionManagement元素为下载和部署定义的仓库。
一些设置如服务器的用户名和密码不应该和pom.xml一起分发。
这种类型的信息应该存在于构建服务器上的settings.xml文件中-->
<servers>
<server>
<id>releases</id>
<username>xxx</username>
<password>xxx</password>
</server>
<server>
<id>snapshots</id>
<username>xxx</username>
<password>xxx</password>
</server>
<server>
<id>test</id>
<username>xxx</username>
<password>xxx</password>
</server>
</servers>
<!--这里的profile元素是pom.xml的profile元素的一个裁剪版本-->
<profiles xmlns="">
<profile>
<id>aa</id>
<repositories>
<repository>
<id>public</id>
<name>all repoes</name>
<url>https://nexus.com/repository/cloud-platform-public</url>
<releases>
<enabled>true</enabled>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>aa</id>
<url>https://nexus.com/repository/cloud-platform-public/</url>
<releases>
<enabled>true</enabled>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
<profile>
<id>test</id>
<repositories>
<repository>
<id>test-public</id>
<name>all repoes</name>
<url>https://nexus.com/repository/test-public</url>
<releases>
<enabled>true</enabled>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>test-plugin</id>
<url>https://nexus.com/repository/test-public/</url>
<releases>
<enabled>true</enabled>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>aa</activeProfile>
<activeProfile>test</activeProfile>
</activeProfiles>
</settings>
mvn deploy:deploy-file -DgroupId=test -DartifactId=test -Dversion=0.0.2.test-SNAPSHOT -Dclassifier='source’ -Dfile='test/classes.jar’ -Durl=https://nexus.com/repository/test-snapshots -DrepositoryId=test
3.以上两种方式只能上传文件到nexus,没有写入依赖关系,以下方法可以
加入到build.gradle中:
apply plugin: 'maven-publish'
############
编译内容
############
def USER_NAME = "name"
def USER_PASSWORD = "password"
def PUBLISH_GROUP_ID = 'test'
def PUBLISH_ARTIFACT_ID = "test"
def PUBLISH_VERSION = "0.0.2.33.3-SNAPSHOT" // 替换成正确的版本号
task generateSourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier 'sources'
}
publishing {
repositories {
maven {
allowInsecureProtocol = true
def releasesRepoUrl = "http://nexus.com/repository/maven-releases/"
def snapshotsRepoUrl = "http://nexus.com/repository/maven-snapshots/"
url = PUBLISH_VERSION.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
// 凭证
credentials {
username USER_NAME // 仓库发布用户名
password USER_PASSWORD // 仓库发布用户密码
}
}
}
publications {
Production(MavenPublication) {
//指定group/artifact/version信息
groupId PUBLISH_GROUP_ID
artifactId PUBLISH_ARTIFACT_ID
version PUBLISH_VERSION
// 必须有这个 否则不会上传AAR包
afterEvaluate { artifact(tasks.getByName("bundleReleaseAar")) }
// 上传source,这样使用方可以看到方法注释
artifact generateSourcesJar
// 此处添加SDK中依赖的第三方库
pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', 'com.github.bumptech.glide')
dependencyNode.appendNode('artifactId', 'glide')
dependencyNode.appendNode('version', '4.12.0')
}
}
}
}
遗留问题:
尝试使用方法二传多个文件,使用-Dclassifiers,-Dfiles,报错,传单个文件是ok的