/* * SPDX-License-Identifier: Apache-2.0 * * The OpenSearch Contributors require contributions made to * this file be licensed under the Apache-2.0 license or a * compatible open source license. * * Modifications Copyright OpenSearch Contributors. See * GitHub history for details. */ import java.nio.file.Files import org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestFramework import org.opensearch.gradle.test.RestIntegTestTask import org.opensearch.gradle.testclusters.ExtensionsProperties buildscript { ext { opensearch_version = System.getProperty("opensearch.version", "3.0.0") isSnapshot = "true" == System.getProperty("build.snapshot", "true") if (isSnapshot) { opensearch_version += "-SNAPSHOT" } buildVersionQualifier = System.getProperty("build.version_qualifier", "") } repositories { mavenLocal() mavenCentral() maven { url "https://aws.oss.sonatype.org/content/repositories/snapshots" } } dependencies { classpath "org.opensearch.gradle:build-tools:${opensearch_version}" } } plugins { id 'java-library' id "com.diffplug.spotless" version "6.20.0" apply false id 'jacoco' id "com.form.diff-coverage" version "0.9.5" // for javadocs and checks spotless doesn't do id 'checkstyle' } apply from: 'gradle/formatting.gradle' apply plugin: 'java' apply plugin: 'opensearch.opensearchplugin' apply plugin: 'opensearch.testclusters' opensearchplugin { name 'opensearch-sdk-java' description 'OpenSearch Software Development Kit' classname 'org.opensearch.sdk.sample.helloworld.HelloWorldExtension' } ext { projectSubstitutions = [:] licenseFile = rootProject.file('LICENSE.txt') noticeFile = rootProject.file('NOTICE.txt') } dependencyLicenses.enabled = false thirdPartyAudit.enabled = false loggerUsageCheck.enabled = false forbiddenApisMain.enabled = false forbiddenApisTest.enabled = false filepermissions.enabled = false forbiddenPatterns.enabled = false testingConventions.enabled = false validateNebulaPom.enabled = false def _numNodes = findProperty('numNodes') as Integer ?: 1 def testPort = 4500 allprojects { project.ext.licenseName = 'The Apache Software License, Version 2.0' project.ext.licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt' group = 'org.opensearch' version = '2.0.0' if (buildVersionQualifier) { version += "-${buildVersionQualifier}" } if (isSnapshot) { version += "-SNAPSHOT" } } group 'org.opensearch.sdk' allprojects { // Default to the apache license project.ext.licenseName = 'The Apache Software License, Version 2.0' project.ext.licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt' publishing { publications { // add license information to generated poms all { pom { name = "opensearch-sdk-java" } pom.withXml { XmlProvider xml -> Node node = xml.asNode() node.appendNode('description', 'OpenSearch Java SDK Client') Node license = node.appendNode('licenses').appendNode('license') license.appendNode('name', project.licenseName) license.appendNode('url', project.licenseUrl) Node developer = node.appendNode('developers').appendNode('developer') developer.appendNode('name', 'OpenSearch') developer.appendNode('url', 'https://github.com/opensearch-project/opensearch-sdk-java') } } } } } publishing { publications { mavenJava(MavenPublication) { from components.java } } repositories { if (version.toString().endsWith("SNAPSHOT")) { maven { name = "Snapshots" // optional target repository name url = "https://aws.oss.sonatype.org/content/repositories/snapshots" credentials { username "$System.env.SONATYPE_USERNAME" password "$System.env.SONATYPE_PASSWORD" } } } maven { name = "localRepo" url = "$buildDir/localRepo" } } } tasks.named("publishMavenJavaPublicationToMavenLocal").configure { dependsOn("generatePomFileForNebulaPublication") } tasks.named("publishNebulaPublicationToMavenLocal").configure { dependsOn("generatePomFileForMavenJavaPublication") } validateMavenJavaPom.enabled = false javadoc { source = sourceSets.main.allJava classpath = sourceSets.main.compileClasspath } repositories { mavenLocal() mavenCentral() maven { url "https://aws.oss.sonatype.org/content/repositories/snapshots" } maven { url "https://d1nvenhzbhpy0q.cloudfront.net/snapshots/lucene/"} } dependencies { def opensearchVersion = "${opensearch_version}" def log4jVersion = "2.20.0" def nettyVersion = "4.1.96.Final" def jacksonDatabindVersion = "2.15.2" def guavaVersion = "32.1.2-jre" def guiceVersion = "7.0.0" def junit4Version = "4.13.2" def junit5Version = "5.10.0" def junitPlatform = "1.10.0" def jaxbVersion = "2.3.1" def jakartaVersion = "2.1.2" def jakartaInjectVersion = "2.0.1" def javaxVersion = "1" def guavaFailureAccessVersion = "1.0.1" def aopallianceVersion = "1.0" def slf4jVersion = "1.7.36" api("org.opensearch:opensearch:${opensearchVersion}") implementation("org.apache.logging.log4j:log4j-api:${log4jVersion}") implementation("org.apache.logging.log4j:log4j-core:${log4jVersion}") implementation("org.apache.logging.log4j:log4j-jul:${log4jVersion}") implementation("org.opensearch.client:opensearch-rest-high-level-client:${opensearchVersion}") api("org.opensearch.client:opensearch-rest-client:${opensearchVersion}") api("org.opensearch.client:opensearch-java:${opensearchVersion}") { exclude group : 'org.apache.httpcomponents.core5' exclude group : 'org.apache.httpcomponents.client5' } implementation("org.opensearch.plugin:transport-netty4-client:${opensearchVersion}") implementation("io.netty:netty-all:${nettyVersion}") implementation("jakarta.inject:jakarta.inject-api:${jakartaInjectVersion}") testCompileOnly("junit:junit:${junit4Version}") { exclude module : 'hamcrest' exclude module : 'hamcrest-core' } implementation("javax.xml.bind:jaxb-api:${jaxbVersion}") implementation("com.fasterxml.jackson.core:jackson-databind:${jacksonDatabindVersion}") implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:${jacksonDatabindVersion}") implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:${jacksonDatabindVersion}") implementation("com.fasterxml.jackson.datatype:jackson-datatype-guava:${jacksonDatabindVersion}") implementation("com.fasterxml.jackson.core:jackson-annotations:${jacksonDatabindVersion}") implementation("com.google.guava:guava:${guavaVersion}") implementation("javax.inject:javax.inject:${javaxVersion}") implementation("com.google.guava:failureaccess:${guavaFailureAccessVersion}") implementation("aopalliance:aopalliance:${aopallianceVersion}") constraints { implementation("com.google.guava:guava:${guavaVersion}") { because 'versions below 30.0 have active CVE' } } implementation("com.google.inject:guice:${guiceVersion}") testImplementation("org.junit.jupiter:junit-jupiter-api:${junit5Version}") testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junit5Version}") testImplementation("org.opensearch.test:framework:${opensearchVersion}") testRuntimeOnly("org.junit.platform:junit-platform-launcher:${junitPlatform}") configurations.all { resolutionStrategy { force("jakarta.json:jakarta.json-api:${jakartaVersion}") force("com.fasterxml.jackson.core:jackson-databind:${jacksonDatabindVersion}") force("com.fasterxml.jackson.core:jackson-core:${jacksonDatabindVersion}") force("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:${jacksonDatabindVersion}") force("com.fasterxml.jackson.dataformat:jackson-dataformat-smile:${jacksonDatabindVersion}") force("com.fasterxml.jackson.dataformat:jackson-dataformat-cbor:${jacksonDatabindVersion}") force("org.apache.logging.log4j:log4j-api:${log4jVersion}") force("org.apache.logging.log4j:log4j-core:${log4jVersion}") force("org.apache.logging.log4j:log4j-jul:${log4jVersion}") force("org.slf4j:slf4j-api:${slf4jVersion}") } } } // this task runs the helloworld sample extension task helloWorld(type: JavaExec) { group = 'Execution' description = 'Run HelloWorld Extension.' mainClass = 'org.opensearch.sdk.sample.helloworld.HelloWorldExtension' classpath = sourceSets.main.runtimeClasspath systemProperty 'log4j2.configurationFile', 'log4j2-sdk.xml' } task getVersion() { doLast { println("version=${version}") } } test { useJUnitPlatform() jvmArgs '--enable-preview' systemProperty 'tests.security.manager', 'false' testLogging { events "passed", "skipped", "failed" exceptionFormat "full" } jacoco { classDumpDir = file("$buildDir/jacoco/classpathdumps") } finalizedBy jacocoTestReport } jacocoTestReport { dependsOn test reports { xml.required = true } } // Get uncommitted files via git diff // https://github.com/form-com/diff-coverage-gradle/issues/73 ext.createDiffFile = { -> def file = Files.createTempFile(URLEncoder.encode(project.name, 'UTF-8'), '.diff').toFile() def diffBase = 'refs/remotes/origin/main' // Only run locally if (!System.getenv('CI')) { file.withOutputStream { out -> exec { commandLine 'git', 'diff', '--no-color', '--minimal', diffBase standardOutput = out } } } return file } diffCoverageReport { afterEvaluate { diffSource.file = createDiffFile() } // View report at build/reports/jacoco/diffCoverage/html/index.html reports { html = true } violationRules { minBranches = 0.60 minLines = 0.75 failOnViolation = true } } task integTest(type: RestIntegTestTask) { description = "Run tests against a cluster" testClassesDirs = sourceSets.test.output.classesDirs classpath = sourceSets.test.runtimeClasspath } tasks.named("check").configure { dependsOn(integTest) } task startTestExtension { doFirst{ ext.process = new ProcessBuilder() .directory(projectDir) .command("./gradlew", "helloWorld") .start() } } integTest { dependsOn(startTestExtension) //TODO: find a way to add permissions for the tests systemProperty 'tests.security.manager', 'false' systemProperty "https", System.getProperty("https") systemProperty "user", System.getProperty("user") systemProperty "password", System.getProperty("password") // The 'doFirst' delays till execution time. doFirst { // Tell the test JVM if the cluster JVM is running under a debugger so that tests can // use longer timeouts for requests. def isDebuggingCluster = getDebug() || System.getProperty("test.debug") != null systemProperty 'cluster.debug', isDebuggingCluster // Set number of nodes system property to be used in tests systemProperty 'cluster.number_of_nodes', "1" // There seems to be an issue when running multi node run or integ tasks with unicast_hosts // not being written, the waitForAllConditions ensures it's written getClusters().forEach { cluster -> cluster.waitForAllConditions() } } // The --debug-jvm command-line option makes the cluster debuggable; this makes the tests debuggable if (System.getProperty("test.debug") != null) { jvmArgs '-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:5005' } } task closeTestExtension { doFirst { if (tasks.startTestExtension.process != null) { tasks.startTestExtension.process.destroy() } } } tasks.named("integTest").configure { finalizedBy(closeTestExtension) } testClusters.integTest { extension(true) testDistribution = "ARCHIVE" // Cluster shrink exception thrown if we try to set numberOfNodes to 1, so only apply if > 1 if (_numNodes > 1) numberOfNodes = _numNodes // When running integration tests it doesn't forward the --debug-jvm to the cluster anymore // i.e. we have to use a custom property to flag when we want to debug elasticsearch JVM // since we also support multi node integration tests we increase debugPort per node if (System.getProperty("opensearch.debug") != null) { def debugPort = 5005 nodes.forEach { node -> node.jvmArgs("-agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=*:${debugPort}") debugPort += 1 } } }