2
votes

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?

1
Out of curiosity, why do you care? - JB Nizet
You can split the project into two modules, one containing the classes and resources (use java plugin), the other the webapp (use war plugin) and depending on the first. - Henry
the jar can be used by others as a dependency. maybe two module is a solution. Can the war project be the root project, and the jar project as a sub-project? or two modules? what is difference between module and sub-project? thanks. - eastwater
Module and subproject are the same. - Henry

1 Answers

1
votes

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