My build.gradle file looks like below.
apply plugin: 'java'
repositories {
maven {
mavenCentral()
}
dependencies {
compile "commons-logging:commons-logging:1.0.4",
"commons-codec:commons-codec:1.3"
}
jar {
manifest{
attributes ("Product-Name" : project.name, "${manifestSectionName}")
attributes ("Product-Display-Name" : project.description, "${manifestSectionName}")
}
}
task build1 {
sourceSets {
main {
java {
srcDirs = ['src/java/com/abc/xyz/dir1']
}
}
}
buildDir = 'op'
jar{
archiveName = 'build1.jar'
}
}
task build2 {
sourceSets {
main {
java {
srcDirs = ['src/java/com/abc/xyz/dir2']
}
}
}
buildDir = 'op'
jar{
archiveName = 'build2.jar'
}
}
So now I want to call individual task to build individual jar, the way we invoke individual targets in Ant.
So when I am calling it like : gw clean build build2
its building build2.jar in correct way, but when I am running it like: gw clean build build1, its still building build2.jar only.
I am very new to gradle, so not sure about this.
Its still building build2.jar only, I am not sure why its happening .. can someone help me here?
Note that both tasks are independent of each other.
When configuring build1 and build2, you're configuring the unique, shared, main sourceSet and the unique, shared, jar task. Since build2 is configured after build1, it overrides the configuration set by build1.
What you want instead is two additional tasks in the build, which both create a jar with a different content:
task build1(type: Jar) {
dependsOn classes
archiveName = 'build1.jar'
from(sourceSets.main.output) {
include '**/package1/**/*'
}
}
task build2(type: Jar) {
dependsOn classes
archiveName = 'build2.jar'
from(sourceSets.main.output) {
include '**/package2/**/*'
}
}
But that looks to me like bad design. If you want to generate different jars, then you should probably have several subprojects, all using the java plugin and not needing any configuration.
Related
I want to run QueryDsl Q-type generation in a separate task. I want that Q-type classes not to be created in a regular compileJava task, but the compiler sees the AnnotationProcessor in the classpath and creates them itself. I tried nulling the annotationProcessorPath, but then I couldn't restore its configuration in a separate task.
May be is it possible to exclude somehow dependencies of subtasks from the classpath?
compileJava {
options.annotationProcessorPath = null
}
tasks.register('generateQTypes'){
group 'build'
description 'Generate Q-Type classes with QueryDsl library'
dependencies {
annotationProcessor(
'com.querydsl:querydsl-apt:4.1.4:jpa',
'javax.persistence:javax.persistence-api:2.2',
'javax.annotation:javax.annotation-api:1.3.1')
}
compileJava {
options.annotationProcessorPath = classpath
}
}
What is the best way to solve this problem?Thanks in advance!
I made the following solution
class QTypeGenerator extends DefaultTask {
#TaskAction
addDependencies() {
project.dependencies {
annotationProcessor(
'com.querydsl:querydsl-apt:4.1.4:jpa',
'javax.persistence:javax.persistence-api:2.2',
'javax.annotation:javax.annotation-api:1.3.1')
}
}
}
tasks.register("generateQTypeClasses", QTypeGenerator) {
group('build')
description('Generate Q-type classes by queryDsl in build directory with default path')
finalizedBy('compileJava')
doLast {
println("Q-types classes will gererated and stored in ${compileJava.options.annotationProcessorGeneratedSourcesDirectory}")
}
}
I have a distributes projects with different sub-projects and I want to accomplish the following:
(root)
client
module A
module B
module C
model
I want to put
protoc {
artifact = 'com.google.protobuf:protoc:3.5.0'
}
plugins {
grpc {
artifact = "io.grpc:protoc-gen-grpc-java:1.7.0"
}
}
generateProtoTasks {
all()*.plugins {
grpc {}
}
} }
dependencies {
compile "com.google.api.grpc:proto-google-common-protos:1.0.0"
compile "io.grpc:grpc-netty:1.7.0"
compile "io.grpc:grpc-protobuf:1.7.0"
compile "io.grpc:grpc-stub:1.7.0"
}
for module A, B and C.
For now I have the following in my root build.gradle
subprojects{
apply plugin: 'java'
sourceCompatibility = 1.8
group 'project'
version '0.0.1-SNAPSHOT'
jar {
manifest {
attributes 'Main-Class': "com.project.${project.name}.App"
}
doFirst {
from { configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) } }
}
}
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.9.5'
}
}
So every sub-project use java plugin, and has the defines dependencies and jar task.
How can I only put the first block for some sub-projects ?
I tried using a variable like in Multi-project Builds - Gradle but I couldn't access it in subprojects block.
Thank you in advance. I'm really interested in using Gradle correctly and it's a bit hard to get into it outside of simple Android/Java projects. Feel free to include any documentations I should read :)
Edit:
Thank you. I wouldn't have posted here if I hadn't search before. Apparently I was missing the keyword "subset" who would have gave me the solution you linked.
A solution is described here: https://discuss.gradle.org/t/configure-only-subset-of-subprojects/5379/3
You can run configure() with a list of projects.
project.ext {
subprojectList = subprojects.findAll{
it.name == 'subprojectA' ||
it.name == 'subprojectB' ||
it.name == 'subprojectC'
}
}
configure(project.subprojectList) {
// insert your custom configuration code
}
or
configure([project(':a'), project(':b'), project(':c')]) {
// configuration
}
When creating a task with the name jar, Gradle automatically knows that the class of the task is org.gradle.api.tasks.bundling.Jar. How can I replicate this with my custom task?
i.e. I have the following class:
class MyTaskType extends DefaultTask {
#Input String name
// Options
#TaskAction
def generateImage() {
// Stuff
}
}
Up until now I've been doing the following:
task veryCoolTaskName(type:MyTaskType) {
name 'some-name'
}
And I want to be able to define a task of this type by writing:
myTaskType {
name 'some-name'
}
Just like with jar {...}. How can I do that?
This is not how Gradle works.
When writing jar { ... }, you don't actually create a new task. Instead, you are refererring to an existing task jar that was created by a plugin (e.g. the Java plugin):
plugins {
id 'java'
}
jar {
// This works because the task was created by the plugin
}
Without any plugin applied, the same excerpt will fail, as a task named jar does not exist:
jar {
// This fails, because the task `jar` does not exist
}
Instead, you need to create the task to configure it:
task jar(type: Jar)
jar {
// This works because the task was created beforehand
}
Of course, the creation and the configuration of the task may be combined:
task jar(type: Jar) {
// ...
}
I've got two sibling projects ProjectA and ProjectB that are both under Parent. Parent is basically only a folder, and has common build.gradle settings for both child projects.
ProjectB depends on code in ProjectA at compile time, but ProjectA is built separately and
contains a META-INF directory. When building ProjectB I get a java.lang.SecurityException: Invalid signature file digest for Manifest main attributes. As you can see below, I've removed the zipTree calls from ProjectB and am unsure of how to fix this problem. Would greatly appreciate some assistance.
Please keep in mind, both projects must build their own JAR and ProjectA must shade the two dependencies seen below.
Parent settings.gradle:
rootProject.name = "Parent"
include ":ProjectA", ":ProjectB"
Parent build.gradle:
allprojects {
buildscript {
repositories {
jcenter()
maven {
name = "forge"
url = "https://files.minecraftforge.net/maven"
}
maven {
name = "sponge"
url = "https://repo.spongepowered.org/maven"
}
}
dependencies {
classpath "net.minecraftforge.gradle:ForgeGradle:2.3-SNAPSHOT"
classpath "org.spongepowered:mixingradle:0.6-SNAPSHOT"
}
}
repositories {
mavenCentral()
maven {
name = 'spongepowered-repo'
url = 'https://repo.spongepowered.org/maven'
}
maven {
name = 'jitpack-repo'
url = 'https://jitpack.io'
}
}
configurations {
shade
compile.extendsFrom(shade)
}
}
ProjectA build.gradle:
apply plugin: "net.minecraftforge.gradle.forge"
apply plugin: 'org.spongepowered.mixin'
version = project.modVersion
group = project.modGroup
minecraft {
version = "${project.mcVersion}-${project.forgeVersion}"
runDir = "run"
// the mappings can be changed at any time, and must be in the following format.
// snapshot_YYYYMMDD snapshot are built nightly.
// stable_# stables are built at the discretion of the MCP team.
// Use non-default mappings at your own risk. they may not always work.
// simply re-run your setup task after changing the mappings to update your workspace.
mappings = project.mcpVersion
// makeObfSourceJar = false // an Srg named sources jar is made by default. uncomment this to disable.
replace("#MOD_VERSION#", project.modVersion)
replace("#MOD_ID#", project.modId)
replace("#MOD_NAME#", project.modBaseName)
replace("#MOD_ACCEPTED#", "[${project.modAcceptedVersions}]")
replaceIn "${project.modBaseName}.java"
}
mixin {
defaultObfuscationEnv searge
add sourceSets.main, "mixins.${project.modId}.refmap.json"
}
dependencies {
shade("org.spongepowered:mixin:0.7.11-SNAPSHOT") {
// Mixin includes a lot of dependencies that are too up-to-date
exclude module: 'launchwrapper'
exclude module: 'guava'
exclude module: 'gson'
exclude module: 'commons-io'
exclude module: 'log4j-core'
}
shade group: 'org.yaml', name: 'snakeyaml', version: '1.6'
}
jar {
from(configurations.shade.collect { it.isDirectory() ? it : zipTree(it) })
//from (configurations.provided.collect { entry -> zipTree(entry) })
manifest {
attributes(
'FMLAT': "${project.modId}_at.cfg",
'MixinConfigs': "mixins.${project.modId}.json",
'TweakOrder': '0',
'TweakClass': "${project.modGroup}.${project.modId}.tweaker.${project.modBaseName}Tweaker",
'Main-Class': 'OpenErrorMessage'
)
}
}
processResources {
// this will ensure that this task is redone when the versions change.
inputs.property "version", project.version
inputs.property "mcversion", project.minecraft.version
// replace stuff in mcmod.info, nothing else
from(sourceSets.main.resources.srcDirs) {
include "**/*.info"
include "**/*.properties"
// replace version and mcversion
expand "version": project.version, "mcversion": project.minecraft.version
}
// copy everything else, thats not the mcmod.info
from(sourceSets.main.resources.srcDirs) {
exclude "**/*.info"
exclude "**/*.properties"
}
}
ProjectB build.gradle:
apply plugin: 'net.minecraftforge.gradle.forge'
apply plugin: 'org.spongepowered.mixin'
version = project.modVersion
group = project.modGroup
minecraft {
version = "${project.mcVersion}-${project.forgeVersion}"
runDir = "run"
// the mappings can be changed at any time, and must be in the following format.
// snapshot_YYYYMMDD snapshot are built nightly.
// stable_# stables are built at the discretion of the MCP team.
// Use non-default mappings at your own risk. they may not always work.
// simply re-run your setup task after changing the mappings to update your workspace.
mappings = project.mcpVersion
// makeObfSourceJar = false // an Srg named sources jar is made by default. uncomment this to disable.
replace("#MOD_VERSION#", project.modVersion)
replace("#MOD_ID#", project.modId)
replace("#MOD_NAME#", project.modBaseName)
replace("#MOD_ACCEPTED#", "[${project.modAcceptedVersions}]")
replaceIn "${project.modBaseName}.java"
}
mixin {
defaultObfuscationEnv searge
add sourceSets.main, "mixins.${project.modId}.refmap.json"
}
dependencies {
/*shade("org.spongepowered:mixin:0.7.11-SNAPSHOT") {
// Mixin includes a lot of dependencies that are too up-to-date
exclude module: 'launchwrapper'
exclude module: 'guava'
exclude module: 'gson'
exclude module: 'commons-io'
exclude module: 'log4j-core'
}*/
compile project(":SkyblockRecords")
}
jar {
archiveName = "${project.modBaseName}-${project.version}-for-MC-1.12.x.jar"
/*from(configurations.shade.collect { it.isDirectory() ? it : zipTree(it) }) {
exclude 'META-INF', 'META-INF/**'
}*/
manifest {
attributes(
'FMLAT': "${project.modId}_at.cfg",
'MixinConfigs': "mixins.${project.modId}.json",
'TweakOrder': '0',
'TweakClass': "${project.modGroup}.${project.modId}.tweaker.${project.modBaseName}Tweaker",
'Main-Class': 'OpenErrorMessage'
)
}
}
processResources {
// this will ensure that this task is redone when the versions change.
inputs.property "version", project.version
inputs.property "mcversion", project.minecraft.version
// replace stuff in mcmod.info, nothing else
from(sourceSets.main.resources.srcDirs) {
include "**/*.info"
include "**/*.properties"
// replace version and mcversion
expand "version": project.version, "mcversion": project.minecraft.version
}
// copy everything else, thats not the mcmod.info
from(sourceSets.main.resources.srcDirs) {
exclude "**/*.info"
exclude "**/*.properties"
}
}
Thanks to a comment by Bjorn Vester on the question, I've solved the problem. The answer is to move the jar's configurations shade collect call to ProjectA.
// Move this to the jar section of ProjectA
from(configurations.shade.collect { it.isDirectory() ? it : zipTree(it) }) {
exclude 'META-INF', 'META-INF/**'
}
I have a multi-build Java project being built with Gradle.
To hide Illegal reflective access warnings I want to find JavaExec and Test tasks within several projects and provide the required JVM arguments. Rather than add these to each project, I want to apply this to all projects in the root build.gradle.
How do I find certain tasks of a certain type throughout all projects?
My intial attempt uses withType however I want to remove the duplication of repeating this for the JavaExec and Test task types. See below:
Attempt #1
allprojects {
repositories {
jcenter()
}
afterEvaluate {
project.tasks.withType(JavaExec) {
it.jvmArgs "--add-opens=java.base/java.lang=ALL-UNNAMED"
}
project.tasks.withType(Test) {
it.jvmArgs "--add-opens=java.base/java.lang=ALL-UNNAMED"
}
}
}
subprojects {
version = "0.0.1-SNAPSHOT"
}
Try this :
afterEvaluate {
[JavaExec, Test].each {
project.tasks.withType(it) {
it.jvmArgs "--add-opens=java.base/java.lang=ALL-UNNAMED"
}
}
}