28

I am a new convert to Gradle. Most of the tasks work fine. However, I see that the war task is always skipped. When I run in the debug mode, I see the following logs -

09:12:34.889 [LIFECYCLE] [class
org.gradle.internal.buildevents.TaskExecutionLogger] :war 09:12:34.889
[DEBUG]
[org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter]
Starting to execute task ':war' 09:12:34.889 [INFO]
**[org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter] Skipping task ':war' as task onlyIf is false.** 09:12:34.889 [DEBUG]
[org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter]
Finished executing task ':war' 09:12:34.889 [LIFECYCLE] [class
org.gradle.internal.buildevents.TaskExecutionLogger] :war SKIPPED

I am not sure why onlyIf is false. I did search on internet. But I did not find anything related.

Here is my Gradle file -

buildscript {
  ext {
    springBootVersion = '2.0.0.M2'
  }
  repositories {
    mavenCentral()
    maven { url "https://repo.spring.io/snapshot" }
    maven { url "https://repo.spring.io/milestone" }
  }
  dependencies {
    classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
  }
}

// Apply the java-library plugin to add support for Java Library
apply plugin: 'java-library'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'war'
apply plugin: 'checkstyle'
apply plugin: 'pmd'
apply plugin: 'findbugs'
apply plugin: 'jacoco'

version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    jcenter()
  mavenCentral()
  maven { url "https://repo.spring.io/snapshot" }
  maven { url "https://repo.spring.io/milestone" }
}

dependencies {

  compile('org.springframework.boot:spring-boot-starter')
  compile("org.springframework.boot:spring-boot-starter-web")
  compile("org.springframework.retry:spring-retry:1.2.1.RELEASE")

  compile("org.springframework.data:spring-data-cassandra:2.0.0.M4")

  compile("io.reactivex.rxjava2:rxjava:2.1.1")

  //compile("javax.persistence:persistence-api:1.0.2")
  //compile("org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.1.Final")
  compile("org.springframework.boot:spring-boot-starter-data-jpa")
  compile("com.zaxxer:HikariCP:2.6.0")

  // Test Dependencies
  testCompile("org.springframework.boot:spring-boot-starter-test")
  testCompile("org.powermock:powermock-mockito-release-full:1.6.4")
  testCompile("org.mockito:mockito-core:2.0.3-beta")
  testCompile("org.cassandraunit:cassandra-unit:3.1.3.2")
  testCompile("org.cassandraunit:cassandra-unit-spring:2.2.2.1")
  testCompile("com.h2database:h2:1.4.196")

    // This dependency is exported to consumers, that is to say found on their compile classpath.
    api 'org.apache.commons:commons-math3:3.6.1'

    // This dependency is used internally, and not exposed to consumers on their own compile classpath.
    implementation 'com.google.guava:guava:21.0'

    testImplementation 'junit:junit:4.12'
}

Here is the image of my project structure -

enter image description here

If you could help me with generating the war file that would be great.

aSemy
  • 5,485
  • 2
  • 25
  • 51
user3276247
  • 1,046
  • 2
  • 9
  • 24
  • You may have invalid project structure, e.g. web.xml is not placed where it should be. – Opal Aug 02 '17 at 08:22
  • I changed it to the standard project structure - src.main/webapp and then WEB-INF folder inside it. web.xml is in the WEB-INF folder. However still, the task gets skipped! – user3276247 Aug 02 '17 at 10:59

4 Answers4

52

try it

war {
    enabled = true
}
  • many thanks!!! I am completely new to gradle (I came from ant and maven), and this seems absurd to me... – andynaz May 24 '21 at 21:23
44

I have also faced same issue with jar task, it is skipping the jar generation as it is taking the default value for enabled=false if you dont specify any external value. Same solution works for jar as well based on @Alexander Servetnik
Enrironment: SpringBoot 2.x and gradle 4.4

jar {
    baseName = <your-jar name>
    enabled=true
}
Naresh
  • 451
  • 4
  • 8
  • 2
    Faced the same problem on java web application after upgrading Spring Boot from 1.x to 2.x. Dependent Gradle projects couldn't be compiled because JAR task didn't produce anything. This solution worked for me. – Toparvion Jul 02 '18 at 08:01
  • 1
    Also hit this bizarre one on a spring-boot 2 upgrade. – Andy Brown Aug 15 '18 at 16:15
  • 1
    Just spent an hour to find out why project A (boot 1.5.x) is building a jar and project B (boot 2.x) is not. – Chris Oct 15 '18 at 09:19
  • 2
    Even explicitly running `./gradlew war` task skips the creation of the war file! Seems counter intuitive. Setting `war {enabled=true}` in build.gradle fixes the issue. – kaicarno Jun 04 '19 at 14:38
  • Could be because with Spring Boot you should use bootJar task – Roman Plášil Jun 11 '19 at 08:59
  • I was getting nowhere until I found this answer. thanks – jeremyt Apr 02 '20 at 10:27
  • Just to comment, having `jar { enabled=true }` is enough to make dependant projects happy. See https://docs.spring.io/autorepo/docs/spring-boot/2.3.0.RELEASE/gradle-plugin/reference/html/#packaging-executable-and-normal. – rand0m86 May 22 '20 at 22:26
0

Try upgrading gradle version

Spring Boot’s Gradle plugin requires Gradle 6 (6.3 or later). Gradle 5.6 is also supported but this support is deprecated and will be removed in a future release.

https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/html/

Abhinav Atul
  • 601
  • 6
  • 14
0

This is because the Springboot Gradle plugin will create a bootJar task and by default will disable jar and war task. You can enable this, by adding the below code in projectName.gradle

jar {
    baseName = 'projectName'
    enabled=true
    
    manifest {
      ....
    }
}
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Soufiane D
  • 21
  • 3