How to add a war build to a distribution - gradle

We have a gradle project that does a distTar to create a tar distribution file.
Each dependency project builds good placing all the dependencies and project jar files into the distribution libs folder.
We need to add a project that builds a war file. By adding the project as a dependency it adds all the war dependencies ... we need just the war file from the project war task.
Also the war project war task is not being executed.
Instead the jar task is being executed from distTar of the distribution project.
How do we execute the project war task and add just the war file to a "gui" folder in the tar distribution?

To get the built war file into the distribution add a task which the distTar depends. Have the depends on task depend on the other project war task. Take the output files and put them in the distribution. The key is to do the into/from in a doLast so the depends on other project war completes before the into/from for the distribution.
// create a task depends on other project war
// put into the distribution gui folder the war.output.files
// the doLast gives visibility to the war.outputs.files
// after the depends on is complete
task otherProjectWar(dependsOn: ':OtherProject:war') {
doLast {
applicationDistribution.into("gui") {
from( project(':OtherProject').war.outputs.files )
}
}
}
// add depends on for distTar
distTar {
dependsOn otherProjectWar
...
}

I believe the proper approach is to include desired task outputs to the distribution. Here is sample script that adds jar built by the project to the main distribution (for war use war outputs instead of jar outputs).
apply plugin: 'java'
apply plugin: 'distribution'
distributions {
main {
contents {
from jar.outputs
}
}
}
Running the distribution task then correctly resolves dependencies for both zip and tar.
> gradlew distTar
:clean
:compileJava
:processResources NO-SOURCE
:classes
:jar
:distTar

You'll need to add a dependency to the output of the war task. By default, ProjectDependency simply adds a dependency to the default configuration of that project.
dependencies {
compile project(':myproject').war.outputs.files
}
Edit: To clarify, compile probably isn't the best configuration to add this dependency to, this is just an example. If indeed all this project is doing is aggregating dependencies into a single distribution archive then the configuration is likely irrelevant.

Related

Why won't gradle jar build a jar that gradle build does?

In my Gradle project, I define an additional sourceSet.
sourceSets {
sample {
java {
srcDir 'sample/java'
}
compileClasspath += sourceSets.main.output + sourceSets.main.compileClasspath
}
}
I then add a task:
task sampleJar(type: Jar) {
classifier 'sample'
from sourceSets.sample.output
}
artifacts {
archives sampleJar
}
If I do > gradle build the additional jar file builds from the additional source set. However, if I do > gradle jar, it doesn't. any reason why?
When I go through the output messages, I see:
gradle build has sampleJar in the Tasks to be executed:
but
gradle jar doesn't.
But unsure as to why?
Because jar is just the task that assembles the main jar file.
build, on the other hand, is the top-level life-cycle task, which depends on assemble. And assemble is documented as
Depends on: jar, and all other tasks that create artifacts attached to the archives configuration.
Since your sampleJar pecisely creates an artifact attached to the archives configuration, assemble, and thus build depends on it.

How to set output file from another gradle build script's task from GradleBuild task?

I have project A with gradle build. I have another project B with gradle build also. I want to include project B's jar into project A's war. I can call project B's build script from project A's build script (see below).
But I can not set project B's jar as output file of task buildB.
Is there any way to set project B's jar (which is output file of task jar) as output file of task buildB?
task buildB(type: GradleBuild) {
buildFile = "../BProject/build.gradle"
tasks = ["clean", "jar"] // jar task produce xxx.jar as it's outputs.files
// HERE, any script to set xxx.jar as outputs.files of task buildB???
}
war {
from (buildB) { // can not get xxx.jar from buildB
into "WEB-INF/classes"
}
}
You probably need to configure a multi module project and define a project-scope dependency.
Since the answer full answer will be lengthy, here you can find a demo that shows how it can be done.
The demo consists of two projects. One project is built to war and has a dependency to another project which is built to war. If you build a project, b will be built automatically and included as a jar.
This is what I did.
/settings.gradle
include 'AProject', 'BProject'
/AProject/build.gradle
task buildB(type: GradleBuild) {
buildFile = "../BProject/build.gradle"
tasks = ["clean", "jar"] // jar task produce xxx.jar as it's outputs.files
}
war {
dependsOn buildB
doFirst {
from (project(':BProject').tasks['jar']) {
into "WEB-INF/classes"
}
}
}

How to list only the project dependencies jars in Gradle task

In my case I have multiproject build with many subprojects and sub/subprojects.
Then I would like to be able to call this task from different subprojects to produce different jars set (basing on the project dependencies). In that way I would be able to create separate distribution for different subprojects.
I've come to the stage that I can list all of projects deps jars in an ugly way:
task showJars {
doLast {
if( configurations && configurations.getByName('compile') ) {
configurations.compile.collect {
if( !it.toString().contains('.gradle') )
println(it)
}
}
}
}
I am working on a task to gather all project jars in one directory to create distribution for the app. Is there any clean way to achieve this ?
Thanks!
You might have to tweak the snippet below depending on your project and configuration, but this should give you the general idea:
task copyProjectDependencies << {
file("build/project-dependencies").deleteDir()
subprojects.findAll().each { project ->
copy {
from project.jar
from project.configurations.runtime
into "build/project-dependencies/jars"
}
}
}
You can also look at gradle's bundled application plugin:
apply plugin: 'application'
From the docs:
The Application plugin facilitates creating an executable JVM application. It makes it easy to start the application locally during development, and to packaging the application as a TAR and/or ZIP including operating system specific start scripts.
It will package it up as an archive with all dependencies.

Why does my jar file not not contain any class files?

I'm trying to add a task (gen or gen2) to my build.gradle that does exactly the same as the Jar-task:
version = "0.0.1"
apply plugin: 'java'
task('gen', type: Jar) {
}
task gen2(type: Jar)
Running
gradle jar
generates a JAR-file that contains .class-files, while running
gradle gen
or
gradle gen2
generate a JAR-file that does NOT contain any .class-files.
Whats wrong with my class definition?
To build a jar with all the classes from main, as a default jar task would, do this:
task gen2(type: Jar){
baseName = 'gen2Jar'
from sourceSets.main.output
}
You can also do from(sourceSets.main.output){ include "package" } to customize what packages are included.
Alternatively, to copy settings from the default jar task:
task gen(type: Jar){
baseName = 'genJar'
with jar
}
Infact you can have both of these in the same build.gradle. Running gradle jar builds default jar. gradle gen builds genJar.jar and gradle gen2 builds gen2Jar.jar, all of which contain all the classes from java.main

Getting Gradle to publish a source Jar instead of a standard compiled Jar

I am slowly moving two projects over to Gradle. Project A produces a jar file that is used by Project B.
Project A is a GWT component and Project B is a GWT application.
How do I get Gradle to generate a Jar that contains the Java source code of Project A rather than the compiled classes?
I have tried adding the following to the build.gradle file, but this generates a second Jar file containing the source - I want the main artefact to contain the source, so that it is published to my local Maven repository when I run the install task.
task sourceJar(type: Jar) {
classifier = 'sources'
from sourceSets.main.allSource
}
artifacts {
archives sourceJar
}
Is there a way to override the standard Jar creation task?
You can define your own 'myArtifacts' configuration and publish that instead. Note that since the install task is of type Upload, you should be able to change the default artifacts configuration from archives to sourceArchives:
configurations{
sourceArchives
}
artifacts{
sourceArchives sourceJar
}
install.configuration = configurations.sourceArchives
Hopefully, install should now just publish members of sourceArchives configuration.

Resources