How to overwrite the old artifacts while publishing latest artifacts in maven repository


(Sumith Waghmare) #1

Hi,

I am new to gradle and I want to know if there is any way to clean the maven repository before publishing new artifacts or overwriting the existing artificats using gradle?

Can anyone please help me on this by posting a dummy gradle script?


(Steven Spungin) #2

Hi Sumith. I refactor my artifactIds and groupsIds all the time and wrote a script to wipe out the local maven repo so it will not resolve the old coordinates. Perhaps it will help you. Don’t forget to
replace the repo path with your own.

 static File getArtifactDirectory(Project project) {
        def pubs = project.publishing.publications.findAll() as Set<MavenPublication>
        def pub = pubs[0]
        if (!pub){
            return null
        }
        //TODO replace this with your repo, or use env to get it
        String repository = '/Users/steven/.m2/repository'
        String groupPath = pub.groupId.replace('.', '/')
        File f = new File("$repository/$groupPath/$pub.artifactId")
        return f
    }
}

task('removeFromMavenLocal') {
                group = 'publishing'
                description = 'Remove artifact and all versions/classifiers/flavors from local maven repository.'
                doLast {
                    File f = getArtifactDirectory(project)
                    if (f.exists()) {
                        println "Deleting ${f.toURI().toString()}"
                        delete f
                    } else {
                        println "Could not find ${f.toURI().toString()}"
                    }
                }
            }