美文网首页
修改upload默认上传的文件路径

修改upload默认上传的文件路径

作者: 感召的鳞 | 来源:发表于2020-07-10 18:04 被阅读0次

    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)
                    }
                }
            }
    
        }
    

    相关文章

      网友评论

          本文标题:修改upload默认上传的文件路径

          本文链接:https://www.haomeiwen.com/subject/grnmcktx.html