# compileOnly not useable for excluding jars from WAR?

**URL:** https://discuss.gradle.org/t/compileonly-not-useable-for-excluding-jars-from-war/17278
**Category:** Help/Discuss
**Created:** [April 29, 2016, 2:08pm UTC](https://discuss.gradle.org/t/compileonly-not-useable-for-excluding-jars-from-war/17278 "2016-04-29T14:08:41Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![carlo\_lf](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/carlo_lf/32/1881_2.png) [@carlo\_lf](https://discuss.gradle.org/u/carlo_lf)
#### Post date: [April 29, 2016, 2:08pm UTC](https://discuss.gradle.org/t/compileonly-not-useable-for-excluding-jars-from-war/17278/1 "2016-04-29T14:08:41Z")

</div>

Recently I changed our build scripts from a self defined configuration named “provided” which we used for excluding unwanted dependencies going into the WAR and used instead the new ‘compileOnly’. I was surprised that this did not give me what I expected. Dependencies marked with 'compileOnly showed up in WEB-INF/lib .

I have the suspicion this is due to the fact that subprojects which are included into the WAR do not declare these unwanted dependencies as ‘compileOnly’, but use them as ‘compile’ dependencies. Therefore, declaring ‘compileOnly’ dependencies in the subproject which is responsible for the WAR has not the effect I hoped it had.

Question: does this explain why the WAR contains those unwanted dependencies?

I found a solution working for me in “[How can I exlude files from WAR when using WAR plugin against default or webAppDirName location](https://discuss.gradle.org/t/how-can-i-exlude-files-from-war-when-using-war-plugin-against-default-or-webappdirname-location/7366/8)”, where user ‘Daz’ pointed to something like this:

war {  
rootSpec.exclude("\*\*/slf4j\*.jar")  
,  
}

---

<div class="post-metadata">

### Author: ![mark\_vieira](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mark_vieira/32/5396_2.png) [@mark\_vieira](https://discuss.gradle.org/u/mark_vieira)
#### Post date: [April 30, 2016, 11:57pm UTC](https://discuss.gradle.org/t/compileonly-not-useable-for-excluding-jars-from-war/17278/2 "2016-04-30T23:57:52Z")

</div>

> [@carlo\_lf](#):
>
> I have the suspicion this is due to the fact that subprojects which are included into the WAR do not declare these unwanted dependencies as ‘compileOnly’, but use them as ‘compile’ dependencies. Therefore, declaring ‘compileOnly’ dependencies in the subproject which is responsible for the WAR has not the effect I hoped it had.
> 
> Question: does this explain why the WAR contains those unwanted dependencies?

Yes. Since they exist in the `compile` configuration as a result of other dependencies they will get included in the WAR. The dependencies in `compileOnly` are not _removed_ from the WAR, the are _omitted_. If the same dependency sneaks its way in by another means it will still be included. You will have to explicitly exclude these JARs, either as you have done (at the packaging level) or from the `compile` configuration itself.

```
configurations.compile.exclude module: 'slf4j'

```

---

<div class="post-metadata">

### Author: ![carlo\_lf](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/carlo_lf/32/1881_2.png) [@carlo\_lf](https://discuss.gradle.org/u/carlo_lf)
#### Post date: [May 1, 2016, 7:31pm UTC](https://discuss.gradle.org/t/compileonly-not-useable-for-excluding-jars-from-war/17278/3 "2016-05-01T19:31:37Z")

</div>

Okay, I understand.

Another option I tried and which didn’t work either was an exclude closure in the WAR task. In my understanding excludes declared at WAR level always should lead to the desired result. But it doesn’t work. Can you please explain this also?

Thanks.

---

<div class="post-metadata">

### Author: ![mark\_vieira](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mark_vieira/32/5396_2.png) [@mark\_vieira](https://discuss.gradle.org/u/mark_vieira)
#### Post date: [May 1, 2016, 10:59pm UTC](https://discuss.gradle.org/t/compileonly-not-useable-for-excluding-jars-from-war/17278/4 "2016-05-01T22:59:21Z")

</div>

Basically the `CopySpec` for the `web-inf` is isolated in that task. To do this you would have to do something like filter the files out of the task’s `classpath`.

```
war {
    classpath = classpath.filter { !it.name.contains('slf4j') }
}

```

Excluding from the configuration is really the better option, in my opinion.

---

<div class="post-metadata">

### Author: ![bendavidpaz](https://avatars.discourse-cdn.com/v4/letter/b/a587f6/32.png) [@bendavidpaz](https://discuss.gradle.org/u/bendavidpaz)
#### Post date: [May 3, 2017, 10:08pm UTC](https://discuss.gradle.org/t/compileonly-not-useable-for-excluding-jars-from-war/17278/5 "2017-05-03T22:08:57Z")

</div>

I am trying to understand what is the difference between compileOnly and runtime from the perspective of a war file. @mark_vieira, your explanation,

> The dependencies in compileOnly are not removed from the WAR, the are omitted.

I have a couple of subprojects, both integrated with a parent project. The parent creates a war file with dependencies from all three gradle scripts. I have runtime dependencies and compileOnly dependencies. I believe the compileOnly dependencies in the sub projects are needed so that the jars will build with those dependencies. If runtime dependencies used in the subprojects then those would be excluded from the jars and parent project war file. The war file obtains a subproject’s created jar but does not include the compileOnly dependencies.

Is this correct?
