Hi!
I’m completely new with Gradle. For beginning, I am trying to create a build script for simple console Java application, without using java plugin. There are only two dependencies in my code from maven repository.
So, my build contains one task, which extends JavaCompile, in which I specify classpath, destination, ect.
My project doesn’t apply java plugin, therefore I need to create dependency configuration by my own, like this:
Then I can bind dependencies on this configuration:
dependencies {
myCompile group: ‘org.apache.logging.log4j’, name: ‘log4j-api’, version: '2.7’
myCompile group: ‘org.apache.logging.log4j’, name: ‘log4j-core’, version: ‘2.7’
}
As far as i understand, after that I must bind those configuration with my compile action, but I can’t figure out how. Can someone point this out to me?
Any help will be really appreciated)
May I ask why you are not using the java plugin? If you just want to have different sourceSets than main/test, then use the java-base plugin. It will automatically create a compile task and dependency configurations and wire them up correctly for each sourceSet you add.
The main reason for not using it is to truly understand gradle basics, just like when people start learning java with terminal only.
I think such approach make sense, because if you get through making build without plugin, you will get into syntax, logic, and understanding how real plugins are written inside (as i understand, the are made with gradle).
That’s why im trying to figure this out)
If bottom-up is your favorite learning approach, then I’d suggest looking at the DSL reference and JavaDoc and how the JavaBasePlugin is implemented. To your specific question: You just set myCompileTask.classpath = configurations.myCompile.
Eventually, I used this task to resolve dependency. In myCompile task, which extends JavaCompile, I specified classpath as project.tasks.resolveCompileDependency. Is this ok?
task resolveCompileDependency(type: Copy) {
//download libraries specified in dependency configuration
def lib_files = configurations.myCompile.resolve()
//print names of downloaded files and copy to build directory (optional)
lib_files.each {File file ->
println file.name
}
//save jars, so that it would be possible to run it
from lib_files
into ‘build/classes’
// put jars in output of the task, so that they can be used in myCompile task classpath
outputs.files(lib_files)
}
No, I don’t understand why you would do that. See my simple solution above. Also, the code you showed resolves the dependencies at configuration time, which is a no-go.
Thanks, your simple solution worked. I came up with my way of doing this after one and a half hour of reading documentation.
Frankly speaking, I feel really off the rails by the fact that Gradle resolve configuration as a classpath so easily, though I remember that it implements FileCollection. I quickly went through the same doc’s pages, but didn’t find a place which would clarified this for me, maybe because it’s 2 a.m. already.
Thanks for advice, I will surely check java base plugin implementation.