microsoft/onnxruntime-extensions
Publicmirrored fromhttps://github.com/microsoft/onnxruntime-extensionsAvailable
java/build.gradle
246lines · modecode
| 1 | plugins { |
| 2 | id 'java-library' |
| 3 | id 'maven-publish' |
| 4 | id 'signing' |
| 5 | id 'jacoco' |
| 6 | id 'com.diffplug.spotless' version '6.22.0' |
| 7 | } |
| 8 | |
| 9 | allprojects { |
| 10 | repositories { |
| 11 | mavenCentral() |
| 12 | } |
| 13 | } |
| 14 | |
| 15 | project.group = "com.microsoft.onnxruntime" |
| 16 | project.version = rootProject.file('../version.txt').text.trim() |
| 17 | |
| 18 | // cmake runs will inform us of the build directory of the current run |
| 19 | def cmakeBuildDir = System.properties['cmakeBuildDir'] |
| 20 | def cmakeJavaDir = "${cmakeBuildDir}/java" |
| 21 | def cmakeNativeLibDir = "${cmakeJavaDir}/native-lib" |
| 22 | def cmakeNativeJniDir = "${cmakeJavaDir}/native-jni" |
| 23 | def cmakeNativeTestDir = "${cmakeJavaDir}/native-test" |
| 24 | def cmakeBuildOutputDir = "${cmakeJavaDir}/build" |
| 25 | |
| 26 | def mavenUser = System.properties['mavenUser'] |
| 27 | def mavenPwd = System.properties['mavenPwd'] |
| 28 | |
| 29 | def mavenArtifactId = project.name |
| 30 | |
| 31 | java { |
| 32 | sourceCompatibility = JavaVersion.VERSION_1_8 |
| 33 | targetCompatibility = JavaVersion.VERSION_1_8 |
| 34 | } |
| 35 | |
| 36 | // This jar tasks serves as a CMAKE signalling |
| 37 | // mechanism. The jar will be overwritten by allJar task |
| 38 | jar { |
| 39 | } |
| 40 | |
| 41 | // Add explicit sources jar with pom file. |
| 42 | task sourcesJar(type: Jar, dependsOn: classes) { |
| 43 | archiveClassifier = "sources" |
| 44 | from sourceSets.main.allSource |
| 45 | into("META-INF/maven/$project.group/$mavenArtifactId") { |
| 46 | from { generatePomFileForMavenPublication } |
| 47 | rename ".*", "pom.xml" |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | // Add explicit javadoc jar with pom file |
| 52 | task javadocJar(type: Jar, dependsOn: javadoc) { |
| 53 | archiveClassifier = "javadoc" |
| 54 | from javadoc.destinationDir |
| 55 | into("META-INF/maven/$project.group/$mavenArtifactId") { |
| 56 | from { generatePomFileForMavenPublication } |
| 57 | rename ".*", "pom.xml" |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | spotless { |
| 62 | java { |
| 63 | removeUnusedImports() |
| 64 | googleJavaFormat() |
| 65 | } |
| 66 | format 'gradle', { |
| 67 | target '**/*.gradle' |
| 68 | trimTrailingWhitespace() |
| 69 | indentWithTabs() |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | compileJava { |
| 74 | dependsOn spotlessJava |
| 75 | options.compilerArgs += ["-h", "${project.buildDir}/headers/"] |
| 76 | if (!JavaVersion.current().isJava8()) { |
| 77 | // Ensures only methods present in Java 8 are used |
| 78 | options.compilerArgs.addAll(['--release', '8']) |
| 79 | // Gradle versions before 6.6 require that these flags are unset when using "-release" |
| 80 | java.sourceCompatibility = null |
| 81 | java.targetCompatibility = null |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | compileTestJava { |
| 86 | if (!JavaVersion.current().isJava8()) { |
| 87 | // Ensures only methods present in Java 8 are used |
| 88 | options.compilerArgs.addAll(['--release', '8']) |
| 89 | // Gradle versions before 6.6 require that these flags are unset when using "-release" |
| 90 | java.sourceCompatibility = null |
| 91 | java.targetCompatibility = null |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | sourceSets.test { |
| 96 | // add test resource files |
| 97 | resources.srcDirs += [ |
| 98 | "${rootProject.projectDir}/../java/testdata" |
| 99 | ] |
| 100 | if (cmakeBuildDir != null) { |
| 101 | // add compiled native libs |
| 102 | resources.srcDirs += [ |
| 103 | cmakeNativeLibDir, |
| 104 | cmakeNativeJniDir, |
| 105 | cmakeNativeTestDir |
| 106 | ] |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | if (cmakeBuildDir != null) { |
| 111 | // generate tasks to be called from cmake |
| 112 | |
| 113 | // Overwrite jar location |
| 114 | task allJar(type: Jar) { |
| 115 | manifest { |
| 116 | attributes('Automatic-Module-Name': project.group, |
| 117 | 'Implementation-Title': 'onnxruntime-extensions', |
| 118 | 'Implementation-Version': project.version) |
| 119 | } |
| 120 | into("META-INF/maven/$project.group/$mavenArtifactId") { |
| 121 | from { generatePomFileForMavenPublication } |
| 122 | rename ".*", "pom.xml" |
| 123 | } |
| 124 | from sourceSets.main.output |
| 125 | from cmakeNativeJniDir |
| 126 | from cmakeNativeLibDir |
| 127 | } |
| 128 | |
| 129 | task cmakeBuild(type: Copy) { |
| 130 | from project.buildDir |
| 131 | include 'libs/**' |
| 132 | include 'docs/**' |
| 133 | into cmakeBuildOutputDir |
| 134 | } |
| 135 | cmakeBuild.dependsOn allJar |
| 136 | cmakeBuild.dependsOn sourcesJar |
| 137 | cmakeBuild.dependsOn javadocJar |
| 138 | cmakeBuild.dependsOn javadoc |
| 139 | |
| 140 | task cmakeCheck(type: Copy) { |
| 141 | from project.buildDir |
| 142 | include 'reports/**' |
| 143 | into cmakeBuildOutputDir |
| 144 | } |
| 145 | cmakeCheck.dependsOn check |
| 146 | } |
| 147 | |
| 148 | dependencies { |
| 149 | testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0' |
| 150 | testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0' |
| 151 | testImplementation 'com.google.protobuf:protobuf-java:3.20.1' |
| 152 | } |
| 153 | |
| 154 | processTestResources { |
| 155 | duplicatesStrategy(DuplicatesStrategy.INCLUDE) // allows duplicates in the test resources |
| 156 | } |
| 157 | |
| 158 | test { |
| 159 | java { |
| 160 | dependsOn spotlessJava |
| 161 | } |
| 162 | if (System.getProperty("JAVA_FULL_TEST") != null) { |
| 163 | // Forces each test class to be run in a separate JVM, |
| 164 | // which is necessary for testing the environment thread pool which is ignored if full test is not set. |
| 165 | forkEvery 1 |
| 166 | } |
| 167 | useJUnitPlatform() |
| 168 | if (cmakeBuildDir != null) { |
| 169 | workingDir cmakeBuildDir |
| 170 | } |
| 171 | systemProperties System.getProperties().subMap(['JAVA_FULL_TEST']) |
| 172 | testLogging { |
| 173 | events "passed", "skipped", "failed" |
| 174 | showStandardStreams = true |
| 175 | showStackTraces = true |
| 176 | exceptionFormat = "full" |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | jacocoTestReport { |
| 181 | reports { |
| 182 | xml.required = true |
| 183 | csv.required = true |
| 184 | html.destination file("${buildDir}/jacocoHtml") |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | publishing { |
| 189 | publications { |
| 190 | maven(MavenPublication) { |
| 191 | groupId = project.group |
| 192 | artifactId = mavenArtifactId |
| 193 | version = project.version |
| 194 | |
| 195 | from components.java |
| 196 | pom { |
| 197 | name = 'onnxruntime-extensions' |
| 198 | description = 'ONNXRuntime-Extensions is a library for pre- and post-processing.' |
| 199 | url = 'https://microsoft.github.io/onnxruntime/' |
| 200 | licenses { |
| 201 | license { |
| 202 | name = 'MIT License' |
| 203 | url = 'https://opensource.org/licenses/MIT' |
| 204 | } |
| 205 | } |
| 206 | organization { |
| 207 | name = 'Microsoft' |
| 208 | url = 'http://www.microsoft.com' |
| 209 | } |
| 210 | scm { |
| 211 | connection = 'scm:git:git://github.com:microsoft/onnxruntime-extensions.git' |
| 212 | developerConnection = 'scm:git:ssh://github.com/microsoft/onnxruntime-extensions.git' |
| 213 | url = 'http://github.com/microsoft/onnxruntime-extensions' |
| 214 | } |
| 215 | developers { |
| 216 | developer { |
| 217 | id = 'onnxruntime' |
| 218 | name = 'ONNX Runtime' |
| 219 | email = 'onnxruntime@microsoft.com' |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | repositories { |
| 226 | maven { |
| 227 | url 'https://oss.sonatype.org/service/local/staging/deploy/maven2/' |
| 228 | credentials { |
| 229 | username mavenUser |
| 230 | password mavenPwd |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | // Generates a task signMavenPublication that will |
| 237 | // build all artifacts. |
| 238 | signing { |
| 239 | // Queries env vars: |
| 240 | // ORG_GRADLE_PROJECT_signingKey |
| 241 | // ORG_GRADLE_PROJECT_signingPassword but can be changed to properties |
| 242 | def signingKey = findProperty("signingKey") |
| 243 | def signingPassword = findProperty("signingPassword") |
| 244 | useInMemoryPgpKeys(signingKey, signingPassword) |
| 245 | sign publishing.publications.maven |
| 246 | } |
| 247 | |