microsoft/onnxruntime-extensions
Publicmirrored from https://github.com/microsoft/onnxruntime-extensionsAvailable
java/build.gradle
246lines · modeblame
08659eaeWenbing Li3 years ago | 1 | plugins { |
66be6bb6Wenbing Li2 years ago | 2 | id 'java-library' |
| 3 | id 'maven-publish' | |
| 4 | id 'signing' | |
| 5 | id 'jacoco' | |
2aeca727Edward Chen2 years ago | 6 | id 'com.diffplug.spotless' version '6.22.0' |
08659eaeWenbing Li3 years ago | 7 | } |
| 8 | | |
| 9 | allprojects { | |
66be6bb6Wenbing Li2 years ago | 10 | repositories { |
| 11 | mavenCentral() | |
| 12 | } | |
08659eaeWenbing Li3 years ago | 13 | } |
| 14 | | |
| 15 | project.group = "com.microsoft.onnxruntime" | |
63fe165eEdward Chen3 years ago | 16 | project.version = rootProject.file('../version.txt').text.trim() |
08659eaeWenbing Li3 years ago | 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 { | |
66be6bb6Wenbing Li2 years ago | 32 | sourceCompatibility = JavaVersion.VERSION_1_8 |
| 33 | targetCompatibility = JavaVersion.VERSION_1_8 | |
08659eaeWenbing Li3 years ago | 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) { | |
66be6bb6Wenbing Li2 years ago | 43 | archiveClassifier = "sources" |
| 44 | from sourceSets.main.allSource | |
| 45 | into("META-INF/maven/$project.group/$mavenArtifactId") { | |
| 46 | from { generatePomFileForMavenPublication } | |
| 47 | rename ".*", "pom.xml" | |
| 48 | } | |
08659eaeWenbing Li3 years ago | 49 | } |
| 50 | | |
| 51 | // Add explicit javadoc jar with pom file | |
| 52 | task javadocJar(type: Jar, dependsOn: javadoc) { | |
66be6bb6Wenbing Li2 years ago | 53 | archiveClassifier = "javadoc" |
| 54 | from javadoc.destinationDir | |
| 55 | into("META-INF/maven/$project.group/$mavenArtifactId") { | |
| 56 | from { generatePomFileForMavenPublication } | |
| 57 | rename ".*", "pom.xml" | |
| 58 | } | |
08659eaeWenbing Li3 years ago | 59 | } |
| 60 | | |
| 61 | spotless { | |
66be6bb6Wenbing Li2 years ago | 62 | java { |
| 63 | removeUnusedImports() | |
| 64 | googleJavaFormat() | |
| 65 | } | |
| 66 | format 'gradle', { | |
| 67 | target '**/*.gradle' | |
| 68 | trimTrailingWhitespace() | |
| 69 | indentWithTabs() | |
| 70 | } | |
08659eaeWenbing Li3 years ago | 71 | } |
| 72 | | |
| 73 | compileJava { | |
66be6bb6Wenbing Li2 years ago | 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 | } | |
08659eaeWenbing Li3 years ago | 83 | } |
| 84 | | |
| 85 | compileTestJava { | |
66be6bb6Wenbing Li2 years ago | 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 | } | |
08659eaeWenbing Li3 years ago | 93 | } |
| 94 | | |
| 95 | sourceSets.test { | |
66be6bb6Wenbing Li2 years ago | 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 | } | |
08659eaeWenbing Li3 years ago | 108 | } |
| 109 | | |
| 110 | if (cmakeBuildDir != null) { | |
66be6bb6Wenbing Li2 years ago | 111 | // generate tasks to be called from cmake |
| 112 | | |
| 113 | // Overwrite jar location | |
| 114 | task allJar(type: Jar) { | |
| 115 | manifest { | |
fcf28fe8Adam Pocock2 years ago | 116 | attributes('Automatic-Module-Name': "com.microsoft.onnxruntime.extensions", |
66be6bb6Wenbing Li2 years ago | 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 | |
08659eaeWenbing Li3 years ago | 146 | } |
| 147 | | |
| 148 | dependencies { | |
66be6bb6Wenbing Li2 years ago | 149 | testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0' |
| 150 | testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0' | |
a1285d8fScott McKay2 years ago | 151 | testImplementation 'com.google.protobuf:protobuf-java:3.21.7' |
08659eaeWenbing Li3 years ago | 152 | } |
| 153 | | |
| 154 | processTestResources { | |
66be6bb6Wenbing Li2 years ago | 155 | duplicatesStrategy(DuplicatesStrategy.INCLUDE) // allows duplicates in the test resources |
08659eaeWenbing Li3 years ago | 156 | } |
| 157 | | |
| 158 | test { | |
66be6bb6Wenbing Li2 years ago | 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 | } | |
08659eaeWenbing Li3 years ago | 178 | } |
| 179 | | |
| 180 | jacocoTestReport { | |
66be6bb6Wenbing Li2 years ago | 181 | reports { |
| 182 | xml.required = true | |
| 183 | csv.required = true | |
| 184 | html.destination file("${buildDir}/jacocoHtml") | |
| 185 | } | |
08659eaeWenbing Li3 years ago | 186 | } |
| 187 | | |
| 188 | publishing { | |
66be6bb6Wenbing Li2 years ago | 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 | } | |
08659eaeWenbing Li3 years ago | 234 | } |
| 235 | | |
| 236 | // Generates a task signMavenPublication that will | |
| 237 | // build all artifacts. | |
| 238 | signing { | |
66be6bb6Wenbing Li2 years ago | 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 | |
08659eaeWenbing Li3 years ago | 246 | } |