0

I'm trying to publish an aar locally to /.m2 directory using the maven-publish Gradle plugin. With the below code, I can publish the release version (with PTML), but not the debug one.

I would like to publish either a release or a debug one with custom tasks or through command line. Any help is greatly appreciated.


    publishing {
        publications {
            aar(MavenPublication) {

                groupId 'com.sample.project'
                artifactId 'SampleProject'
                version '1.1.0'
                artifact bundleReleaseAar

            }
        }

}

Reddy
  • 65
  • 1
  • 10

1 Answers1

5

You have to create a new publication for each of your buildTypes.

Currently you have only one publication named library which uses as the output from the bundleReleaseAar task as artifact.

When you want to publish the debug version you have to use the output from the bundleDebugAar task.

Therefore you need something like this:

project.afterEvaluate {
    publishing {
        publications {
            libraryRelease(MavenPublication) {

                artifact bundleReleaseAar
                artifact sourceJar

                groupId 'com.sample.project'
                artifactId 'DummyProject'
                version '1.0'

                pom.withXml {
                    def dependenciesNode = asNode().appendNode('dependencies')
                    def dependencies = new ArrayList<Dependency>()
                    dependencies.addAll(configurations.api.allDependencies)
                    dependencies.addAll(configurations.implementation.allDependencies)
                    dependencies.each {
                        if (it.group != null && (it.name != null || "unspecified".equals(it.name)) && it.version != null) {
                            def dependencyNode = dependenciesNode.appendNode('dependency')
                            dependencyNode.appendNode('groupId', it.group)
                            dependencyNode.appendNode('artifactId', it.name)
                            dependencyNode.appendNode('version', it.version)
                        }
                    }
                }
            }
            libraryDebug(MavenPublication) {

                artifact bundleDebugAar
                artifact sourceJar

                groupId 'com.sample.project'
                artifactId 'DummyProject'
                version '1.0'

                pom.withXml {
                    def dependenciesNode = asNode().appendNode('dependencies')
                    def dependencies = new ArrayList<Dependency>()
                    dependencies.addAll(configurations.api.allDependencies)
                    dependencies.addAll(configurations.implementation.allDependencies)
                    dependencies.each {
                        if (it.group != null && (it.name != null || "unspecified".equals(it.name)) && it.version != null) {
                            def dependencyNode = dependenciesNode.appendNode('dependency')
                            dependencyNode.appendNode('groupId', it.group)
                            dependencyNode.appendNode('artifactId', it.name)
                            dependencyNode.appendNode('version', it.version)
                        }
                    }
                }
            }
        }
    }
}

This will create two tasks publishLibraryReleaseToLocalMaven and publishLibraryDebugToLocalMaven. You can one run them depending on which type you want to publish.

StefMa
  • 3,344
  • 4
  • 27
  • 48
  • Hi, thanks for your answer. SInce code inside libraryDebug and libraryRelease are almost similar, is there anyways to reuse the code? – Danish Ansari Jan 20 '23 at 14:05
  • 1
    Sure. That's Groovy code (can also be Kotlin nowadays). You could extract methods/functions if you like. – StefMa Jan 20 '23 at 20:52