import se.bjurr.gitchangelog.plugin.gradle.GitChangelogTask import java.nio.charset.Charset plugins { id 'java' id 'maven-publish' id "ca.coglinc2.javacc" version "latest.release" id 'jacoco' id "com.github.spotbugs" version "latest.release" id "com.diffplug.spotless" version "latest.release" id 'pmd' id 'checkstyle' // download the RR tools which have no Maven Repository id "de.undercouch.download" version "latest.release" id "se.bjurr.gitchangelog.git-changelog-gradle-plugin" version "latest.release" } group = 'com.github.jsqlparser' def getVersion = { boolean considerSnapshot -> def major = 0 def minor = 0 def patch = 0 def commit = "" def snapshot ="" new ByteArrayOutputStream().withStream { os -> exec { workingDir "$projectDir" args = [ "--no-pager" , "describe" , "--tags" , "--always" , "--dirty=-SNAPSHOT" ] executable "git" standardOutput = os } def matcher = os.toString() =~ /jsqlparser-(\d*)\.(\d*)-(\d*)-([a-zA-Z\d]*)/ matcher.find() major = matcher[0][1] minor = matcher[0][2] patch = matcher[0][3] commit = matcher[0][4] if (considerSnapshot) { minor++ snapshot = "-SNAPSHOT" } } return "${major}.${minor}${snapshot}" } version = getVersion(true) description = 'JSQLParser library' java.sourceCompatibility = JavaVersion.VERSION_1_8 repositories { gradlePluginPortal() mavenLocal() mavenCentral() } dependencies { testImplementation 'commons-io:commons-io:2.+' testImplementation 'org.apache.commons:commons-text:+' testImplementation 'org.mockito:mockito-core:4.+' testImplementation 'org.assertj:assertj-core:3.+' testImplementation 'org.hamcrest:hamcrest-core:2.+' testImplementation 'org.apache.commons:commons-lang3:3.+' testImplementation 'com.h2database:h2:2.+' // for JaCoCo Reports testImplementation 'org.junit.jupiter:junit-jupiter-api:5.+' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.+' testImplementation 'org.junit.jupiter:junit-jupiter-params:+' // https://mvnrepository.com/artifact/org.mockito/mockito-junit-jupiter testImplementation 'org.mockito:mockito-junit-jupiter:4.+' // enforce latest version of JavaCC testImplementation 'net.java.dev.javacc:javacc:7.0.12' javacc 'net.java.dev.javacc:javacc:7.0.12' } compileJavacc { arguments = [grammar_encoding: 'UTF-8', static: 'false', java_template_type: 'modern'] } java { withSourcesJar() withJavadocJar() } test { environment = [ 'EXPORT_TEST_TO_FILE': 'True' ] useJUnitPlatform() // set heap size for the test JVM(s) minHeapSize = "128m" maxHeapSize = "1G" jvmArgs << [ '-Djunit.jupiter.execution.parallel.enabled=true', '-Djunit.jupiter.execution.parallel.config.strategy=dynamic', '-Djunit.jupiter.execution.parallel.mode.default=concurrent' ] finalizedBy check } jacocoTestReport { dependsOn test // tests are required to run before generating the report reports { xml.required = false csv.required = false html.outputLocation = layout.buildDirectory.dir('reports/jacoco') } } jacocoTestCoverageVerification { violationRules { rule { //element = 'CLASS' limit { //@todo: temporarily reduced it 80%, we need to bring that back to 84% accepting the Keywords PR minimum = 0.80 } excludes = [ 'net.sf.jsqlparser.util.validation.*', 'net.sf.jsqlparser.**.*Adapter', 'net.sf.jsqlparser.parser.JJTCCJSqlParserState', 'net.sf.jsqlparser.parser.TokenMgrError', 'net.sf.jsqlparser.parser.StreamProvider', 'net.sf.jsqlparser.parser.CCJSqlParserTokenManager', 'net.sf.jsqlparser.parser.ParseException', 'net.sf.jsqlparser.parser.SimpleNode', 'net.sf.jsqlparser.parser.SimpleCharStream', 'net.sf.jsqlparser.parser.StringProvider', ] } rule { //element = 'CLASS' limit { counter = 'LINE' value = 'MISSEDCOUNT' //@todo: temporarily increased to 7000, we need to bring that down to 5500 after accepting the Keywords PR maximum = 7000 } excludes = [ 'net.sf.jsqlparser.util.validation.*', 'net.sf.jsqlparser.**.*Adapter', 'net.sf.jsqlparser.parser.JJTCCJSqlParserState', 'net.sf.jsqlparser.parser.TokenMgrError', 'net.sf.jsqlparser.parser.StreamProvider', 'net.sf.jsqlparser.parser.CCJSqlParserTokenManager', 'net.sf.jsqlparser.parser.ParseException', 'net.sf.jsqlparser.parser.SimpleNode', 'net.sf.jsqlparser.parser.SimpleCharStream', 'net.sf.jsqlparser.parser.StringProvider', ] } // rule { // element = 'CLASS' // limit { // counter = 'LINE' // value = 'MISSEDRATIO' // maximum = 0.3 // } // excludes = [ // 'net.sf.jsqlparser.util.validation.*', // 'net.sf.jsqlparser.**.*Adapter', // 'net.sf.jsqlparser.parser.JJTCCJSqlParserState', // 'net.sf.jsqlparser.parser.TokenMgrError', // 'net.sf.jsqlparser.parser.StreamProvider', // 'net.sf.jsqlparser.parser.CCJSqlParserTokenManager', // 'net.sf.jsqlparser.parser.ParseException', // 'net.sf.jsqlparser.parser.SimpleNode', // 'net.sf.jsqlparser.parser.SimpleCharStream', // 'net.sf.jsqlparser.parser.StringProvider', // ] // } } } spotbugsMain { reports { html { enabled = true destination = file("build/reports/spotbugs/main/spotbugs.html") stylesheet = 'fancy-hist.xsl' } } } spotbugs { // fail only on P1 and without the net.sf.jsqlparser.parser.* excludeFilter = file("config/spotbugs/spotBugsExcludeFilter.xml") // do not run over the test, although we should do that eventually spotbugsTest.enabled = false } pmd { consoleOutput = false //toolVersion = "6.46.0" sourceSets = [sourceSets.main] // clear the ruleset in order to use configured rules only ruleSets = [] //rulesMinimumPriority = 1 ruleSetFiles = files("config/pmd/ruleset.xml") pmdMain { excludes = [ "build/generated/*" ] } } checkstyle { sourceSets = [sourceSets.main, sourceSets.test] configFile =rootProject.file('config/checkstyle/checkstyle.xml') } spotless { // optional: limit format enforcement to just the files changed by this feature branch ratchetFrom 'origin/master' format 'misc', { // define the files to apply `misc` to target '*.gradle', '*.md', '.gitignore' // define the steps to apply to those files trimTrailingWhitespace() indentWithSpaces(4) // or spaces. Takes an integer argument if you don't like 4 endWithNewline() } java { indentWithSpaces(4) eclipse().configFile('config/formatter/eclipse-java-google-style.xml') } } tasks.withType(Checkstyle) { reports { xml.required = false html.required = true } } task renderRR() { dependsOn(compileJavacc) doLast { // these WAR files have been provided as a courtesy by Gunther Rademacher // and belong to the RR - Railroad Diagram Generator Project // https://github.com/GuntherRademacher/rr // // Hosting at manticore-projects.com is temporary until a better solution is found // Please do not use these files without Gunther's permission download.run { src 'http://manticore-projects.com/download/convert.war' dest "$buildDir/rr/convert.war" overwrite false } download.run { src 'http://manticore-projects.com/download/rr.war' dest "$buildDir/rr/rr.war" overwrite false } javaexec { standardOutput = new FileOutputStream("${buildDir}/rr/JSqlParserCC.ebnf") main = "-jar" args = [ "$buildDir/rr/convert.war", "$buildDir/generated/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jj" ] } javaexec { main = "-jar" args = [ "$buildDir/rr/rr.war", "-noepsilon", "-color:#4D88FF", "-offset:0", "-width:800", //"-png", //"-out:${buildDir}/rr/JSqlParserCC.zip", "-out:${buildDir}/rr/JSqlParserCC.xhtml", "${buildDir}/rr/JSqlParserCC.ebnf" ] } //@todo: a Java based solution may be more appropriate here exec { commandLine "sh", "-c", "xsltproc src/main/resources/rr/xhtml2rst.xsl $buildDir/rr/JSqlParserCC.xhtml > src/site/sphinx/syntax.rst" } } } task gitChangelogTask(type: GitChangelogTask) { fromRepo = file("$projectDir") file = new File("${projectDir}/src/site/sphinx/changelog.rst") fromRef = "4.0" //toRef = "1.1"; // switch off the formatter since the indentation matters for Mark-down // @formatter:off templateContent =""" ************************ Changelog ************************ {{#tags}} {{#ifMatches name "^Unreleased.*"}} Latest Changes since |JSQLPARSER_VERSION| {{/ifMatches}} {{#ifMatches name "^(?!Unreleased).*"}} Version {{name}} {{/ifMatches}} ============================================================= {{#issues}} {{#commits}} {{#ifMatches messageTitle "^(?!Merge).*"}} * **{{{messageTitle}}}** {{authorName}}, {{commitDate}} {{/ifMatches}} {{/commits}} {{/issues}} {{/tags}} """ // @formatter:on } task updateKeywords(type: JavaExec) { group = "Execution" description = "Run the main class with JavaExecTask" classpath = sourceSets.main.runtimeClasspath args = [ file('src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt').absolutePath , file('src/site/sphinx/keywords.rst').absolutePath ] main("net.sf.jsqlparser.parser.ParserKeywordsUtils") dependsOn(compileJava) } task sphinx(type: Exec) { dependsOn(gitChangelogTask, renderRR, updateKeywords) String PROLOG = """ .. |_| unicode:: U+00A0 :trim: .. |JSQLPARSER_EMAIL| replace:: support@manticore-projects.com .. |JSQLPARSER_VERSION| replace:: ${getVersion(false)} .. |JSQLPARSER_SNAPSHOT_VERSION| replace:: ${getVersion(true)} .. |JSQLPARSER_STABLE_VERSION_LINK| raw:: html ${project.name}-${getVersion(false)}.jar .. |JSQLPARSER_SNAPSHOT_VERSION_LINK| raw:: html ${project.name}-${getVersion(true)}.jar """ args = [ "-Dproject=JSQLParser" , "-Dcopyright=Tobias Warneke, 2022" , "-Dauthor=Tobias Warneke" , "-Drelease=${getVersion(false)}" , "-Drst_prolog=$PROLOG" , "${projectDir}/src/site/sphinx" , "${project.buildDir}/sphinx" ] executable "sphinx-build" //store the output instead of printing to the console: standardOutput = new ByteArrayOutputStream() //extension method stopTomcat.output() can be used to obtain the output: ext.output = { return standardOutput.toString() } } publishing { publications { mavenJava(MavenPublication) { artifactId 'jsqlparser' from(components.java) versionMapping { usage('java-api') { fromResolutionOf('testFixturesRuntimeClasspath') } usage('java-runtime') { fromResolutionResult() } } } } repositories { maven { // Username and Password are defined in ~/.gradle/gradle.properties name "ossrh" url "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/" credentials(PasswordCredentials) } } } tasks.withType(JavaCompile) { options.encoding = 'UTF-8' } task upload(type: Exec) { dependsOn(build, gitChangelogTask) def versionStable = getVersion(false) if( findProperty("${project.name}.host")==null ) { println( """ Property \"${project.name}.host\' not found. Please define \"${project.name}.host\" in the Gradle configuration (e. g. \$HOME/.gradle/gradle.properties. """ ) } else { // define the USERNAME and HOST properties in ~/.gradle/gradle.properties args = ["sftp://${findProperty("${project.name}.username")}@${findProperty("${project.name}.host")}/download"] executable "sftp" standardInput = new ByteArrayInputStream("""<