Gradle generate zip with jars excluding sub-projects - gradle

I have multi-module project. I am using the following Gradle script to build zip which contains the sub-project's jar along with its dependencies.
dependencies {
api(project(":sub-project1"))
implementation(project(":sub-project2"))
implementation("some:lib:1")
implementation("some:lib:2")
}
tasks.register<Zip>("buildZip") {
archiveFileName.set("filename.zip")
from(tasks.jar)
from(configurations.runtimeClasspath)
}
The generated zip file will have all the jars of project1 and project2 along with dependency jars mentioned in the current sub-project.
My requirement is, I want to exclude jar files from project1 and project2 (along with dependency jars from there) and only include this sub-project's jar and dependencies mentioned in this sub-project.
How can this be achieved ? Any help is appreciated. Thanks.

Related

How to refer resource files from dependent jar in gradle task?

I want to use resource file from dependent jar to generate code/POJO. Maven has maven-remote-resources-plugin. I didn't find any equivalent gradle plugin. Also not sure if gradle has any default task for the same.
I use this way to use my custom lib with Gradle:
Copy your JAR file to your module ‘libs’ folder. If you don’t have ‘libs’ folder then create one.
Add the library to your ‘module’ Gradle dependencies
dependencies:
dependencies {
compile files('libs/your-library-file-name.jar')
}

gradle ear plugin - custom jar's added to EAR root folder

I am trying to build ear using gradle build and ear is building fine.In my project ejb module is creating multiple custom jar along with ejb jar .How to add them onto ear root directory instead of adding them in lib folder
I am able to include them in ear lib folder but not in EAR root directory
EAR/APP-INF/lib/A.jar
should be EAR/A.jar
project(':A-project')
earproject(path: ':A-project',configurations:'provided')
earfiles(project(':A-project').tasks.findByName('ATask'))
Try adding deploy(A.jar) to your dependencies{} code block. This will create an ejb entry in the application.xml
build.gradle
dependencies {
deploy(A.jar)
}
Reference
dependencies
{
deploy project(path: ':A-Project')//jar files
deploy project(path: ':B-Project',configuration:'archives')//war files
deploy project(project(':C-Project').tasks.findByName("app"))//project in built jars
}

Gradle include jar produced by another project in war

Currently I have two projects with gradle build.gradle. The first is going to create a fat jar file, which I would like to include in a war file. I thought compiling it would be enough, but it doesn't seem to be ending up in the /lib directory of my war file. Anyone have thoughts I am quite new to gradle.
dependencies {
compile project(':JarProject')
providedCompile 'javax.servlet:javax.servlet-api:3.1.0'
providedCompile 'org.apache.tomcat:tomcat-jsp-api:7.0.55'
}
war {
archiveName 'WarProject.war'
from 'JarProject/build/libs'
webXml = file('src/web.xml')
}
Does the second project war need to be in providedRuntime? Or should I publish the jar from the other project in the local maven repo and include it that way?
The War task essentially behaves like a CopyTask with regards to stuff it packs in the war, so the documentation on working with files is useful. In essence, I think you need something like (untested):
from fileTree('JarProject/build/libs') {
into("lib")
}
That being said, using mavenLocal() and publishing there also works, but it can lead to unexpected results when the war includes some old version from local, picking up the jar explicitly from the file system like above is better.
I think the elegant solution would be to use multi project builds and project level dependencies. You would have the two builds as separate projects of the same Gradle build and add the "jar project" as a regular compile dependency.
How have you declared the dependency? I assume you have a multi-project build with subprojects A and B, both using the War plugin. I made an experiment using Gradle 2.4 and if I declare B/build.gradle like this:
apply plugin: 'war'
dependencies {
compile project(':A')
}
then B.war contains WEB-INF/lib/A.jar. If you correctly follow conventions of Gradle War plugin (place web resources in A/src/main/webapp/ and code-related resources in A/src/main/resources/), then A.jar should contain what you want.
see this

gradle to bundle nested jar dependencies into module output jar

How to make gradle to included some dependencies into module resulted jar as jar? i.e. to make jar with nested jar's e.g. in lib folder ?
This is not Android project, and this should be done for many modules in multi-module project, so fatJar, uberJar, shadowJar alike solutions seem not to fit.
You just need to add an additional from directive to include dependencies in your jar:
task jarJar(type: Jar) {
baseName = project.name + '-jarjar'
from { configurations.compile }
with jar
}

Gradle: Include jar dependencies into ear/libs

I'm using Gradle to create an ear, but I'm having a hard time figuring out how to properly include dependencies in the lib folder.
I know that I can use
earlib project (group:name:version)
to include jars there. And that also those jars' compile dependencies are going to be added in the ear's lib folder.
My problem is that I also have jars that are listed in the ear's build.gradle as
deploy project (':jar-name')
How can I add their dependencies in the lib folder?
The only way I found is to add them as earlib or as dependencies of an earlib, but this forces me to write them twice and feels harder than it should be (with Maven you just need to specify your dependencies in the subprojects and they will be added in the ear\lib directory)
EDIT
Basically what I would like to end up with, is a transitive deploy configuration.

Resources