How to configure gradle dependency for tomcat lib folder?

Hi have a multiproject where I want one of the libs to be in the tomcat lib folder (usually would be log4j or some other common lib). Code is available at github, the dependencies in the build files are:

regularLibA/build.gradle.kts

dependencies {
    implementation("org.apache.commons:commons-text:1.10.0")
}

regularLibB/build.gradle.kts

dependencies {
    implementation("org.apache.commons:commons-lang3:3.12.0")
    implementation("org.apache.commons:commons-text:1.10.0")
}

tomcatLib/build.gradle.kts

dependencies {
    implementation(project(":regularLibA"))
}

webApp/build.gradle.kts

dependencies {
    implementation(project(":regularLibA"))
    implementation(project(":regularLibB"))
    implementation(project(":tomcatLib"))
}

The generated war file is:

webApp.war
β”œβ”€β”€ index.jsp
└── WEB-INF
    β”œβ”€β”€ classes
    β”‚   └── com
    β”‚       └── github
    β”‚           └── i23098
    β”‚               └── web
    β”‚                   └── W.class
    └── lib
        β”œβ”€β”€ commons-lang3-3.12.0.jar
        β”œβ”€β”€ commons-text-1.10.0.jar
        β”œβ”€β”€ regularLibA.jar
        β”œβ”€β”€ regularLibB.jar
        └── tomcatLib.jar

How to configure gradle so that tomcatLib project is to be in the tomcat lib folder, so, the jar and it’s dependencies should not be added to the war file? i.e. the generated war file should be:

webApp.war
β”œβ”€β”€ index.jsp
└── WEB-INF
    β”œβ”€β”€ classes
    β”‚   └── com
    β”‚       └── github
    β”‚           └── i23098
    β”‚               └── web
    β”‚                   └── W.class
    └── lib
        β”œβ”€β”€ commons-lang3-3.12.0.jar
        └── regularLibB.jar

I tried changing the implementation(project(":tomcatLib")) to providedCompile(project(":tomcatLib")) in webApp/build.gradle.kts but for some reason gradle doesn’t add commons-lang3-3.12.0.jar in that case.

Also, how to create copy task of the files that are excluded from the war (tomcatLib.jar, regularLibA.jar and commons-text-1.10.0.jar) into the tomcat lib folder?

providedCompile is exactly the way to go. It is available at compile time but otherwise provided by the container.

As commons-text depends on commons-lang3 and you declared that :tomcatLib and all its transitive dependencies are provided by the container, also commons-lang3 is not packaged into the war.

For the last question, well, just do it, what problem do you have with it?

I picked some random common libs, did not notice they had dependency between them. Then it makes sense :slight_smile:

I don’t know how to do it :confused: How do I dynamically get the list of files (the generated tomcatLib.jar and transitive regularLibA.jar from the subprojects as well as their dependencies commons-lang3-3.12.0.jar and commons-text-1.10.0.jar) to put in the from of the Copy task?

val foo by tasks.registering(Copy::class) {
    from(configurations.providedRuntime)
    into(...)
}
1 Like

Thanks for you help. It worked :slight_smile:

1 Like