Ktor shadow plugin doesn't include my resources folder - jar

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.

Related

Cannot specify -processorpath or --processor-path via `CompileOptions.compilerArgs`. Use the `CompileOptions.annotationProcessorPath` property instead

I'm cloning an android course project from GitHub, and facing a series of errors, I've manged to solve some of them till I stuck in last one, the scenario as follows:
1- error: Could not find com.android.support:appcompat-v7:25.1.0. (Solved).
2- error: File google-services.json is missing from module root folder. The Google Services Plugin cannot function without it.(solved)
3-error: Cannot specify -processorpath or --processor-path via CompileOptions.compilerArgs. Use the CompileOptions.annotationProcessorPath property instead.
This one that I can't fixError snip
and after adding this implementation (
implementation 'com.google.firebase:firebase-core:17.4.3'), I get that error as well.
error: This project uses AndroidX dependencies, but the 'android.useAndroidX' property is not enabled. Set this property to true in the gradle.properties file and retry.
Error snip
I've found that link that has exact fix to the errors mentioned in the question.
Cannot specify -processorpath or --processor-path via `CompileOptions.compilerArgs`
solution.
Delete “apply plugin: ‘android-apt’” line from your app module build.gradle file.
(Not necessary to work) Delete “classpath ‘com.neenbedankt.gradle.plugins:android-apt:1.2’” from your top-level build.gradle file.
In “dependencies” section in build.gradle rename
apt ‘net.simonvt.schematic:schematic-compiler:0.6.3’
to
annotationProcessor ‘net.simonvt.schematic:schematic-compiler:0.6.3’
I’m using newest 26.0.2 buildToolsVersion as well as newest support libraries and everything works like a charm.
Remember to use newest gradle (4.1-all for now) and gradle plugin (3.0.0 for now). Add google repository to download newest support libraries. Sync and rebuild project. Now when you go to your manifest file, there should be no more red android:name=".provider.generated.SquawkProvider"
My top-level build.gradle:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath ‘com.android.tools.build:gradle:3.0.0’
// Google Services plugin
classpath 'com.google.gms:google-services:3.1.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
google()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Let me know if it works.

How to rename a jar file inside another jar file?

I have jar foo.jar which contains jar foo/config/baar-temp.jar.
What is the best method to rename baar-temp.jar to baar.jar?
Actually, jar format is based on zip and can be operated on as a file system using for example ZipFileSystemProvider available in Java7. That allows us to do a rather simple manipulation with the insides of one:
private void renameStuffInsideJar(String jarFilePath){
URI uri = URI.create("jar:file:"+jarFilePath);
try {
FileSystem jarFile = FileSystems.getFileSystem(uri)) {
Path pathInJarfile = jarFile.getPath("foo/config/baar-temp.jar");
Files.move(pathInZipfile,pathInZipfile.resolveSibling("baar.jar"));
} catch(IOException e){
//TODO
}
}
Alternatively, if it's not code you want, you could just open your jar file in your preferred archive manager like 7zip or WinRar and rename it using that.

How to stop xsbt from reloading webapp on resources change

We are using sbt with xsbt-web-plugin to develop our liftweb app. In our project build we have several subprojects and we use dependencies of a Project to share some stuff between all the subprojects.
object ProjectBuild extends Build {
//...
lazy val standalone = Project(
id = "standalone",
base = file("standalone"),
settings = Seq(...),
dependencies = Seq(core) // please notice this
)
lazy val core = Project(
id = "core",
base = file("core"),
settings = Seq(...)
}
// ...
}
To ease the development we use 'project standalone' '~;container:start; container:reload /' command automatically recompile changed files.
We decided to serve some common assets from shared core project as well. This works fine with lift. But what we faced when added our files to core/src/main/resources/toserve folder, is that any change to any javascript or css file causes application to restart jetty. This is annoying since such reload takes lots of resources.
So I started investigating on how to prevent this, even found someone mentioning watchSources sbt task that scans for changed files.
But adding this code as a watchSources modification (event that println prints all the files) does not prevent from reloading webapp each time I change assets located in core resources folder.
lazy val core = Project(
id = "core",
base = file("core"),
settings = Seq(
// ...
// here I added some tuning of watchSources
watchSources ~= { (ws: Seq[File]) => ws filterNot { path =>
println(path.getAbsolutePath)
path.getAbsolutePath.endsWith(".js")
} }
)
I also tried adding excludeFilter to unmanagedSorces, unmanagedResorces but with no luck.
I'm not an sbt expert and such modification of settings looks more like a magic for me (rather then a usual code). Also such tuning seem to be uncovered by documentation =(
Can anyone please help me to prevent sbt from reloading webapp on each asset file change?
Thanks a lot!
You're on the right track by using watchSources, but you'll also need to exclude the resources directory itself:
watchSources ~= { (ws: Seq[File]) =>
ws filterNot { path =>
path.getName.endsWith(".js") || path.getName == ("resources")
}
}
Can you switch from using "resources" folder to using "webapp" folder? That will also free you from restarts. Here's a demo project for Lift (that uses "webapp"):
https://github.com/lift/lift_26_sbt/
For example, the "basic" template:
https://github.com/lift/lift_26_sbt/tree/master/scala_211/lift_basic/src/main/webapp

How to copy a file to an empty directory through gradle

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 /).

Gradle: How to add resource (not lib) jars to root of war?

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
}

Resources