0

I'm not sure if this is feasible, but wanted to get some comments/solution for the problem.

I'm creating a new dependency com.example:app-dep:1.0.1 will be used in com.example:app as compile dependency.

app-dep having a dependency io.undertow:undertow-core:2.0.1.Final which I don't want in com.example:app project, because I'm excluding the class file from com.example:app-dep related to undertow because the class requires on development time but not required in production.

When I add com.example:app-dep:1.0.1 in com.example:app I want to exclude io.undertow:undertow-core:2.0.1.Final.

But I want to control that from com.example:app-dep:1.0.1 maybe enable in future.

Some gradle I tried

TRY 1

app-dep - build.gradle

dependencies {
  implementation('io.undertow:undertow-core:2.0.1.Final') {
    transitive = true
  }
  implementation('io.undertow:undertow-servlet:2.0.1.Final') {
    transitive = false
  }
}
jar {
    from sourceSets.main.allSource
    excludes = ['com/example/ExampleServer**', 'public']
}

production-app build.gradle using spring-boot-gradle-plugin bootRepackage

buildscript {
  dependencies {
    classpath "org.springframework.boot:spring-boot-gradle-plugin:1.5.12.RELEASE"
  }
}
dependencies {
  compile 'com.example:app-dep:1.0.1'
}

OUTPUT: Still pulling and adding io.undertow:undertow-core:2.0.1.Final in production spring-boot jar

TRY 2

Using profiles

dependencies {
    compile 'org.apache.commons:commons-lang3:3.4'
    testCompile 'org.mockito:mockito-core:3.1.0'
    testCompile 'org.powermock:powermock-module-junit4:1.6.6'
    testCompile 'org.powermock:powermock-api-mockito:1.6.6'
    if(isDev) {
        implementation 'io.undertow:undertow-core:2.0.1.Final'
    }
}

OUTPUT: Class inside app-dep throwing compilation error saying the following when I do ./gradlew build

Undertow server = Undertow.builder().addHttpListener(8081, "localhost").setHandler(routingHandler).build(); ^ symbol: variable Undertow location: class ExampleServer 26 errors

What I can do but I don't want

In production-app build.gradle

dependencies {
  compile('com.example:app-dep:1.0.1') {
    exclude(module: 'com.example.app-dep')
  }
}

Because I want to control that from app-dep

Pasupathi Rajamanickam
  • 1,982
  • 1
  • 24
  • 48
  • So far I understand you want to exclude dynamically but not to use `exclude` in your production project, right?. – Jonathan JOhx Oct 14 '19 at 17:35
  • can you clarify what you mean- the dependency `app-dep` is required at "development time" - does this mean you need this to run on a dev box but not in production- or is the dependency "compile-only"? In the first case, profiles could be used- in the second case, compileOnly should be enough (see https://docs.gradle.org/current/userguide/java_library_plugin.html#sec:java_library_configurations_graph) – Daniele Oct 14 '19 at 18:00
  • @JonathanJohx correct – Pasupathi Rajamanickam Oct 14 '19 at 18:04
  • Okay, then generally when you have a big project with several dependencies which should be excluded or included for `prod` and `dev` you should use `profiles` for dependencies, then if it is `prod` you can add/exclude, perhaps this answer https://stackoverflow.com/a/40660106/10426557 can help you. – Jonathan JOhx Oct 14 '19 at 18:20
  • @JonathanJohx I tried that no luck, added the output in the question `TRY 2` – Pasupathi Rajamanickam Oct 14 '19 at 18:38

0 Answers0