How to configure gradle dependency for tomcat lib folder?

97 Views Asked by At

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 and dependencies are:

  • regularLibA depends on commons-text
  • regularLibB depends on commons-text and commons-lang3
  • tomcatLib depends on regularLibA
  • webApp depends on regularLibA, regularLibB and tomcatLib

With the simple build files/dependencies, the generated war file is:

webApp
├── 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
├── index.jsp
└── WEB-INF
    ├── classes
    │   └── com
    │       └── github
    │           └── i23098
    │               └── web
    │                   └── W.class
    └── lib
        ├── commons-lang3-3.12.0.jar
        └── regularLibB.jar

Also, how to create copy task of those files (tomcatLib.jar, regularLibA.jar and commons-text-1.10.0.jar) to the tomcat lib folder (i.e. how to get the list of excluded files from the war)?

1

There are 1 best solutions below

3
user6708591 On

Use the Gradle War Plugin.

plugins {
    id 'war'
}

It adds two dependency configurations, providedCompile and providedRuntime, which cause the dependency not to be included in the war file. Configure your dependencies as necessary, for example:

dependencies {
    providedRuntime 'tomcatLib'
    providedCompile 'org.apache.commons:commons-text:1.10.0'
    ...
}

As far as copying jars to the Tomcat lib directory is concerned, you can use the Copy task. See Working With Files in the Gradle docs for a good introduction how to do that.