Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle: How to exclude a particular package from a jar?

Tags:

We have a package that is related to some requirements that were removed, but we don't want to necessarily delete the code because there's a possibility it will be needed again in the future. So in our existing ant build, we've just excluded this package from being compiled in our jar. These classes do not compile due to the fact that we've also removed their dependencies, so they can't be included in the build.

I'm attempting to mimic that functionality in Gradle as follows:

jar {
    sourceSets.main.java.srcDirs = ['src', '../otherprojectdir/src']

    include (['com/ourcompany/somepackage/activityadapter/**',
                 ...
        'com/ourcompany/someotherpackage/**'])

    exclude(['com/ourcompany/someotherpackage/polling/**'])
}

Even with the exclude call above (and I've tried it without the square brackets as well), gradle is still attempting to compile the polling classes, which is causing compile failures. How do I prevent Gradle from attempting to compile that package?

like image 344
StormeHawke Avatar asked Oct 24 '13 20:10

StormeHawke


People also ask

How do I exclude tasks in Gradle?

To skip any task from the Gradle build, we can use the -x or –exclude-task option. In this case, we'll use “-x test” to skip tests from the build. As a result, the test sources aren't compiled, and therefore, aren't executed.

How do you resolve transitive dependencies in Gradle?

A variant of a component can have dependencies on other modules to work properly, so-called transitive dependencies. Releases of a module hosted on a repository can provide metadata to declare those transitive dependencies. By default, Gradle resolves transitive dependencies automatically.

What is group and module in Gradle?

Group and module are properties for looking up libraries within maven repositories. For your dependency com.google.http-client:google-http-client:1.20.0. Group is com.google.http-client , module is google-http-client and the version is 1.20.0 .


1 Answers

This solution is valid if you don´t want to compile these packages, but if you want to compile them and exclude from your JAR you could use

// tag::jar[]
jar {
    exclude('mi/package/excluded/**')   
    exclude('mi/package/excluded2/**')  
}
// end::jar[]
like image 191
Gusa Avatar answered Sep 21 '22 12:09

Gusa