Gradle: How to Exclude a Particular Package from a Jar

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

If you have some sources that you don't want to be compiled, you have to declare a filter for the sources, not for the class files that are put in the Jar. Something like:

sourceSets {
main {
java {
include 'com/ourcompany/somepackage/activityadapter/**'
include 'com/ourcompany/someotherpackage/**'
exclude 'com/ourcompany/someotherpackage/polling/**'
}
}
}

How to exclude resources from the JAR in Gradle and also run via IntelliJ

Excluding Resources from the JAR with Gradle

Example build.gradle

plugins {
id 'java'
id 'application'
}

// Project specifics
version '1.0-SNAPSHOT'
group 'com.example'

sourceCompatibility = 1.8
project.mainClassName = "core.ExampleApp"

dependencies {
testCompile 'junit:junit:4.12'
}

// Define the project source paths
sourceSets {
main.java {
srcDir 'core/src'
}
test.java {
srcDir 'core/test'
}
}

// Exclude all resources from the jar
jar {
processResources.exclude('*')
}

// Allows the 'test' task to see the directories on the classpath
test {
classpath += files('conf')
classpath += files('res')
}

// Allows the 'run' task to see the directories on the classpath
run {
classpath += files('conf')
classpath += files('res')
}

// Adds the directories to classpath in the start scripts
// They will have '$APP_DIR/lib/' prepended so they need to be copied into 'dist/lib/'
startScripts {
run {
classpath += files('conf')
classpath += files('res')
}
}

// Copy 'conf' and 'res' into the 'dist/lib' directory
distributions {
main {
contents {
from('conf').into("lib/conf")
from('res').into("lib/res")
}
}
}

  • Resources are found during gradle run
  • Resources are found during gradle test
  • Built JAR does not contain the resources
  • Resources copied into example.zip/lib/conf & .../lib/res
  • Resource content are added to the classpath in the generated build scripts
  • Running tests via IntelliJ UI does work (right click -> run tests e.g.)

BUT:

  • Running via IntelliJ UI does not work (right click -> run main() e.g.)

Resolution:

  • The IntelliJ Gradle Plugin allows right click -> run and right click -> debug
  • The above adds a configuration to IntelliJ run panel as a decent substitute

How to exclude a dependency from built JAR in Gradle?

I think you want something like this.

dependencies {
compileOnly "com.thesamet.scalapb:scalapb-runtime-grpc_2.13:0.11.1"
}

ref: https://blog.gradle.org/introducing-compile-only-dependencies

gradle exclude specific jars from runtime dependencies

You can exclude all dependencies from a group, or only some modules of a group, in dependencies block:

    dependencies {

/* -------------- SpringBoot without Tomcat ------------------- */
compile('org.springframework.boot:spring-boot-starter-web') {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
}

}

I add link to the Gradle documentation explaining transitive dependencies in detail: https://docs.gradle.org/current/userguide/managing_transitive_dependencies.html

Gradle - Exclude file from being packaged with jar

You can try something like this, which i know works on my end. In your build.gradle under the JAR definition add your exclude. Ex:

jar {     
exclude('your thing')
}


Related Topics



Leave a reply



Submit