Unable to build java files!


(insaneyogi) #1

New to gradle. I was trying to generate a build from my existing code base using the following code, gradle.build file:

apply plugin: “java” apply plugin: “maven”

group = “myorg” version = 1.0

repositories {

mavenCentral() }

sourceSets.all { set ->

def jarTask = task("${set.name}Jar", type: Jar) {

baseName = baseName + “-$set.name

from set.output

}

artifacts {

archives jarTask

} }

sourceSets {

java

javaLib

}

dependencies {

compile group: ‘commons-collections’, name: ‘commons-collections’, version: ‘3.2’

}

jar {

from sourceSets.java.output

}

The idea is that I am trying to generate a jar file for my build project , however I see that the jar files are turning up empty & not containing .class files like i would expect. Is there something i am doing wrong here with my configuration?Please let me know


(Chris Marks) #2

I think I’m a bit lost with what you posted. If I assume you are trying to just build the project and create a jar and your files are in the standard Maven directory structure, then I expect your build file to look more like the following:

apply plugin: 'java'
  group = "myorg"
 version = 1.0
  repositories {
   mavenCentral()
 }
  dependencies{
 compile 'commons-collections:commons-collections:3.2'
}

Then you can simply run either

gradle assemble

or

gradle build

and your jar file will be in build/libs.


(insaneyogi) #3

Thank you Chris your response is much appreciated. However I had a follow up question. While my build folder contains the gradle.build that I would like to use for doing the build compile . The actual files that I want to compile reside in a top level directory called java & the dependencies for those classes exist at the same level in the ‘javalib’ folder.

so pictorially it looks like this

->build (contains libs & temp where the jar file will be deposited) ->java (contains the core java files that are target of compilation) ->javalib (contains dependency jars) ->build.gradle (The build gradle file)

How do I ensure that the java files in the java folder get picked for compilation?

Your help is appreciated.

With regards S


(Chris Marks) #4

For the source file question, see chapter 23.4.1 of the user guide.

Essentially, you would add to build.gradle:

sourceSets {
    main {
        java {
            srcDir 'java'
        }
    }
}

For the question on the libs, does javalib contain the libraries you want to use, or libraries you want to add to some sort of package. If to use, I recommend reading chapter 50 of the user guide, with 50.4.4 presenting an example of what you are looking for. If to package, you may want to look into the Distribution plugin (see chapter 44) or the Maven Publish plugin (see chapter 65) depending on your requirements.

HTH, Chris


(insaneyogi) #5

Thanks for the pointers Chris, As regards libraries it is the former namely it is the libraries I want to use to compile the code. Given that my build file comes down to :

apply plugin: ‘java’ version = 1.0

repositories {

mavenCentral()

}

sourceSets {

main {

java {

srcDir ‘java’

}

} }

dependencies {

runtime files(‘javalib/commons-codec-1.6.jar’, ‘javalib/commons-discovery-0.2’)

runtime fileTree(dir:‘javalib’ ,include: ‘*.jar’) }

compileJava.destinationDir = file("$buildDir/output/classes")

However even after that I seem to be erroring out , it seems to me that I might be missing a directory setting because the compile job is not able to find the jars at all.

Here is the output when I run ‘gradle assemble’ or ‘gradle build’

symbol:

class AbstractJdbcType

location: class TypedColumn D:\Users\s.s\workspace\DCent\etsvn\java\CassandraJDBC\src\org\ apache\cassandra\cql\jdbc\TypedColumn.java:44: error: cannot find symbol

public TypedColumn(Column column, AbstractJdbcType<?> comparator, AbstractJd bcType<?> validator)

^

symbol:

class Column

location: class TypedColumn Note: Some input files use or override a deprecated API. Note: Recompile with -Xlint:deprecation for details. Note: Some input files use unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. 100 errors :compileJava FAILED

I can gurantee that Cassandra specific jar files lives in javalib directory. Is there another trick I am missing in my configuration.

Your input much appreciated.

With Regards S


(Chris Marks) #6

Your dependencies are declared with the “runtime” configuration, which means you indicated the jars are required to run your application, but not required to compile your app. Change the configuration to “compile” and the jars will be available to the compiler.


(insaneyogi) #7

Thanks for the tip Chris that did generate the class files, I now will have to look into building a jar out of them which I think should be specified in the documentation. If I run into any issues I will be sure to reach out.

Thanks for helping me in my initial baby steps in understanding Gradle.

-S