upload默认就是上传当前项目的aar,如果想改下这个地址还挺麻烦。直接上代码
//用来记录原本的aar地址
private File artifactFile
//uploadName是自己起的任务名
Upload myUpload = project.tasks.create(uploadName,Upload){
doFirst{
//在执行上传任务前,将aar的地址替换成自己目标的文件地址
changeFile(getConfiguration(),".aar",aarFile)
}
doLast {
//任务执行结束后,替换回原本的aar地址,防止影响别的上传任务
changeFile(getConfiguration(),aarFile.getName(),artifactFile)
}
setConfiguration(project.getConfigurations().getByName("archives"))
repositories {
mavenDeployer {
pom.project {
groupId project.GROUP_ID
artifactId project.ARTIFACT_ID
version project.VERSION
}
repository(url: project.RELEASE_REPOSITORY_URL) {
authentication(userName: project.USERNAME, password: project.PASSWORD)
}
snapshotRepository(url: project.SNAPSHOT_REPOSITORY_URL) {
authentication(userName: project.USERNAME, password: project.PASSWORD)
}
}
}
}
/**
* 修改upload指定aar地址
* @param defaultConfiguration
* @param filterStr
* @param file
*/
void changeFile(DefaultConfiguration defaultConfiguration, String filterStr,File file){
if (!defaultConfiguration instanceof DefaultConfiguration){
return
}
DefaultConfiguration configuration = (DefaultConfiguration) defaultConfiguration
OutgoingVariant outgoingVariant = configuration.convertToOutgoingVariant()
for (PublishArtifact publishArtifact : outgoingVariant.getArtifacts()) {
if (publishArtifact.getFile().toString().endsWith(filterStr)) {
if (publishArtifact instanceof DecoratingPublishArtifact) {
artifactFile = publishArtifact.getFile()
DecoratingPublishArtifact artifact = (DecoratingPublishArtifact) publishArtifact
PublishArtifact pArtifact = artifact.getPublishArtifact()
Method method = pArtifact.getClass().getDeclaredMethod("getDelegate")
method.setAccessible(true)
ArchivePublishArtifact archivePublishArtifact = (ArchivePublishArtifact) method.invoke(pArtifact)
archivePublishArtifact.setFile(file)
} else if (publishArtifact instanceof ArchivePublishArtifact) {
artifactFile = publishArtifact.getFile()
((ArchivePublishArtifact) publishArtifact).setFile(file)
}
}
}
}
网友评论