Renaming file in jar via Gradle - java

I have a jar task (well a ShadowJar, but that shouldn't matter I think).
named<com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar>("shadowJar") {
archiveClassifier.set("")
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}
There's a file called linux/x64/org/lwjgl/liblwjgl.so within a dependency which I need to rename to liblwjgl3.so. Is there a way to do that within the jar task?

Related

Gradle - Create .properties file before / as part of each build and include it into JAR

I want Gradle to write a version.properties file into my Java classpath whenever I build the project.
I have a task like this
task createVersionPropertiesFile() {
def outputFile = new File(projectDir, "src/main/java/org/example/lpimport/version.properties")
outputs.file outputFile
doLast {
outputFile.text =
"""# generated by Gradle
version=$version
revision=${getSvnRevision()}
buildtime=${new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())}
"""
}
outputs.upToDateWhen { false }
}
And I include it like this:
build.dependsOn createVersionPropertiesFile
When I run the Gradle build task in Eclipse, the file is created as expected! ✅
If I run my application in Eclipse, the generated version.properties file can be loaded by my application. ✅
props.load( Main.class.getResourceAsStream( "version.properties" ) );
But the version.properties file will not be part of the built myproject-1.0.0.jar inside the myproject-1.0.0.zip distribution, although it resides in a normal class path package. ❌
How can I make Gradle include the on-the-fly generated version.properties inside the JAR?
I solved the problem by making two changes:
Write the .properties file to src/main/resources instead of src/main/java:
def outputFile = new File(projectDir, "src/main/resources/org/example/lpimport/version.properties")
Make the compileJava task depend on my custom task:
compileJava.dependsOn createVersionPropertiesFile
It seems the .properties file is ignored by Gradle (by its jar task??) if it resides below src/main/java.
Now it works and my built and packaged application can display its version properties on startup.

How to create a JAR file from gradle that contains the .java fields (source code) as well

I am using gradle to build some a uber jar file, this jar file has dependencies and I used this guide to create the task that I can run
My task is like this btw I am using kotlin
tasks.register<Jar>("uberJar") {
archiveClassifier.set("uber")
from(sourceSets.main.get().output)
duplicatesStrategy = DuplicatesStrategy.EXCLUDE;
dependsOn(configurations.runtimeClasspath)
from({
configurations.runtimeClasspath.get().filter { it.name.endsWith("jar") }.map { zipTree(it) }
}})
What can I add to this task to add my .java files as well.
Also I don't really understand much of what is going on here like what actually is SourceSets?
Try adding this to your build.gradle.kts and see if it works:
tasks.named<ProcessResources>("processResources") {
from("src/main/java")
}
When your uber-JAR builds, it should contain the source files. Though this will also add the source files to any JAR. If that is not what you want, then you will need to configure something similar in the uber-JAR setup.

Gradle task to rename file

I want to do a simple file rename in a gradle task. I have a jar called project-1.5.jar under the folder src and I want the jar to be renamed to just project.jar
So,
project/project-1.5.jar to project/project.jar using gradle
Any ideas are much appreciated.
The rename method should do the trick.
task renameArtifacts (type: Copy) {
from ('project/')
include 'project-1.5.jar'
destinationDir file('project/')
rename 'project-1.5.jar', "project.jar"
}
For me this worked - does not leave source files (no duplications).
task pdfDistributions(type: Sync) {
from('build/asciidoc/pdf/docs/asciidoc/')
into('build/asciidoc/pdf/docs/asciidoc/')
include '*.pdf'
rename { String filename ->
filename.replace(".pdf", "-${project.version}.pdf")
}
}
Gradle allows to call ant tasks from your task implementation.
It makes moving files as easy as
ant.move(file:'oldFile.txt', tofile:'newfile.txt')

How do I specify an extra folder to be on the classpath for gradle's application plugin 'run' task?

I've successfully configured my gradle build script to create a zip distribution of my application with an extra 'config' folder at the root. This folder contains (at least right now) only one properties file in use by the application, and is on the classpath for the application.
What I'm looking for now, however, is a way to do the same with the 'run' task in the application plugin. When I try to run my application this way, (for testing), my program fails to run because of a class trying to access this properties file on the root of the classpath.
A bonus would be if I could get IntelliJ or Eclipse to also add this folder to its classpath just like the other folders (src/main/java, src/main/resources, ...) so I can run and debug my code from within the IDE without invoking a gradle task. I want to try to avoid as much as possible tying this code to any one IDE, so that when anybody needs to work on the project, they just need to import the build.gradle file and have the IDE make the appropriate config files it needs.
Here is my build.gradle file:
apply plugin: 'application'
mainClassName = "MainClass"
startScripts {
// Add config folder to classpath. Using workaround at
// https://discuss.gradle.org/t/classpath-in-application-plugin-is-building-always-relative-to-app-home-lib-directory/2012
classpath += files('src/dist/config')
doLast {
def windowsScriptFile = file getWindowsScript()
def unixScriptFile = file getUnixScript()
windowsScriptFile.text = windowsScriptFile.text.replace('%APP_HOME%\\lib\\config', '%APP_HOME%\\config')
unixScriptFile.text = unixScriptFile.text.replace('$APP_HOME/lib/config', '$APP_HOME/config')
}
}
repositories {
...
}
dependencies {
...
}
Likely what needs to happen is that I need to have the /src/dist/config folder to be copied into the build directory and added to the classpath, or have its contents be copied into a folder that is already on the classpath.
I ended up taking Opal's suggestion as a hint, and came up with the following solution. I added the following to my build.gradle file:
task processConfig(type: Copy) {
from('src/main/config') {
include '**/*'
}
into 'build/config/main'
}
classes {
classes.dependsOn processConfig
}
run {
classpath += files('build/config/main')
}
Alternatively, a simpler approach would be to add a runtime dependency to my project as such:
dependencies {
...
runtime files('src/main/config')
}
I didn't end up doing it this way, however, because my distribution package ended up having .properties files in the lib folder... and I'm just picky that way.
As you can see in the docs run is a task of type JavaExec. So classpath for it can be modified. Try to add config folder to the classpath. See here.

How to create two separate jar files with a single build.gradle

I was trying to create two separate jar files with a single build.gradle.
It is almost working but one will overwrite the other. What's the right way of doing it?
jar {
//include contents of output dir
from "$buildDir/classes/main"
exclude '**/a1/**'
archiveName "X1-1.0.0.jar"
}
jar {
//include contents of output dir
from "$buildDir/classes/main"
exclude '**/a2/**'
archiveName "X2-1.0.0.jar"
}
Thanks
Rather than configuring the same Jar task twice, you need to use two Jar tasks. Assuming you are using the java plugin, you can reuse the Jar task named jar added by that plugin, and add another one yourself:
// reconfigure the Java plugin's `jar` task
jar {
exclude '**/a1/**'
baseName = 'X1'
}
// need to configure this one from scratch
task x2jar(type: Jar) {
// referring to the output in this way
// allows Gradle to infer task dependencies automatically
from sourceSets.main.output
exclude '**/a2/**'
baseName = 'X2'
}
// one way to make `gradle build` run both Jar tasks
assemble.dependsOn(x2jar)
For details on the configuration options, refer to the Gradle Build Language Reference.

Categories

Resources