Publish test jar with dependencies in gradle - 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)
}
}

Related

Protobuf gradle plugin: how to avoid module affects?

I have multimodule gradle project. When I add com.google.protobuf to build.gradle - it affects to testIntegration module.
Without plugin:
With plugin:
And the real problem is - when I run Jenkins build job - it freezes on testIntegration. Without plugin - all works fine.
gradle.build
apply plugin:'com.google.protobuf'
def grpcVersion = '1.30.0'
dependencies {
...
implementation "io.grpc:grpc-netty:${grpcVersion}"
implementation "io.grpc:grpc-protobuf:${grpcVersion}"
implementation "io.grpc:grpc-stub:${grpcVersion}"
...
}
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.8.0"
}
plugins {
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:1.30.0'
}
}
generateProtoTasks {
all()*.plugins {
grpc {}
}
}
}
main-gradle.build
...
subprojects {
configurations {
compileOnly {
extendsFrom annotationProcessor
}
testIntegrationImplementation.extendsFrom(testImplementation)
testIntegrationRuntimeOnly.extendsFrom(testRuntimeOnly)
}
sourceSets {
testIntegration {
compileClasspath += sourceSets.main.output
runtimeClasspath += sourceSets.main.output
}
}
...

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?

gradle conflicting transitive dependency not being included

I have a dependency, my-project that uses two dependencies that use different versions of commons-lang3 but when I build my war artifact, commons-lang3 is not included in the artifact. What could be wrong?
My build.gradle looks like:
apply plugin: 'io.spring.dependency-management'
apply plugin: 'java'
apply plugin: 'maven-publish'
apply plugin: 'war'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'io.spring.gradle:dependency-management-plugin:0.3.0.RELEASE'
}
}
compileJava {
sourceCompatibility = 1.7
targetCompatibility = 1.7
}
configurations.all {
exclude group: 'commons-logging'
}
repositories {
jcenter()
maven {
credentials {
username = "${artifactory_user}"
password = "${artifactory_password}"
}
url "myrepo.com"
}
}
dependencyManagement {
imports {
mavenBom 'io.spring.platform:platform-bom:1.1.2.RELEASE'
}
}
dependencies {
compile "my.project:my-project:1.0.0-SNAPSHOT"
// Spring Framework
compile 'org.springframework:spring-context'
compile 'org.springframework:spring-web'
compile 'org.springframework:spring-webmvc'
compile 'org.springframework.security:spring-security-config'
compile 'org.springframework.security:spring-security-web'
// Jackson
compile "com.fasterxml.jackson.core:jackson-annotations"
compile "com.fasterxml.jackson.core:jackson-core"
compile "com.fasterxml.jackson.core:jackson-databind"
// Logging
compile 'ch.qos.logback:logback-classic'
compile 'org.slf4j:slf4j-api'
runtime 'org.slf4j:jcl-over-slf4j'
runtime 'org.logback-extensions:logback-ext-loggly:0.1.2'
// Test
testCompile 'junit:junit'
testCompile 'org.mockito:mockito-core'
testCompile 'org.springframework:spring-test'
providedCompile 'javax.servlet:javax.servlet-api'
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.web
}
}
repositories {
maven {
credentials {
username = "${artifactory_user}"
password = "${artifactory_password}"
}
if(project.version.endsWith('-SNAPSHOT')) {
url "myrepo.com/libs-snapshot-local"
} else {
url "myrepo.com/libs-release-local"
}
}
}
}
task wrapper(type: Wrapper) {
gradleVersion = '2.4'
}
EDIT Dependency diagram of my.project:my-project
my-project is a library project that my co-worker wrote that is dependent on two other library projects that contain commons-lang3. An example diagram would be
my.project:my-project:1.0.0-SNAPSHOT
+---my.project:my-dependency-1:1.0.0
| +---org.apache.commons:commons-lang3:3.4
+---my.project:my-dependency-2:1.0.0
+---org.apache.commons:commons-lang3:3.3.2
I updated the spring gradle dependency-management-plugin to 0.5.2.RELEASE and that fixed my problem.

Cargo use installed container (Local "C:\tomcat) instead of AppData/Local/Temp

I want to use local tomcat container which is already installed in c:\tomcat instead of cargo downloading container automatically in temp folder. Please help me here. What am i doing wrong?
Here is my build.gradle so far
group = "com.biw.hc"
version = "0.1.0_SNAPSHOT"
buildscript {
ext {
springBootVersion = "1.2.3.RELEASE"
tomcatVersion = "8.0.20"
logbackJaninoVersion = "2.7.8"
}
repositories {
jcenter()
maven { url "http://repo.spring.io/snapshot" }
maven { url "http://repo.spring.io/milestone" }
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
classpath "net.saliman:gradle-cobertura-plugin:2.2.7"
classpath "com.bmuschko:gradle-cargo-plugin:2.1"
}
}
apply plugin: "spring-boot"
apply plugin: "java"
apply plugin: "groovy"
apply plugin: 'war'
apply plugin: "eclipse"
apply plugin: "idea"
apply plugin: 'com.bmuschko.cargo'
apply plugin: "net.saliman.cobertura"
bootRepackage {
mainClass = 'com.biw.hc.admin.Application'
}
war {
archiveName 'hcadmin.war'
}
repositories {
mavenLocal()
jcenter()
maven { url "http://repo.spring.io/snapshot" }
maven { url "http://repo.spring.io/milestone" }
}
dependencies {
providedRuntime "org.apache.tomcat.embed:tomcat-embed-core"
providedRuntime "org.apache.tomcat.embed:tomcat-embed-el"
providedRuntime "org.apache.tomcat.embed:tomcat-embed-logging-juli"
providedRuntime "org.apache.tomcat.embed:tomcat-embed-websocket"
providedRuntime "org.apache.tomcat:tomcat-jdbc:${tomcatVersion}"
providedRuntime "org.apache.tomcat:tomcat-juli:${tomcatVersion}"
providedRuntime "org.apache.tomcat:tomcat-dbcp:${tomcatVersion}"
compile ("org.springframework.boot:spring-boot-starter-web:${springBootVersion}") {
exclude module: "spring-boot-starter-tomcat:${springBootVersion}"
}
compile "org.springframework.boot:spring-boot-starter-actuator:${springBootVersion}"
compile "org.springframework.boot:spring-boot-starter-thymeleaf:${springBootVersion}"
compile "org.springframework.boot:spring-boot-starter-data-jpa:${springBootVersion}"
compile("org.codehaus.janino:janino:${logbackJaninoVersion}")
// BIW dependencies...
compile "com.biw.hc:hc-core:0.1.0-SNAPSHOT"
// security dependencies...
compile "org.springframework.boot:spring-boot-starter-security:${springBootVersion}"
// testing dependencies...
testCompile "org.codehaus.groovy:groovy-all:2.2.0"
testCompile "org.spockframework:spock-core:0.7-groovy-2.0"
testCompile "org.spockframework:spock-spring:0.7-groovy-2.0"
testCompile "org.springframework:spring-test:2.5"
testCompile "org.springframework.boot:spring-boot-starter-test:${springBootVersion}"
compile('org.codehaus.groovy.modules.http-builder:http-builder:0.5.1')
def cargoVersion = '1.4.5'
cargo "org.codehaus.cargo:cargo-core-uberjar:$cargoVersion",
"org.codehaus.cargo:cargo-ant:$cargoVersion"
}
cargo {
containerId = 'tomcat8x'
port = 8080
deployable {
file = file('build/libs/hcadmin.war')
context = 'hcadmin'
}
local {
homeDir = file('C:\\apache-tomcat-8.0.21')
outputFile = file('build/output.log')
containerProperties {
property 'cargo.tomcat.webappsDirectory', 'build/libs'
}
}
}
I came up with a solution like this instead of using cargo plugin
build.mustRunAfter clean
task deploy(dependsOn: ['clean', 'build', 'deployWar']) << {
println '*********************'
println 'hcadmin.war installed'
println '*********************'
}
task deployWar(type: Copy) {
from war
into System.env.'TOMCAT_HOME' + "\\webapps"
}

Resources