How to copy some buildscript classpath JARs but exclude others in Gradle? - gradle

I have buildscript block that references many JARs:
buildscript {
dependencies { classpath "com.github.kulya:jmeter-gradle-plugin:1.3.4-2.13" }
dependencies { classpath "com.eriwen:gradle-css-plugin:2.14.0" }
dependencies { classpath "com.eriwen:gradle-js-plugin:2.14.1" }
dependencies { classpath "com.google.javascript:closure-compiler:v20160208" }
dependencies { classpath "org.akhikhl.gretty:gretty:+" }
}
task copyWarGradlePlugins(type: Copy) {
destinationDir = file(project.buildDir.name + '/warGradlePlugins')
from (buildscript.configurations.classpath)
}
I want to copy (to my WAR file) some of the referenced JARs (e.g. dependencies of gradle-css-plugin, gradle-js-plugin, closure-compiler) but not others (e.g. gretty, jmeter-gradle-plugin).
How do I exclude for example jmeter-gradle-plugin and all its dependencies?
UPDATE:
There are 150+ JARs. Excluding them by file name is not practical/maintainable.

buildscript {
dependencies { classpath "com.github.kulya:jmeter-gradle-plugin:1.3.4-2.13" }
dependencies { classpath "com.eriwen:gradle-css-plugin:2.14.0" }
dependencies { classpath "com.eriwen:gradle-js-plugin:2.14.1" }
dependencies { classpath "com.google.javascript:closure-compiler:v20160208" }
dependencies { classpath "org.akhikhl.gretty:gretty:+" }
}
task copyWarGradlePlugins(type: Copy) { copy ->
destinationDir = file(project.buildDir.name + '/warGradlePlugins')
copy.from(buildscript.configurations.classpath) { copySpec ->
// exclusions are based on the file name as here you will be handed
// DefaultFileVisitDetails type
copySpec.exclude { it.name.startsWith 'gretty' }
copySpec.exclude { it.name.startsWith 'closure-compiler' }
}
}

Related

Could not find com.github.searchy2:CustomButton:1.9.0. ERROR

My build.gradle file looks like this:
buildscript {
repositories {
google()
mavenCentral()
gradlePluginPortal()
maven { url 'https://jitpack.io' }
maven { url 'https://maven.google.com' }
maven { url 'https://plugins.gradle.org/m2/' }
jcenter() // Warning: this repository is going to shut down soon
}
dependencies {
classpath "com.android.tools.build:gradle:7.0.4"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10"
classpath 'com.google.gms:google-services:4.3.10'
classpath 'gradle.plugin.com.onesignal:onesignal-gradle-plugin:[0.12.10, 0.99.99]'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
ERROR
Could not find com.github.searchy2:CustomButton:1.9.0.
Required by:
project :app > com.github.searchy2:CustomPermissionsDialogue:2.2.1

How to run jaxb xjc task with gradle?

I picked up 3 gradle plugins for jaxb/xjc and none of them worked for me.
For each case, I've put the maven model xsd maven-4.0.0.xsd https://maven.apache.org/xsd/maven-4.0.0.xsd in src/main/xsd. (it's just an example, any other xsd will do)
no.entitas.gradle.jaxb:gradle-jaxb-plugin:2.0
http://github.com/stianh/gradle-jaxb-plugin
This plugin looks for xsd files in src/<source set>/xsd/**/*.xsd.
Here is the build.gradle:
apply plugin: 'jaxb'
dependencies {
jaxb 'com.sun.xml.bind:jaxb-xjc:2.2.4-1'
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'no.entitas.gradle.jaxb:gradle-jaxb-plugin:2.0'
}
}
Error:
* What went wrong:
A problem occurred evaluating root project 'test-gradle'.
> Failed to apply plugin [id 'jaxb']
> Could not find method add() for arguments [jaxb, no.entitas.gradle.jaxb.plugin.JaxbPlugin$_apply_closure1#62c72501] on configuration container.
org.hibernate.build.gradle:gradle-xjc-plugin:1.0.2.Final
https://github.com/hibernate/gradle-xjc-plugin
Here is the build.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.hibernate.build.gradle:gradle-xjc-plugin:1.0.2.Final'
}
}
apply plugin: 'org.hibernate.build.gradle.xjc'
dependencies {
xjc 'org.glassfish.jaxb:jaxb-xjc:2.2.11'
xjc 'org.jvnet.jaxb2_commons:jaxb2-basics:0.9.3'
xjc 'org.jvnet.jaxb2_commons:jaxb2-basics-ant:0.9.3'
}
xjc {
xjcTaskName = 'org.jvnet.jaxb2_commons.xjc.XJC2Task'
// access the schemas NamedDomainObjectContainer
schemas {
// and add a new SchemaDescriptor to it under the name 'cfg'
cfg {
// and now, configure the SchemaDescriptor
xsd = file( 'src/main/xsd/maven-4.0.0.xsd')
}
}
}
Error:
* What went wrong:
Neither path nor baseDir may be null or empty string. path='null' basedir='C:\Users\xxxxx\eclipse-workspace\test-gradle'
com.intershop.gradle.jaxb:jaxb-gradle-plugin:5.1.0
https://github.com/IntershopCommunicationsAG/jaxb-gradle-plugin
Here is the build.gradle:
plugins {
id 'java'
id 'com.intershop.gradle.jaxb' version '5.1.0'
}
jaxb {
javaGen {
posConfig {
packageName = 'org.apache.maven.model'
schema = file('src/main/xsd/maven-4.0.0.xsd')
}
}
}
repositories {
mavenCentral()
}
dependencies {
jaxb 'com.sun.xml.bind:jaxb-xjc:3.0.1'
jaxb 'com.sun.xml.bind:jaxb-jxc:3.0.1'
jaxb 'com.sun.xml.bind:jaxb-impl:3.0.1'
jaxb 'com.sun.xml.bind:jaxb-core:3.0.1'
jaxb 'org.glassfish.jaxb:jaxb-runtime:3.0.1'
jaxb 'jakarta.xml.bind:jakarta.xml.bind-api:3.0.1'
jaxb 'jakarta.activation:jakarta.activation-api:2.0.1'
}
Error:
* What went wrong:
An exception occurred applying plugin request [id: 'com.intershop.gradle.jaxb', version: '5.1.0']
> Failed to apply plugin [id 'com.intershop.gradle.jaxb']
> Could not generate a proxy class for class com.intershop.gradle.jaxb.extension.JaxbExtension.
EDIT
Gradle version : 2.3 (it's the version that I must use)
JDK version : 1.8.0_311
Command : gradle build
Here is an example without using specific plugins (just java), and v4 of jaxb-xjc:
(config expects maven-4.0.0.xsd to be in src/main/resources)
plugins {
id 'java'
}
group 'com.example.xjc-demo'
archivesBaseName = "xjcdemo"
version '1.0.0-SNAPSHOT'
repositories {
mavenCentral()
}
def generated_dir = "src/main/java"
sourceSets {
generated {
java.srcDir "$generated_dir"
}
}
// JAXB configuration holds classpath for running the JAXB XJC compiler
configurations {
jaxb
}
dependencies {
jaxb "org.glassfish.jaxb:jaxb-xjc:4.0.1"
jaxb "org.glassfish.jaxb:jaxb-runtime:4.0.1"
// The next two dependencies are not necessary for generating code, only when running the code:
// Generated code depends on the JAXB API, which is removed from base Java in JDK 11, and therefore needs to be added
implementation "org.glassfish.jaxb:jaxb-runtime:4.0.1"
implementation 'jakarta.xml.bind:jakarta.xml.bind-api:4.0.0' // In JAXB v3, 'javax.xml.bind' was moved to 'jakarta.xml.bind'
}
// Cookie cutter function for defining multiple XJC tasks
def addXjcTask(taskName, schema, pkg, dest) {
// If you haven't already, create the generated output dir before running XJC or it will fail
file(dest).mkdirs()
// The main XJC task, calls XJCFacade which is the entry point of the XJC JAR
tasks.create(name: taskName, type: JavaExec) {
classpath configurations.jaxb
mainClass = 'com.sun.tools.xjc.XJCFacade'
// To explore available args, download the XJC JAR and run java -jar jaxb-xjc.jar --help
args schema, "-p", pkg, "-d", dest, "-no-header"
}
}
// Add all the XJC tasks you need
addXjcTask("maven-xsd",
"src/main/resources/maven-4.0.0.xsd",
"${archivesBaseName}.my_maven_package_name",
"$generated_dir")
tasks.register("generateSources") {
description "Generate Java sources fram XSD files"
dependsOn tasks.getByName("maven-xsd")
}
Run with ./gradlew generateSources (verified with Gradle v7.4.2).
I found 2 other plugins.
The jacobono/gradle-jaxb-plugin uses relative directories and creates the destination directory :
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "com.github.jacobono:gradle-jaxb-plugin:1.3.5"
}
}
apply plugin: "com.github.jacobono.jaxb"
repositories {
mavenCentral()
}
sourceSets {
main {
java {
srcDirs = ["${project.buildDir}/generated-sources/xjc",'src/main/java']
}
resources {
srcDirs = ['src/main/resources']
}
}
}
dependencies {
jaxb 'org.jvnet.jaxb2_commons:jaxb2-basics-ant:1.11.1'
jaxb 'org.jvnet.jaxb2_commons:jaxb2-basics:1.11.1'
jaxb 'org.jvnet.jaxb2_commons:jaxb2-basics-annotate:1.1.0'
jaxb 'org.glassfish.jaxb:jaxb-xjc:2.2.11'
jaxb 'org.glassfish.jaxb:jaxb-runtime:2.2.11'
jaxb 'javax.xml.bind:jaxb-api:2.2.11'
jaxb 'org.slf4j:slf4j-nop:1.7.30'
}
jaxb {
xsdDir = "src/main/xsd"
xjc {
taskClassname = "org.jvnet.jaxb2_commons.xjc.XJC2Task"
generatePackage = "org.apache.maven.model"
destinationDir = "build/generated-sources/xjc"
args = ["-Xinheritance", "-Xannotate"]
}
}
compileJava.dependsOn 'xjc'
The rackerlabs/gradle-jaxb-plugin uses absolute directories and creates the destination directory only if it's the default one ${project.buildDir}/generated-sources/xjc:
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath 'gradle.plugin.org.openrepose:gradle-jaxb-plugin:2.5.0'
}
}
apply plugin: "org.openrepose.gradle.plugins.jaxb"
repositories {
mavenCentral()
}
sourceSets {
main {
java {
srcDirs = ["${project.buildDir}/generated-sources/xjc",'src/main/java']
}
resources {
srcDirs = ['src/main/resources']
}
}
}
dependencies {
jaxb 'org.jvnet.jaxb2_commons:jaxb2-basics-ant:1.11.1'
jaxb 'org.jvnet.jaxb2_commons:jaxb2-basics:1.11.1'
jaxb 'org.jvnet.jaxb2_commons:jaxb2-basics-annotate:1.1.0'
jaxb 'org.glassfish.jaxb:jaxb-xjc:2.2.11'
jaxb 'org.glassfish.jaxb:jaxb-runtime:2.2.11'
jaxb 'javax.xml.bind:jaxb-api:2.2.11'
jaxb 'org.slf4j:slf4j-nop:1.7.30'
}
jaxb {
xsdDir = "${project.projectDir}/src/main/xsd"
xjc {
generateEpisodeFiles = false
taskClassname = "org.jvnet.jaxb2_commons.xjc.XJC2Task"
generatePackage = "org.apache.maven.model"
args = ["-Xinheritance", "-Xannotate"]
}
}
compileJava.dependsOn 'xjc'

Gradle | Configuration with name 'complileClasspath' not found. for multi-level project build

I am trying to build a multi-project build in Gradle using Kotlin DSL.
Root build.gradle.kts:
allprojects {
repositories {
jcenter()
google()
}
}
subprojects{
version = "1.0"
}
plugins {
java
application
}
repositories {
jcenter()
}
dependencies {
implementation("com.google.guava:guava:27.0.1-jre")
testImplementation("junit:junit:4.12")
}
application {
mainClassName = "gradle.youtube.series.App"
}
gitutils subproject's build.gradle.kts
plugins {
`java-library`
}
repositories {
mavenCentral()
}
dependencies {
implementation("org.sl4j:sl4j-api:1.7.2")
implementation("org.eclipse.jgit:org.eclipse.jgit:5.3.0.201903130848-r")
testImplementation("org.junit.jupiter:junit-jupiter:5.4.1")
testRuntimeOnly("org.sl4j:sl4j-simple:1.7.2")
}
tasks.test {
useJUnitPlatform()
}
I am trying to download gradeutil's dependencies using the following command:
./gradlew gitutils:dependencies --configuration complileClasspath
I am getting the following error:
Configuration with name 'complileClasspath' not found.
Is compileClassPath required for multi-level gradle project? What is its use?

how to create fat jar which does not overwrite entries under META-INF/services

I have the following gradle build config:
plugins {
id 'com.github.johnrengelman.shadow' version '1.2.3'
}
group 'abc'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'application'
mainClassName = "abc.Driver"
repositories {
mavenCentral()
}
dependencies {
compile (group: 'org.apache.hadoop', name: 'hadoop-client', version: '2.6.0')
}
sourceSets {
main {
java {
srcDir './src'
}
}
}
jar {
manifest {
attributes(
'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
'Main-Class': mainClassName
)
}
}
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'Gradle Jar File Example',
'Implementation-Version': version,
'Main-Class': mainClassName
}
baseName = project.name + '-all'
from { (configurations.compile - configurations.provided).collect
{
//println it.getName()
it.isDirectory() ? it : zipTree(it)
}
}
{
exclude "META-INF/*.SF"
exclude "META-INF/*.DSA"
exclude "META-INF/*.RSA"
}
with jar
}
The main method that I have is just the following piece of code:
public static void main(String[] args) {
Iterable<ClientProtocolProvider> frameworkLoader =
ServiceLoader.load(ClientProtocolProvider.class);
for(ClientProtocolProvider cpp: frameworkLoader) {
System.out.println(cpp.toString());
}
}
When I run the main method from IDE as expected I get the following output:
org.apache.hadoop.mapred.YarnClientProtocolProvider#4783da3f
org.apache.hadoop.mapred.LocalClientProtocolProvider#300ffa5d
But when I run the gradle fat jar task and I create the fat jar, after running the main method using (java -jar) through terminal I just get:
org.apache.hadoop.mapred.LocalClientProtocolProvider#7f31245a
I found that when fat jar is created, the entries under META-INF/services are merged for all dependencies and hence I lose the declaration for YarnClientProtocolProvider which I need further in my code.
YarnClientProtocolProvider is declared in hadoop-mapreduce-client-jobclient.jar
LocalClientProtocolProvider is declared in hadoop-mapreduce-client-common.jar
Does any body know how to create a fat jar which does not merges entries under META-INF/services?!
This should do the work
shadowJar {
mergeServiceFiles()
}

Publish test jar with dependencies in gradle

I wrote the tests for my project under src/integTests directory and now I have to build and publish only jar generated out of integ tests. Here is my code:
apply plugin: 'java'
apply plugin: 'maven-publish'
sourceSets {
integTest {
java.srcDir file('src/integTest/java')
resources.srcDir file('src/integTest/resources')
compileClasspath += main.output + test.output
runtimeClasspath += main.output + test.output
}
}
configurations {
integTestCompile.extendsFrom testCompile
integTestRuntime.extendsFrom testRuntime
}
task integTestJar (type:Jar) {
from sourceSets.integTest.output
appendix = 'integ-tests'
}
publishing {
publications {
myPublicationName(MavenPublication) {
artifactId "client-security-internal-integ-tests"
from components.java
}
}
repositories {
maven {
url ="${artifactory_contextUrl}"
credentials {
username = "${artifactory_user}"
password = "${artifactory_password}"
}
}
}
}
In the above, when I have from components.java, it publishes the product's jar with name as specified in artifact-id. Instead when I use artifact integTestJar.archivePath, it publishes the right jar but does not include the dependencies info in the pom file.
I tried from components.integTest, but that fails with error Could not find property 'integTest' on SoftwareComponentInternal set
How do I publish the integ-test jar with all its dependencies included in pom file ?
Here is the solution which works!
pom.withXml {
def dependencies = asNode().appendNode('dependencies')
configurations.integTestRuntime.getResolvedConfiguration().getFirstLevelModuleDependencies().each {
def dependency = dependencies.appendNode('dependency')
dependency.appendNode('groupId', it.moduleGroup)
dependency.appendNode('artifactId', it.moduleName)
dependency.appendNode('version', it.moduleVersion)
}
}

Resources