OSDN Git Service

Gradle: add stageprofileid
[dictzip-java/dictzip-java.git] / build.gradle
index 961066c..7c4b83a 100644 (file)
@@ -1,91 +1,77 @@
 import org.apache.tools.ant.filters.ReplaceTokens
-import groovy.swing.SwingBuilder
 
 plugins {
-    // github
-    id "co.riiid.gradle" version "0.4.2"
-    // bintray
-    id "com.jfrog.bintray" version "1.6"
+    id 'java-library'
+    id 'maven-publish'
+    id 'java-library-distribution'
+    id "signing"
+    id "com.jfrog.bintray" version "1.8.5"
+    id "org.nosphere.gradle.github.actions" version "1.1.0"
+    id "io.github.gradle-nexus.publish-plugin" version "1.0.0"
+    id "io.freefair.maven-central.validate-poms" version "5.3.0"
+    id 'com.palantir.git-version' version "0.12.3"
+}
+
+// calculate version string from git tag, hash and commit distance
+if (versionDetails().isCleanTag) {
+    // drop first 'v' from version tag
+    version = gitVersion().substring(1)
+} else {
+    version = versionDetails().lastTag.substring(1) + '-' + versionDetails().commitDistance + '-' + versionDetails().gitHash + '-SNAPSHOT'
 }
 
 // common settings
 subprojects {
-    apply plugin: 'java'
     apply plugin: 'jacoco'
+    apply plugin: 'java'
     apply plugin: 'checkstyle'
-    apply plugin: 'findbugs'
-    apply plugin: 'co.riiid.gradle'
-    apply plugin: 'com.jfrog.bintray'
     apply plugin: 'maven'
-    apply plugin: 'signing'
+    apply plugin: 'maven-publish'
 
-    sourceCompatibility = jdkVersion
-    targetCompatibility = jdkVersion
     [compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
-    [compileJava, compileTestJava]*.options*.bootClasspath = "$JDK_HOME/jre/lib/rt.jar"
     checkstyle {
         config = resources.text.fromFile("${rootProject.projectDir}/config/checkstyle/checkstyle.xml")
         ignoreFailures = true
         toolVersion = '6.16.1'
     }
-
-    // Force prevent checkstyle/findbugs on Test.
-    task checkstyleTest(overwrite: true) << {}
-    task findbugsTest(overwrite: true) << {}
-    tasks.withType(FindBugs) {
-        reports {
-            xml.enabled = false
-            html.enabled = true
-        }
-    }
+    version = rootProject.version
+    group = projectGroup
 
     repositories {
-        jcenter()
         mavenCentral()
     }
 
     dependencies {
-        testCompile 'org.testng:testng:6.9.10'
+        testImplementation 'commons-io:commons-io:2.6'
+        testImplementation 'org.testng:testng:6.9.10'
     }
+    test.useTestNG()
 
-    test {
-        useTestNG()
+    java {
+        sourceCompatibility = JavaVersion.VERSION_1_8
+        targetCompatibility = JavaVersion.VERSION_1_8
+        withSourcesJar()
+        withJavadocJar()
     }
 
     javadoc {
         options.locale = 'en_US'
     }
 
-    task sourceJar(type: Jar) {
-        from sourceSets.main.allSource
-        classifier = 'sources'
-    }
-
-    task javadocJar(type: Jar, dependsOn: javadoc) {
-        classifier = 'javadoc'
-        from javadoc.destinationDir
-    }
-
     artifacts {
         archives jar
-        archives sourceJar
+        archives sourcesJar
         archives javadocJar
     }
-
-    signing {
-        sign configurations.archives
-    }
 }
 
 project(':dictzip-lib') {
-    dependencies {
-        testCompile 'tokyo.northside:northside-io:0.2.0'
-    }
-    version = projectVersion
-    group = projectGroup
+    apply plugin: 'java-library'
+    apply plugin: 'com.jfrog.bintray'
+    version = rootProject.version
     bintray {
-        user = project.hasProperty('bintrayUser') ? project.property('bintrayUser') : System.getenv('BINTRAY_USER')
-        key = project.hasProperty('bintrayApiKey') ? project.property('bintrayApiKey') : System.getenv('BINTRAY_API_KEY')
+        user = System.getenv('BINTRAY_USER')
+        key = System.getenv('BINTRAY_KEY')
         configurations = ['archives']
         pkg {
             repo = 'maven'
@@ -93,48 +79,141 @@ project(':dictzip-lib') {
             userOrg = 'dictzip'
             licenses = ['GPL-2.0+CE']
             vcsUrl = projectUrl
-            labels = ['java','dictzip']
+            labels = ['java', 'dictzip']
             publicDownloadNumbers = true
         }
     }
-    task mandoc << {}
-    task githubRelease(overwrite: true) << {}
-}
+    install {
+        repositories.mavenInstaller {
+            pom.project {
+                name = 'dictzip'
+                packaging = 'jar'
+                description = projectDesc
+                url = projectUrl
+                version = project.version
+                licenses {
+                    license {
+                        name = licenseName
+                        url = licenseUrl
+                        distribution = 'repo'
+                    }
+                }
+                scm {
+                    url = githubUrl
+                    connection = "scm:git:${githubUrl}"
+                    developerConnection = "scm:git:${githubUrl}"
+                }
+                developers {
+                    developer {
+                        id = projectOwner
+                        name = developerName
+                        email = developerEmail
+                    }
+                }
+            }
+        }
+    }
+
+    publishing {
+        publications {
+            mavenJava(MavenPublication) {
+                from components.java
+                artifactId = "dictzip"
+                groupId = "io.github.dictzip"
+                pom {
+                    name.set("dictzip")
+                    description.set(projectDesc)
+                    url.set(projectUrl)
+                    licenses {
+                        license {
+                            name.set("GNU General Public License Version 2+CE")
+                            url.set("https://www.gnu.org/licenses/old-licenses/gpl-2.0")
+                        }
+                    }
+                    scm {
+                        connection.set("scm:git:git://github.com/dictzip/dictzip-java.git")
+                        developerConnection.set("scm:git:git://github.com/dictzip/dictzip-java.git")
+                        url.set(projectUrl)
+                    }
+                    developers {
+                        developer {
+                            id = "miurahr"
+                            name = "Hiroshi Miura"
+                            email = "miurahr@linux.com"
+                        }
+                    }
+                    issueManagement {
+                        url.set(projectUrl + "/issues")
+                    }
+                }
+            }
+        }
+    }
+    repositories {
+        maven {
+            name = "GitHubPackages"
+            url = "https://maven.pkg.github.com/dictzip/dictzip-java"
+            credentials {
+                username = System.getenv("GITHUB_ACTOR")
+                password = System.getenv("GITHUB_TOKEN")
+            }
+        }
+        maven {
+            name 'AzurePackages'
+            url 'https://pkgs.dev.azure.com/miurahr/github/_packaging/maven/maven/v1'
+            credentials(PasswordCredentials) {
+                username = System.getenv("AZURE_USER")
+                password = System.getenv("AZURE_TOKEN")
+            }
+            authentication {
+                basic(BasicAuthentication)
+            }
+        }
+    }
+    nexusPublishing {
+        repositories {
+            sonatype {
+                stagingProfileId = "a1cf138b142cd"
+                nexusUrl.set(uri("https://s01.oss.sonatype.org/service/local/"))
+                snapshotRepositoryUrl.set(uri("https://s01.oss.sonatype.org/content/repositories/snapshots/"))
+                username = project.hasProperty('sonatypeUsername') ? project.property('sonatypeUsername') : System.getenv('SONATYPE_USER')
+                password = project.hasProperty('sonatypePassword') ? project.property('sonatypePassword') : System.getenv('SONATYPE_PASS')
+            }
+        }
+    }
+
+    signing {
+        def signingKey = findProperty("signingKey")
+        def signingPassword = findProperty("signingPassword")
+        if (signingKey) {
+            useInMemoryPgpKeys(signingKey, signingPassword)
+        } else {
+            useGpgCmd()
+        }
+            sign publishing.publications.mavenJava
+        }
+        tasks.withType(Sign) {
+            def hasKey = project.hasProperty("signingKey") || project.hasProperty("signing.gnupg.keyName")
+            onlyIf { hasKey && versionDetails().isCleanTag }
+        }
+    }
 
 project(':dictzip-cli') {
     apply plugin: 'application'
     mainClassName = 'org.dict.zip.cli.Main'
     applicationName = 'dictzip'
-
+    version = rootProject.version
     dependencies {
-        compile project(':dictzip-lib'),
-           'gnu.getopt:java-getopt:1.0.13'
-        testCompile 'tokyo.northside:northside-io:0.2.0'
+        implementation project(':dictzip-lib')
+        implementation 'gnu.getopt:java-getopt:1.0.13'
     }
-    version = projectVersion
-    group = projectGroup
-    bintray {
-        user = project.hasProperty('bintrayUser') ? project.property('bintrayUser') : System.getenv('BINTRAY_USER')
-        key = project.hasProperty('bintrayApiKey') ? project.property('bintrayApiKey') : System.getenv('BINTRAY_API_KEY')
-        configurations = ['archives']
-        pkg {
-            repo = 'maven'
-            name = 'dictzip-cli'
-            userOrg = 'dictzip'
-            licenses = ['GPL-3.0']
-            vcsUrl = projectUrl
-            labels = ['java','dictzip']
-            publicDownloadNumbers = true
-        }
-    }
-
-    task mandoc(type: Copy, overwrite: true) {
+    task mandoc(type: Copy) {
         from "doc/dictzip.1.in"
         into 'build/docs'
         rename { String fileName ->
             fileName.replace('dictzip.1.in', 'dictzip.1')
         }
-        filter(ReplaceTokens, tokens: [copyright: projectYears, version: projectVersion])
+        filter(ReplaceTokens, tokens: [copyright: projectYears, version: project.version])
     }
 
     distTar {
@@ -152,69 +231,10 @@ project(':dictzip-cli') {
                 from(javadocJar) {
                     into 'docs'
                 }
-                from(sourceJar) {
+                from(sourcesJar) {
                     into 'source'
                 }
             }
         }
     }
-    task githubRelease(overwrite: true) << {}
-}
-
-task mandoc << {}
-task bintrayUpload(overwrite: true) << {}
-
-boolean validProperty(propertyName) {
-    try { project.property(propertyName) != null }
-    catch (MissingPropertyException) { false }
-}
-
-String askPassword(prompt) {
-    def keyPass = ''
-    if(System.console() == null) {
-        new SwingBuilder().edt {
-            dialog(modal: true, title: 'Enter password', alwaysOnTop: true, resizable: false, locationRelativeTo: null, pack: true, show: true) {
-                vbox {
-                    label(text: prompt)
-                    def input1 = passwordField()
-                    button(defaultButton: true, text: 'OK', actionPerformed: {
-                        keyPass = input1.password;
-                        dispose();
-                    })
-                }
-            }
-        }
-    } else {
-        keyPass = System.console().readPassword(prompt)
-    }
-    if (keyPass.size() <= 0) {
-        throw new InvalidUserDataException("You must enter the passwords to proceed.")
-    }
-    new String(keyPass)
-}
-
-github {
-    owner = projectOwner
-    repo = 'dictzip-java'
-    token = githubToken
-    tagName = projectTag
-    targetCommitish = 'master'
-    name = projectTag
-    draft = true
-    body = projectReleaseBody
-    assets = [
-            'dictzip-cli/build/distributions/dictzip-' + projectVersion + '.tgz',
-            'dictzip-cli/build/distributions/dictzip-' + projectVersion + '.zip',
-            'dictzip-lib/build/libs/dictzip-lib-' + projectVersion + '.jar',
-            'dictzip-lib/build/libs/dictzip-lib-' + projectVersion + '-sources.jar',
-            'dictzip-lib/build/libs/dictzip-lib-' + projectVersion + '-javadoc.jar'
-    ]
-}
-
-if (gradle.startParameter.taskNames.contains('bintrayUpload')  ||
-    gradle.startParameter.taskNames.contains('githubRelease')) {
-    assert validProperty('signing.keyId'),             'properties for signing must be provided'
-    assert validProperty('signing.secretKeyRingFile'), 'properties for signing must be provided'
-    ext.'signing.password' = askPassword("Enter password for PGP key ${property('signing.keyId')}: ")
-
 }