Why this gradle build script is not compiling java class? - gradle

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)

Related

Gradle : Even though jar contains main class file getting Error: Could not find or load main class

We created jar file with all dependencies using build.gradle. It successfully gets created, with all dependencies and .class files under appropriate directory (e.g. Launch class under com.role.Browse.framework).
But while running the jar getting Error: Could not find or load main
C:\IdeaProjects\launch_app\build\libs>java -jar Launch_App-all-1.0.0.jar
Error: Could not find or load main class com.role.Browse.framework.Launch
Below is the snap of build.gradle
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'eclipse'
version '1.0.0'
description = ""Launch_App""
sourceCompatibility = 1.8
targetCompatibility = 1.8
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
//create a single Jar with all dependencies
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'Gradle Jar File',
'Implementation-Version': version,
'Main-Class': 'com.role.Browse.framework.Launch'
}
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar }
//To add this single Jar standard assemble or build task
artifacts {
archives fatJar
}
I refer some solutions for similar issues, but nothing works for me,
Gradle : Could Not find...
Note: My main class (i.e. Launch ) extends junit.framework.TestCase class and junit libraries are also included in .jar file.
Please, let me know if anything i'm missing here.
Even for me facing the same issue.
For my execution after running the jar file execution command
java -jar jarfile_name.jar giving no output.
please check few points.
Check with the manifest.mf file and see this should have an entry for main class
like Main class="class_Name"

gradle jar cant find java source

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

dependent jar is not bundled along with project jar Gradle

I have a project corehibernate and a project coregeneral. corehibernate is dependent on coregeneral. I need the jar file of coregeneral to be bundled along with the corehibernate jar. I tried various versions of the build.gradle thing, nothing worked.
I tried compile files("../coregeneral/build/libs/coregeneral.jar")
This version of fatJar too does not work.
apply plugin: 'java'
repositories {
jcenter()
}
dependencies {
compile (':coregeneral')
testCompile 'junit:junit:4.12'
}
jar {
baseName='corehibernate'
from ('bin')
}
task fatJar(type: Jar, dependsOn: jar) {
baseName = project.name + '-fat'
}
There are two basic ways how to bundle projects together. The first would be to use application plugin which creates a zip with scripts that will also execute your application and bundle all jars by default. Second way is to use distribution plugin and define the final archive yourself (zip or tar).
Here is a sample project using the application plugin:
settings.gradle
rootProject.name = 'root'
include 'partone', 'parttwo'
build.gradle
subprojects {
apply plugin: 'java'
}
partone/build.gradle - this one is empty
parttwo/build.gradle
apply plugin: 'application'
mainClassName = 'Hello'
dependencies {
compile project (':partone')
}
Give that both projects actually have some content (classes), when you run gradle :projecttwo:build it will generate a zip file with executable scripts and both jars bundled inside.
If you prefer to use distribution plugin, change the parttwo/build.gradle to:
apply plugin: 'distribution'
distributions {
main {
contents {
from jar
from (project.configurations.runtime)
}
}
}
dependencies {
compile project (':partone')
}
And again run gradle :parttwo:build. It will create a zip file that contains both jars.

How to generate a war file based on two subprojects

I have a project which is splitted into two subprojects.
/project
/sub-project-a (backend with JAVA source which is compiled into JAR file)
/sub-project-b (frontend sources which are compiled with grunt via gradle call)
build.gradle
settings.gradle (contains include 'sub-project-a', 'sub-project-b')
My Question is how can I create a War file with sub-projects and external lib dependencies? The following code snipped is my current build.gradle:
apply plugin: 'war'
version '1.0.0'
repositories {
mavenCentral()
}
dependencies {
compile project(':sub-project-a')
compile project(':sub-project-b')
compile 'com.google.code.gson:gson:2.2.4'
}
task copy(type: Copy) {
from 'sub-project-a/build', 'sub-project-b/build'
into 'build'
}
build.dependsOn clean, copy
war {
archiveName 'project.war'
}
One detail is important. The java context listener (deep inside project code) work with compiled backend as jar file from WEB-INF/lib folder. This means that all class files can't be easily used from WEB-INF/classes folder.
As you can see I played with dependencies and a custom copy task. I'm not sure what is right gradle way. How should I do this?
SOLUTION
Define with war.from methode, where you get your static sources.
gradle docu
from(sourcePaths) -
Specifies source files or directories for a copy. The given paths are
evaluated as per Project.files().
My changed build.gradle
apply plugin: 'war'
version '1.0.0'
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.code.gson:gson:2.2.4'
}
war {
archiveName 'project.war'
from 'sub-project-a/build/dist', 'sub-project-b/build/dist'
}
SOLUTION (for cleanly closing this question) shamefully taken from the question's originator ;-)
Define subproject dependencies with the "war.from" method, where you get your static sources.
gradle documentation excerpt: from(sourcePaths) - Specifies source files or directories
for a copy. The given paths are evaluated as per Project.files().
Ronny's changed build.gradle
apply plugin: 'war'
version '1.0.0'
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.code.gson:gson:2.2.4'
}
war {
archiveName 'project.war'
from 'sub-project-a/build/dist', 'sub-project-b/build/dist'
}

Gradle equivalent of Maven Assembly

As long as Gradle has been around, I'd have thought this was well documented, but I'm struggling. Sorry if this is obvious to everyone else.
I have a multi-project build setup with maven, I'm setting up gradle for comparison.
Project A produces a jar file, Project B produces a war file (and depends on the jar from A). Project C I want to create an assembly, i.e. a zip file. The zip file should contain the contents of a directory within project C, the war file from Project B, and the unpacked contents of a tomcat assembly pulled down from Archiva.
The problem I'm having is that when gradle evaluates the build.gradle files, it fails because it can't find the war file - which is no surprise, the war file hasn't been built yet.
So, how do I express the dependency on the war file in such a way that it doesn't cause the failure during evaluation?
apply plugin: 'distribution'
description = """E2am Assembly"""
project(':assembly') {
dependencies {
compile project(':webapps/e2am')
}
}
dependencies {
compile group: 'com.e2open.platform', name: 'e2am', version:'9.0-SNAPSHOT', ext:'war'
compile group: 'com.e2open.platform.third-party.tomcat', name: 'tomcat-assembly',version:'7.0.42-1002', classifier:'bin', ext:'zip'
}
task explodeTomcat(type: Copy, dependsOn: ':webapps/e2am:install'){
configurations.compile.filter { it.toString().endsWith(".zip") }.each{
from zipTree(it)
}
into 'TomcatUnpacked'
configurations.compile.filter { it.toString().endsWith(".war") }.each{
from it
}
into 'war'
}
distributions {
main {
baseName = 'e2am-assembly'
contents {
from { 'tomcat' }
from { 'TomcatUnpacked' }
from { 'war' }
}
}
}
distZip.dependsOn explodeTomcat
install.dependsOn distZip
Also, the top level project applies the 'java' and 'maven' plugin to all sub projects.
The error is:
FAILURE: Build failed with an exception.
* Where:
Build file '/kwork/wkcopy/e2amA/assembly/build.gradle' line: 25
* What went wrong:
A problem occurred evaluating project ':assembly'.
> Could not resolve all dependencies for configuration ':assembly:compile'.
> Could not find com.e2open.platform:e2am:9.0-SNAPSHOT.
Required by:
com.e2open.platform:assembly:9.0-SNAPSHOT
Can someone point me to documentation or a discussion that gets me farther down the road? Thanks!
You should take a look at Multi-project Builds - Project lib dependencies in the Gradle documentation.
Something like this should work:
dependencies{
compile project(:webapps/e2am)
compile group: 'com.e2open.platform.third-party.tomcat', name: 'tomcat-assembly',version:'7.0.42-1002', classifier:'bin', ext:'zip'
}
There's also an example that looks a lot like what you are trying to do - might be worth a look :)

Resources