I have a multi-project structure managed with gradle. All the java sub-projects have this jar
block:
def main_class = "foo.Main"
jar {
manifest {
attributes 'Main-Class': main_class
}
}
So I decided that I'm going to move the jar block to the root project and have only the def main_class
in the sub-projects, but no matter how I defined the main_class
variable, it didn't work. I tried project.ext
and properties {}
as well.
I had a task, called dist
which worked with project.ext
:
root project:
subprojects {
task dist(dependsOn: jar) << {
copy {
from('build/libs') {
include (build_jar)
}
into('.')
rename(build_jar, app_jar)
}
}
}
a sub-project:
project.ext {
build_jar = "..."
app_jar = "..."
}
How can I define a variable that works with jar
like with the dist
task?
You can just do:
ext {
main_class = 'com.Main'
}
subprojects {
apply plugin: 'java'
jar {
manifest {
attributes 'Main-Class': main_class
}
}
}
in PROJECT_ROOT/build.gradle
file.
To override extra properties in subproject's build.gradle you must put your desired task (in this case the jar task
) inside the afterEvaluate
block.
root project build.gradle:
subprojects {
apply plugin: 'java'
apply plugin: 'application'
ext {
// the property that should be overridden in suproject's build.gradle
main_class = ''
}
afterEvaluate {
jar {
manifest {
attributes 'Main-Class': main_class
}
}
}
}
sub project build.gradle:
mainClassName = 'project.specific.Main'
ext.set("main_class", mainClassName)
// you could also use this notation, but I prefer the first one
// since it is more clear on what it is doing
//main_class = mainClassName
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.