How to build jar and war in a project

Hello users,

I applied a eclipse-wtp plugin to make a war file, but at the same time I would like to build two jars using some part of the project. Can I apply both war and jar plugin in a build.graddle?

Thanks,

You can create as many tasks of type Jar and War as you need.

If you apply the war plugin, a conventionally configured war task is added for you. The war plugin also applies the java plugin. The java plugin is what normally creates the default jar task, not specifically a jar plugin. You can apply both, war and java plugins, but there won’t be any difference vs. just applying the war plugin.

Add a Jar task for each additional jar file that you want:

task someOtherJar(type: Jar) {
    baseName = 'some-other-jar'
    from sourceSets.main.output // CUSTOMIZE WITH THE PART OF PROJECT YOU WANT
}
2 Likes

Thanks. :slight_smile:

@jjustinic would you be able to explain what the baseName and sourceSets.main.output values refer to/how they are used? I’m not sure what to replace those with in respect to my project to make this work.

This code configures a Jar task to create a JAR file. This is not required if you just want the default behavior from the java plugin which creates a single JAR with your main sources, but you can still customize the baseName and include additional files similarly.

To create a JAR file, the file needs a name. The name of the archive defaults to this pattern: [baseName]-[appendix]-[version]-[classifier].[extension]. Setting the baseName simply sets the first part of the name (it could be the only part if you leave those other properties blank). The actual name doesn’t matter. It can be whatever makes sense for your project.

The JAR file also needs contents. The example shown is what you would use if you just want the output from the main source set (compiled classes from src/main/java and the resources from src/main/resources by default), but this doesn’t make sense in practice. You wouldn’t normally want an additional JAR with the same contents as the default JAR. You’d want something different, but that depends on your need for separate JARs from the same project.