Addev
Addev

Reputation: 32221

How to remove a dependency from the pom when publishing in mavenCentral

I'm trying to publish a library in MavenCentral. So I'm following this Chris Banes post.

Everything works but I have a little problem: The library uses the google play services lib:

compile 'com.google.android.gms:play-services:10.2.0'

But I don't need the dependency, it is just optional and, if the developer using my app also adds the play services to his dependencies, I want to detect it and use some classes. But it the developer doesn't add this dependency, I detect the classes are missing and I don't use them.

So basically I want to use the dependency in order to the build to compile, but I dont want the POM file to add this dependency. How can I do this?

Upvotes: 5

Views: 1408

Answers (1)

Bertrand Martel
Bertrand Martel

Reputation: 45352

From Maven plugin doc, you can use pom.whenConfigured to reorganize dependencies.

For instance, the following will not take junit & play-services artifactId as dependencies :

whenConfigured {
    p - >
        p.dependencies = p.dependencies.findAll {
            dep - > (dep.artifactId.notIn(["junit", "play-services"]))
        }
}

In your example you will have the following to exclude play-services :

pom {

    project {
        name POM_NAME
        packaging POM_PACKAGING
        description POM_DESCRIPTION
        url POM_URL

        scm {
            url POM_SCM_URL
            connection POM_SCM_CONNECTION
            developerConnection POM_SCM_DEV_CONNECTION
        }

        licenses {
            license {
                name POM_LICENCE_NAME
                url POM_LICENCE_URL
                distribution POM_LICENCE_DIST
            }
        }

        developers {
            developer {
                id POM_DEVELOPER_ID
                name POM_DEVELOPER_NAME
            }
        }
    }

    whenConfigured {
        p - >
            p.dependencies = p.dependencies.findAll {
                dep - > (dep.artifactId.notIn(["play-services"]))
            }
    }
}

//https://stackoverflow.com/a/26810828/2614364
Object.metaClass.notIn = { Object collection ->
    !(delegate in collection)
}

You can also exclude as dep.groupId or dep.version

Upvotes: 2

Related Questions