Skip to content

Instantly share code, notes, and snippets.

@ibrahimsn98
Last active May 4, 2022 07:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ibrahimsn98/3524fab9a4cea060b1824d84a79cee8e to your computer and use it in GitHub Desktop.
Save ibrahimsn98/3524fab9a4cea060b1824d84a79cee8e to your computer and use it in GitHub Desktop.
apply plugin: 'maven-publish'
static def isReleaseBuild() {
return !Config.versionName.contains("SNAPSHOT")
}
def getOutputDir() {
if (isReleaseBuild()) {
return "${project.buildDir}/releases"
} else {
return "${project.buildDir}/snapshots"
}
}
def getArtifactFilePath() {
if (isReleaseBuild()) {
return "$buildDir/outputs/aar/${Config.artifactId}-release.aar"
} else {
return "$buildDir/outputs/aar/${Config.artifactId}-debug.aar"
}
}
def getPublicationName() {
if (isReleaseBuild()) {
return "release"
} else {
return "debug"
}
}
publishing {
publications {
"${getPublicationName()}" (MavenPublication) {
groupId Config.groupId // com.company.project
artifactId Config.artifactId // my-component-library
version Config.versionName // 1.0.0-SNAPSHOT
artifact getArtifactFilePath()
// To include project dependencies
pom.withXml {
def dependencies = asNode().appendNode('dependencies')
configurations.getByName("${getPublicationName()}CompileClasspath").getResolvedConfiguration().getFirstLevelModuleDependencies().each {
def dependency = dependencies.appendNode('dependency')
dependency.appendNode('groupId', it.moduleGroup)
dependency.appendNode('artifactId', it.moduleName)
dependency.appendNode('version', it.moduleVersion)
}
}
}
}
repositories {
maven {
url "https://gitlab.com/api/v4/projects/projectId/packages/maven"
credentials(HttpHeaderCredentials) {
name = "Private-Token"
value = Config.gitLabAccessToken
}
authentication {
header(HttpHeaderAuthentication)
}
}
}
}
@mbilal51214
Copy link

Cannot resolve symbol 'Config'

@ibrahimsn98
Copy link
Author

@mbilal51214 try like this;

apply plugin: 'maven-publish'

def mVersionName = "..."
def mGroupId = "..."
def mArtifactId = "..."
def mGitLabAccessToken = "..."

static def isReleaseBuild() {
    return !mVersionName.contains("SNAPSHOT")
}

def getOutputDir() {
    if (isReleaseBuild()) {
        return "${project.buildDir}/releases"
    } else {
        return "${project.buildDir}/snapshots"
    }
}

def getArtifactFilePath() {
    if (isReleaseBuild()) {
        return "$buildDir/outputs/aar/${artifactId}-release.aar"
    } else {
        return "$buildDir/outputs/aar/${artifactId}-debug.aar"
    }
}

def getPublicationName() {
    if (isReleaseBuild()) {
        return "release"
    } else {
        return "debug"
    }
}

publishing {
    publications {
        "${getPublicationName()}" (MavenPublication) {
            groupId mGroupId // com.company.project
            artifactId mArtifactId // my-component-library
            version mVersionName // 1.0.0-SNAPSHOT
            artifact getArtifactFilePath()
            
            // To include project dependencies
            pom.withXml {
                def dependencies = asNode().appendNode('dependencies')
                configurations.getByName("${getPublicationName()}CompileClasspath").getResolvedConfiguration().getFirstLevelModuleDependencies().each {
                    def dependency = dependencies.appendNode('dependency')
                    dependency.appendNode('groupId', it.moduleGroup)
                    dependency.appendNode('artifactId', it.moduleName)
                    dependency.appendNode('version', it.moduleVersion)
                }
            }
        }
    }
    repositories {
        maven {
            url "https://gitlab.com/api/v4/projects/projectId/packages/maven"
            credentials(HttpHeaderCredentials) {
                name = "Private-Token"
                value = mGitLabAccessToken
            }
            authentication {
                header(HttpHeaderAuthentication)
            }
        }
    }
}

@mbilal51214
Copy link

mbilal51214 commented Feb 11, 2022

@mbilal51214
Copy link

apply from: "$rootDir/gradle-mvn-push.gradle"
where to put this line in build.gradle. I mean before android or inside android block {}

@desgraci
Copy link

desgraci commented May 4, 2022

  • What went wrong:
    Execution failed for task ':generatePomFileForChatPublication'.

Could not apply withXml() to generated POM
Configuration with name 'testmoduleCompileClasspath' not found.

Adding this at the root, where is this supposed to be run?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment