In my gradle.build I have the following code in order to copy the content of a folder into the jar during the build process
sourceSets {
main {
resources {
srcDirs = ["build/swagger-ui-myapp/"]
}
output.resourcesDir = "build/resources/main/swagger-ui-myapp"
}
}
The content of the folder ends up in the BOOT-INF/classes/ folder in the jar. But I don't want the content of the folder in that directory, I want the files to be inside the original folder, so I want them inside BOOT-INF/classes/swagger-ui-myapp.
How can I achieve this?
I wouldn't change the resources to do that. instead, I would just configure the bootJar task:
bootJar {
dependsOn(taskWhichGeneratesTheBuildSwaggerUiMyapp)
bootInf {
into("classes/swagger-ui-myapp") {
from("$buildDir/swagger-ui-myapp")
}
}
}
Related
I am trying to build a fat jar using the following in my Kotlin based gradle file.
val fatJar = task("fatJar", type = Jar::class) {
baseName = "safescape-lib-${project.name}"
// manifest Main-Class attribute is optional.
// (Used only to provide default main class for executable jar)
from(configurations.runtimeClasspath.map({ if (it.isDirectory) it else zipTree(it) }))
with(tasks["jar"] as CopySpec)
}
tasks {
"build" {
dependsOn(fatJar)
}
}
However, the fat jar has all the dependencies expanded out. I would like to have the jars included as is in a /lib directory but I cannot work out how to achieve this.
Can anyone give any pointers as to how I can achieve this?
Thanks
Well you are using zipTree in that map part of the spec, and it behaves according to the documentation: it unzips the files that are not a directory.
If you want the jars in /lib, replace your from with:
from(configurations.runtimeClasspath) {
into("lib")
}
In case anyone is using kotlin-multiplatform plugin, the configuration is a bit different. Here's a fatJar task configuration assuming JVM application with embedded JS frontend from JS module:
afterEvaluate {
tasks {
create("jar", Jar::class).apply {
dependsOn("jvmMainClasses", "jsJar")
group = "jar"
manifest {
attributes(
mapOf(
"Implementation-Title" to rootProject.name,
"Implementation-Version" to rootProject.version,
"Timestamp" to System.currentTimeMillis(),
"Main-Class" to mainClassName
)
)
}
val dependencies = configurations["jvmRuntimeClasspath"].filter { it.name.endsWith(".jar") } +
project.tasks["jvmJar"].outputs.files +
project.tasks["jsJar"].outputs.files
dependencies.forEach { from(zipTree(it)) }
into("/lib")
}
}
}
I was trying plugins:
io.freefair.war-overlay - but it just overlay one war to another,
waroverlay - it has appropriate option "includeWarJars true" but it
does't works for me.
Currently I'm trying write script:
dependencies {
/* jar, his files I would like to include in war */
compile project(':my_jar_1')
}
war {
/* Step 1. Copy all from my_jar_1 into WEB-INF/classes */
into('WEB-INF/classes') {
from configurations.compile
.filter { it.name.startsWith("my_jar_1") }
.collect { zipTree(it).matching { exclude 'META-INF/**/*.*' }}
}
/* Step 2. Deleting jar from war WEB-INF/lib. I got stuck here. It println, but doesn't delete jar */
doLast {
zipTree('build/libs/war_1-0.0.0.war').files.each {
if (it.path.endsWith('.jar')) {
delete it
println 'DELETED ' + it.path
}
}
}
}
Could somebody tell me how to make it work?
Or maybe smb know more elegant solution?
Also I was trying to declare my own configuration
configurations { overlay }
dependencies {
overlay project(':my_jar_1')
}
war {
into('WEB-INF/classes') {
from configurations.overlay
...
But it shows error
FAILURE: Build failed with an exception.
What went wrong: Failed to capture snapshot of input files for task 'war' property 'rootSpec$1$1' during up-to-date check.
Failed to create MD5 hash for file '/home/user/projects/OveralJarToWar/my_jar_1/build/libs/my_jar_1-1.0-SNAPSHOT.jar'.
The content of WEB-INF/lib and WEB-INF/classes is configured by single property classpath of war task. According to documentation:
Any JAR or ZIP files in this classpath are included in the WEB-INF/lib directory. Any directories in this classpath are included in the WEB-INF/classes directory
So, in your case, the classpath should be modified as follow
war {
def myJar = project(':my_jar_1').jar.outputs
def myClassesAndResources = project(':my_jar_1').sourceSets.main.output
classpath = classpath - myJar + myClassesAndResources
}
My build uses source code from two projects: ProjectA and ProjectB, and produces JAR with classes and resources from ProjectB. I defined custom sourceSet mainProjectB which is supposed to have output in a separate directory:
sourceSets {
mainProjectB {
output.classesDir = "$buildDir/build/classes/projectB"
output.resourcesDir = "$buildDir/build/resources/projectB"
java { srcDirs = ['src/main/java']}
resources { srcDirs = ['src/main/resources']}
}
mainProjectA {
java { srcDirs = [
'../projectA/src/main/java'
]}
resources { srcDirs = [
'../projectA/src/main/resources'
]}
}
test {
java {
srcDirs = [
'../projectA/src/test/java',
'src/test/java'
]}
resources {
srcDirs = [
'../projectA/src/test/resources',
'src/test/resources'
]}
}
}
compileJava {
source sourceSets.mainProjectB.allJava
source sourceSets.mainProjectA.allJava
}
processResources {
from sourceSets.mainProjectB.resources
from sourceSets.mainProjectA.resources
}
jar {
from sourceSets.mainProjectB.output.classesDir
from sourceSets.mainProjectB.output.resourcesDir
}
Problem: custom sourceSet mainProjectB ingores specified output directories.
The directories "$buildDir/build/classes/projectB" and "$buildDir/build/resources/projectB" are not created, and as a consequence, JAR includes files from both projects (instead of ProjectB).
UPDATE:
Projects A and B have circular dependencies. That is why they have to share source code.
I would consider to use subprojects and project to achieve your goal - gradel docs . With the following approach you can get any kind of jar file depending on your build :
group 'CoreProject'
version '1.0-SNAPSHOT'
subprojects {
apply plugin: 'java'
repositories {
mavenCentral()
}
}
project (':projectA') {
}
project (':projectB') {
def generatedResources = "$buildDir"
//in case you want resources and classes to be written to custom location where
//redefined paths are relative to projectB root folder
sourceSets {
main {
output.classesDir = 'build/classes/projectB'
output.resourcesDir = 'build/resources/projectB'
}
}
dependencies {
compile project(':projectA')
}
jar {
manifest.mainAttributes(
'Main-Class': "ProjectBClass"
)
}
//To create fat Jar that will contain classes and resources from all dependencies
task fatJar(type: Jar) {
manifest.from jar.manifest
classifier = 'all'
from {
configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) }
} {
exclude "ProjectAResource" //if want to exclude resources from projectA
exclude "META-INF/*.SF"
exclude "META-INF/*.DSA"
exclude "META-INF/*.RSA"
}
with jar
}
}
If you run jar task of projectB following jars will be created, each including only its own classes and resources : projectA/build/libs/projectA.jar , projectB/build/libs/projectB.jar('JAR with classes and resources from ProjectB' as you asked in your comment)
If you run farJar task of projectB the following jar file will be created that includes classes and resources from both projects and you can exclude any files patterns from projectA and projectB projects to create any final jar you like : projectB/build/libs/projectB-all.jar
Here is a screenshot of projects folders structure I created to mimic your scenario(as I understood it):
P.S. Also make sure none of the folders projectB/build and projectA/build are locked by any process and remove those handles if any, as otherwise Gradle will fail to run.
I created a simple Gradle build that exports the contents of ./src/main/groovy to a zip file. The zip file contains a folder with the exact same name as the zip file. I cannot figure out how to get the files into the root of the zip file using the distribution plugin.
i.e. gradlew clean distZip produces:
helloDistribution-1.0.zip -> helloDistribution-1.0 -> files
what I would like:
helloDistribution-1.0.zip -> files
My build.gradle file:
apply plugin: 'groovy'
apply plugin: 'distribution'
version = '1.0'
distributions {
main {
contents {
from {
'src/main/groovy'
}
}
}
}
I have attempted to fix the problem by adding into { 'dir' } but to no avail.
Using into '/' seems to do the trick:
contents {
from {
'src/main/groovy'
}
into '/'
}
Unfortunately, penfold's answer did not work for me. Here is the solution I came up with:
task Package(type: Zip) {
from {
def rootScriptFiles = [] // collection of script files at the root of the src folder
new File('src/main/groovy/').eachFile { if (it.name.endsWith('.groovy')) rootScriptFiles.add(it) }
['build/libs/', // build binaries
'src/res/', // resources
rootScriptFiles, // groovy root source files
]
}
baseName = pluginName
}
To copy files into the root of helloDistribution-1.0.zip -> helloDistribution-1.0 -> use
contents {
from {
'some/file'
}
into ''
}
I am new to Gradle and I have a source code location different than what Gradle expects.
Gradle expects to find the production source code under src/main/java and your test source code under src/main/resources. How do I configure Gradle to a different source code?
You have to add few lines to build.gradle:
To replace the default source folders, you will want to use srcDirs instead, which takes an array of the path.
sourceSets {
main.java.srcDirs = ['src/java']
main.resources.srcDirs = ['src/resources']
}
Another way of doing it is:
sourceSets {
main {
java {
srcDir 'src/java'
}
resources {
srcDir 'src/resources'
}
}
}
The same thing is applicable to test folder too.