By default, Blade generates the jar file of a module including the module version.
Searching information about how to remove the version from de filename, I was success change the jar's name with only the project name using this code in the workspace build.gradle file:
allprojects {
apply plugin: 'java'
jar.archiveFileName = "${project.name}.jar"
}
Now, I'm trying to use the symbolic name of the module (the value from the Bundle-SymbolicName attribute in bnd.bnd file) to generate the jar, but I can't find the way to get it from the build.gradle file.
Is there way to do it?
Finally, the solution was quite simple. When using the deploy task from the Liferay plugin in IntelliJ, it generates the .jar file with the version in the build/libs directory of the module, but deploys it to the server (directory bundles/osgi/modules) without the version.
How can I run Kotlin-Script (*.kts) files from within Gradle?
From the command line, I can call:
kotlinc -script foo.kts
How can I do this from gradle (build.gradle)?
You have two options here to run a gradle build file named build.gradle.kts.
Change the name of your build file in settings.gradle to build.gradle.kts:
rootProject.buildFileName = 'build.gradle.kts' and run gradle <tasknames>.
Run gradle -b build.gradle.kts <tasknames>.
Note that with version gradle 4.21 (current at the time of writing this), you do not need any "buildFileName" entry, or any other entry in settings.gradle. If there is a build.gradle.kts and no build.gradle, gradle will use build.gradle.kts by default.
However having the setting for buildFileName does allow build.gradle to be ignored. Without the setting, you have to remove (or rename) build.gradle in order to run build.gradle.kts.
If you have both a build.gradle and a build.gradle.kts you can switch between the two by commenting and un-commenting the buildFileName setting.
I am going through the gradle jar build example at https://guides.gradle.org/building-jvm-libraries/
The java source is not in a default src/main/java directory, it's in org\example\mylib directory. How can I customise gradle to run gradle jar from this directory and compile the java source files to a jar?
The whole directory structure is \mylib\src\main\java\org\example\mylib
When I am in that directory, and run gradle jar there is a success message but then when I check with jar -tf build/libs/mylib-0.1.0.jar all I see is the manifest files. There are no java classes.
If I try and run gradle jar in the \mylib directory alone, then it fails with error message Task 'jar' not found in root project
The build.gradle file is:
apply plugin: 'java'
version = '0.1.0'
Try adding this to the gradle.build:
sourceSets {
main {
java {
srcDirs = ['src\main\java\org\example\mylib']
}
}
}
I have the following project structure:
my-project
+my-project-data
+my-project-service
+my-project-example
Where each of these my-project-data, my-project-service, my-project-examples are defined as sub project to my-project in settings.gradle file. Of course, my-project-services has a dependency to my-project-data. And my-project-examples has a dependencies to my-project-data and my-project service.
In the project my-project-examples I want to have some class with main method and make some queries to DB (Assume that main class is in package com.project.main). Moreover I want to execute this in command line:
java -jar my-project.jar
But to do this I have to set Main-Class attribute. Where do I have to do this in root project build.gradle or somewhere else and what have to be the value of attribute?
You can use the application plug-in which provides exactly what you need.
Add the following to your example's project build.gradle:
apply plugin: 'application'
mainClassName = "com.company.package.YourClassWithMain"
Then, simply run the sample by executing 'gradle run' (or using gradle wrapper) under the example project directory.
I am trying with Gradle first time. I am trying with a maven java project to compile and create a jar file. It is compiling and creating the jar file in build/libs directory as
trunk-XXXVERSION-SNAPSHOT.jar
I am running gradle build file from trunk directory of this maven java project.
I want to get the project name (for ex: project1) in the jar file name, something like
project1-XXXVERSION-SNAPSHOT.jar
in build/libs directory.
Please suggest.
Here is the directory structure:
trunk
˪ build
˪ libs
˪ project1-1.0-SNAPSHOT.jar
˪ build.gradle
Include the following in build.gradle:
apply plugin: 'java'
apply plugin: 'maven'
archivesBaseName = 'project1'
version = '1.0-SNAPSHOT'
group = 'example'
This will produce the correct ZIPs, POMs and JARs.
Additionally, try setting:
archivesBaseName = 'project1'
or (deprecated):
jar.baseName = 'project1'
The default project name is taken from the directory the project is stored in. Instead of changing the naming of the jar explicitly you should set the project name correct for your build. At the moment this is not possible within the build.gradle file. Instead, you have to create a settings.gradle file in your root directory. This settings.gradle file should have this one liner included:
rootProject.name = 'project1'
I recently migrated to gradle 4.6 (from 3. something) and the
jar {
baseName = 'myjarname'
}
stopped working, gradle named my jar from the folder name.
So I switched to archivesBaseName = 'myjarname' which works.
Maybe this helps somebody else too.
If you has some submobule, you can use in build.gradle (for jar)
configurations {
jar.archiveName = 'submodule-jar.jar'
}
In Kotlin DSL you can also use:
tasks.jar {
archiveFileName.set("app.jar")
}
With Spring boot and Kotlin DSL you can use:
tasks {
bootJar {
archiveFileName.set("app.jar")
}
}
Currently using Kotlin as Gradle DSL. Following statement works for me:
tasks.withType<AbstractArchiveTask> {
setProperty("archiveFileName", "hello-world.jar")
}
It works on Spring Boot executable jars as well.
If you want to keep version numbers:
tasks.withType<AbstractArchiveTask> {
setProperty("archiveBaseName", "hello-world")
}
It will produce something like hello-world-1.2.3.jar
If you are using a newer Gradle version, baseName, archiveName will now be deprecated. Instead, use something like
jar {
archivesBaseName = 'project1'
archiveVersion = '1.0-SNAPSHOT'
}
if you want to append a date to the jar file name, you can do it like this:
jar {
baseName +='_' +new java.text.SimpleDateFormat("dd_MM_yyyy").format(new java.util.Date())
println(baseName) // just to verify
which results in <basename>_07_05_2020.jar
You have to remove the 'version' tag in your build.gradle file!
It can works in Gradle 7.5.1 with Groove DSL:
jar {
archiveFileName = "name.jar"
}
If you are using Kotlin DSL:
tasks.withType<Jar> {
archiveFileName.set("name.jar")
}