I'm writing a Javafx Application where I had to include a fxml file to copy from the source to the build directory. This is my task.
task copyRequiredFiles(type: Copy) {
from '/src/com/indywiz/game/ui/view/Game2048.fxml'
into 'build/classes/main/com/indywiz/game/ui'
}
task (runui, dependsOn: ['classes', 'copyRequiredFiles'], type: JavaExec) {
main = 'com.indywiz.game.ui.Main'
classpath = sourceSets.main.runtimeClasspath
}
If I run runui task, I get Skipping task ':copy Required Files' as it has no source files.
What is going wrong? Please let me know if you need any more information.
Below is my folder structure:
You gave an absolute part for from, but it needs to be a relative path (i.e. no leading /).
Related
I build my ktor project with this command: gradle shadowJar --no-daemon, but it doesn't add my resources folder to the fat jar.
My build.gradle is this:
val ktor_version = "2.0.2"
val kotlin_version = "1.6.10"
val logback_version = "1.2.11"
plugins {
application
kotlin("jvm") version "1.6.10"
id("com.github.johnrengelman.shadow") version "7.1.2"
}
group = "com.cstcompany"
version = "0.0.1"
application {
mainClass.set("com.cstcompany.ApplicationKt")
val isDevelopment: Boolean = project.ext.has("development")
applicationDefaultJvmArgs = listOf("-Dio.ktor.development=$isDevelopment")
}
repositories {
mavenCentral()
maven { url = uri("https://maven.pkg.jetbrains.space/public/p/ktor/eap") }
}
dependencies {
implementation("io.ktor:ktor-server-core-jvm:$ktor_version")
implementation("io.ktor:ktor-server-netty-jvm:$ktor_version")
implementation("ch.qos.logback:logback-classic:$logback_version")
testImplementation("io.ktor:ktor-server-tests-jvm:$ktor_version")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version")
//FreeMarker
implementation("io.ktor:ktor-server-freemarker:$ktor_version")
}
In IntelliJ my code works, but when I deploy it to a fat jar it doesn't find my files in my resources folder.
In order to have the same url that work for both Jar or in local, the url (or path) needs to be a relative path from the repository root.
..meaning, the location of your file or folder from your src folder.
could be "/main/resources/your-folder/" or "/client/notes/somefile.md"
Whatever it is, in order for your JAR file to find it, the url must be a relative path from the repository root.
it must be "src/main/resources/your-folder/" or "src/client/notes/somefile.md"
Now you get the drill, and luckily for Intellij Idea users, you can get the correct path with a right-click on the folder or file -> copy Path/Reference.. -> Path From Repository Root (this is it)
Last, paste it and do your thing.
I have to write the script in Gradle and I therefore have some questions.
The script must unpack the .ear file, and then included in the jar, then edit the content and save as ear (text file> jar> ear.)
1) Firstly, I would like to learn how to save a file to the current directory.
(The following code saves the file to another folder.)
task unzip(type: Copy) {
def zipFile = file('C:/Test/file.ear')
def outputDir = file('jar')
from zipTree(zipFile)
into getDestDir()
}
Phrases such as '.', '/' don't work.
2)Secondly, I would like to ask how to unpack the jar file because I can't unpack the above method. (It works only to the EAR)
3)At the end I would ask how you can convert the edited text file on the jar, and then on ear (without dependencies and manifest).
Because the resulting file do I have to file .ear
Thank you in advance for your answer.
You don't want to be writing files into your working directory. All the work should be done under the $buildDir.
A standard method is to set your into directory to a temporary location:
task myTask(type: Copy) {
from 'my/dir/'
into temporaryDir
}
You can unpack a JAR or ZIP file like so:
copy {
from zipTree('path/tozip.zip')
into temporaryDir
}
I am learning Gradle but I don't understand the jar task code that creates a jar with all the dependencies inside ( taken from Gradle Cookbook ):
jar {
baseName = jarBaseName
manifest { attributes "Main-Class": mainClass }
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}
My questions are:
1.The task name is jar. Because it's not written like jar<<{...} I'm assuming that this is run in the configuration phase, and not the execution one. Am I correct?
2.What exactly is configurations.compile? I'm assuming that some kind of dependencies classpath is queried, and each jar is zipTree-ed. Then all of this stuff is merged with the base classpath
. Please elaborate about it
3.The zipTree method, I'm assuming it kind of unarchives each jar but I'm not sure. Am I correct?
Regards,
Yes You're correct. When no action added (mostly with << see docs) the code is run at configuration phase. The code You provided is also run at configuration phase.
configurations.compile refers to a configuration named compile using configurations object (a kind of a configurations container). Prior to gradle 2.0 compile configuration is provided by default with java plugin. AFAIR now it's called javaCompile. According to zipTree You're also correct.
Yes You're.
I have an issue with copying files in gradle. Here is a snippet of build.gradle:
task mydir() {
new File("build/models").mkdir()
}
task copyTask(dependsOn:mydir, type: Copy){
from 'src/test/groovy/models'
into 'build/models'
from 'src/test/groovy/test.groovy'
into 'build/'
}
The issue here is in the first copy, that is, test.groovy is copied into build. But the first copy, where I am copying all the files in src/../models into build/models is not working. It simply copies the files into build directory only. My build/models remain empty. Can someone please tell me where what I'm doing wrong? I also have a separate task to create build/models directory prior to copyTask execution.
The build script contains the following mistakes:
The directory is created when mydir is configured, not when it is executed. (This means the directory will be created on every build invocation, regardless of which tasks get executed.)
new File('some/path') creates a path relative to the current working directory of the Gradle process, not a path relative to the project directory. Always use project.file() instead.
A Copy task can only have a single top-level into.
Here is a corrected version:
task mydir {
doLast {
mkdir('build/models')
}
}
task copyTask(dependsOn: mydir, type: Copy) {
into 'build'
from 'src/test/groovy/test.groovy'
into('models') {
from 'src/test/groovy/models'
}
}
The Copy task will create build/models automatically, so there is likely no need to have the mydir task. It's odd to use build as the target directory of a Copy task (should use a subdirectory).
In gradle, the copy task could only have single 'into' target with multiple froms.
The second 'into' override the first 'into' value and that's why it's only copied into 'build/' not 'build/models''
You may want to have a separate copy task or use ant.copy instead.
In the continuing saga of attempting to migrate from an insanely complicated ant build to gradle - We have some resource jar files for 'javahelp' that I'm generating. They contain no classes. I need to add the output of the project that creates these resource jars to the root of my war (not to WEB-INF/lib).
My attempted solution:
apply plugin: 'war'
//Move files into position for the mmplEar project
task stage(overwrite: true, dependsOn: war) << {
}
war {
from project(':help:schedwincli').buildDir.absolutePath + '/libs'
include '*.jar'
}
dependencies {
//Ensure the jar is generated, but we don't want it in the lib dir
providedCompile project(':help:schedwincli')
}
This compiles and runs, and the :help:schedwincli does run and generate the needed jar, however when I open up my war file, the expected jar is not present anywhere in the war. Suggestions?
Edit
I made the changes suggested by Peter below, but now I get this error:
Could not find property 'resources' on configuration container.
This is where it says it's failing:
from '../../runtime', /*Fails on this line*/
'../../runtime/html',
'../../runtime/html/Jboss',
'../../runtime/props',
'../../runtime/props/Jboss',
'../../scripts',
'../../../proj/runtime',
'../../../proj/runtime/html',
'../../../proj/runtime/html/Jboss',
'../../../proj/runtime/props',
'../../../proj/runtime/props/Jboss',
configurations.resources
include '*.css'
include '*.gif'
include '*.html'
include '*.jpg'
include '*.jnlp'
include '*.props'
include '*.properties'
include 'jsps/**'
include '*.jar'
include 'log4j/**'
include 'setupLdap.cmd'
include 'spreadsheets/*.xlsx'
You want something like:
configurations {
resources
}
dependencies {
resources project(':help:schedwincli')
}
war {
from configurations.resources
}