eastwater
eastwater

Reputation: 5572

gradle war: how to build jar, not classes

Gradle war plugin: how to build a jar and add it to war?

projectRoot/
        src/main/java
        src/main/resources
        src/main/webapp
  1. build a jar (foo.jar) from the java source code and resources.
  2. add the jar under the WEB-INF/lib of the war.

    WEB-INF/lib/foo.jar

The war task will not build a jar by default, and add all java classes and resources under WEB-INF/classes.

UPDATE

The War plugin extends the Java plugin to add support for assembling web
application WAR files. It disables the default JAR archive generation of the 
Java plugin and adds a default WAR archive task.

There is a way to enable the Jar generation and let task war depends on task jar?

Upvotes: 2

Views: 1820

Answers (2)

jdir.s
jdir.s

Reputation: 131

Add those into your root build.gradle

plugins {
    id 'java'
    id 'idea'
}

jar{
    String somestr=''
    configurations.runtime.each{somestr=somestr+" lib\\"+it.name}
    manifest{
        attributes 'Main-Class':'your_class_name'
        attributes 'Class-Path':somestr
    }
}

task copyJar(type:Copy){
    from configurations.runtime
    into ('build/libs/lib')
}

task release(type: Copy,dependsOn:[build,copyJar]){
}

add finally run this command gradle release

Upvotes: 0

cokekutu
cokekutu

Reputation: 51

not sure if eastwater still needs the answer, hope others with the same problem will find this helpful

you can add/configure the war task in build.gradle

war {
    classpath = classpath - sourceSets.main.output
    from (jar) {
        into 'WEB-INF/lib'
    }
}

once build succeed, in build/libs folder you'll see the generated jar and the war containing the generated jar instead of classes

Upvotes: 2

Related Questions