gradle jar cant find java source - gradle

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']
}
}
}

Related

Unable to run jar file. (Error: Could not find or load main class co.pissarra.Mainkt)

I was trying to create a small webserver using spark and kotlin.
But I am stuck at the step where I should be able to create a jar of the project and run it from the command line. But I get the following error on running java -jar pissarra-core-all-1.0-SNAPSHOT.jar
Error: Could not find or load main class co.pissarra.Mainkt
I tried using intellij idea's artifact creation without success, and moved on to creating jar using build.gradle. Following is the code for the same
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Version': version,
'Main-Class': 'co.pissarra.Mainkt'
}
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
After running ./gradlew clean followed by ./gradlew fatJar, I am able to create jar file in build/libs/ directory. This jar also gives the same error.
On extracting the jar file, I am able to see the directory structure co/pissarra/ with the Mainkt.class file inside it. Also it has the META-INF directory with MANIFEST.MF file whose content are as follows
Manifest-Version: 1.0
Implementation-Version: 1.0-SNAPSHOT
Main-Class: co.pissarra.Mainkt
Since stackoverflow does not allows to upload files, you can find the jar file here. You can also build the jar file from the github project here.
Check your MANIFEST file. The class name should be "MainKt" and not "Mainkt"

gradle war: how to build jar, not classes

Gradle war plugin: how to build a jar and add it to war?
projectRoot/
src/main/java
src/main/resources
src/main/webapp
build a jar (foo.jar) from the java source code and resources.
add the jar under the WEB-INF/lib of the war.
WEB-INF/lib/foo.jar
The war task will not build a jar by default, and add all java classes and resources under WEB-INF/classes.
UPDATE
The War plugin extends the Java plugin to add support for assembling web
application WAR files. It disables the default JAR archive generation of the
Java plugin and adds a default WAR archive task.
There is a way to enable the Jar generation and let task war depends on task jar?
not sure if eastwater still needs the answer, hope others with the same problem will find this helpful
you can add/configure the war task in build.gradle
war {
classpath = classpath - sourceSets.main.output
from (jar) {
into 'WEB-INF/lib'
}
}
once build succeed, in build/libs folder you'll see the generated jar and the war containing the generated jar instead of classes
Add those into your root build.gradle
plugins {
id 'java'
id 'idea'
}
jar{
String somestr=''
configurations.runtime.each{somestr=somestr+" lib\\"+it.name}
manifest{
attributes 'Main-Class':'your_class_name'
attributes 'Class-Path':somestr
}
}
task copyJar(type:Copy){
from configurations.runtime
into ('build/libs/lib')
}
task release(type: Copy,dependsOn:[build,copyJar]){
}
add finally run this command
gradle release

Why does my jar file not not contain any class files?

I'm trying to add a task (gen or gen2) to my build.gradle that does exactly the same as the Jar-task:
version = "0.0.1"
apply plugin: 'java'
task('gen', type: Jar) {
}
task gen2(type: Jar)
Running
gradle jar
generates a JAR-file that contains .class-files, while running
gradle gen
or
gradle gen2
generate a JAR-file that does NOT contain any .class-files.
Whats wrong with my class definition?
To build a jar with all the classes from main, as a default jar task would, do this:
task gen2(type: Jar){
baseName = 'gen2Jar'
from sourceSets.main.output
}
You can also do from(sourceSets.main.output){ include "package" } to customize what packages are included.
Alternatively, to copy settings from the default jar task:
task gen(type: Jar){
baseName = 'genJar'
with jar
}
Infact you can have both of these in the same build.gradle. Running gradle jar builds default jar. gradle gen builds genJar.jar and gradle gen2 builds gen2Jar.jar, all of which contain all the classes from java.main

Why this gradle build script is not compiling java class?

I am trying to use Cascading in my Hadoop project. I am trying to implement first example given in Enterprise Data Workflows with Cascading book. I have written java class which contains Cascading related code and I have another build.graddle file which is supposed to compile that java class and build jar file out of it.
My folder structure is as follows :
main_folder
impatient
Main.java
build.gradle
My build.gradle file looks as below :
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'eclipse'
archivesBaseName = 'impatient'
repositories {
mavenLocal()
mavenCentral()
mavenRepo name: 'conjars', url: 'http://conjars.org/repo/'
}
ext.cascadingVersion = '2.1.0'
dependencies {
compile( group: 'cascading', name: 'cascading-core', version: cascadingVersion )
compile( group: 'cascading', name: 'cascading-hadoop', version: cascadingVersion )
}
jar {
description = "Assembles a Hadoop ready jar file"
doFirst {
into( 'lib' ) {
from configurations.compile
}
}
manifest {
attributes( "Main-Class": "impatient/Main" )
}
}
When I run gradle clean jar command from command prompt, I get build successful message. I tried to run this jar file using
hadoop jar impatient.jar <input file path> <output file path>
command but then it gives me Exception in thread "main" java.lang.ClassNotFoundException: impatient.Main exception.
So I checked contentes of jar file and found that that jar does not contain impatient/Main.class file.
Please note that I do not know anything about gradle.
Request someone to please tell me if there is anything wrong with gradle script or I am making some mistake.
Thanks !!!
Move your source file to
main_folder/impatient/src/main/java/Main.java
but leave build.gradle file where it is.
By default, Gradle uses src/main/java and src/test/java to look for production and test java sources (relative to root folder, which is impatient in your case)

Gradle - jar file name in java plugin

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")
}

Resources