如何在Gradle构建脚本中传递trustStore属性

15

我正试图通过Gradle脚本为一个SOAP Web服务生成类。我使用的是一个名为 gradle-jaxws-plugin 的插件,该插件可在Maven中央仓库中获取。

我的脚本如下所示:

buildscript {   
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "eu.schnuckelig.gradle:gradle-jaxws-plugin:1.0.2"
    }
}

apply plugin: 'maven'
apply plugin: 'jaxws'

jaxws {
    System.setProperty('javax.xml.accessExternalSchema', 'all') 
    packageName = 'com.myservice'
    wsdlURL = 'https://example.org/services/users.svc?wsdl'
}

repositories {
    mavenCentral()
}
如果我按原样使用这个脚本,会得到以下错误。
[ant:wsimport] [ERROR] sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

解决此错误的一种方法是我尝试了gradle build -Djavax.net.ssl.trustStore=cacerts -Djavax.net.ssl.trustStorePassword=changeit,它有效了。但我想在构建脚本中传递这些jvm属性。

我尝试了systemProperty.set(),但没有成功。我正在尝试使用gradle.properties,但也不起作用。有没有干净的方法来传递这些属性?另外,当我进行自动化构建时,我在生产环境中如何处理这个问题呢?

2个回答

34
通常,由于此类数据很敏感,应通过命令行传递或 - 如果您在生产中具有自动化构建 - 应通过例如环境变量配置在系统中(这是最常见的处理方式)。
您可以通过gradle.properties配置系统属性,但它们应该以systemProp前缀为开头,因此:

gradle.properties:

systemProp.javax.net.ssl.trustStore=cacerts
systemProp.javax.net.ssl.trustStorePassword=changeit

以下代码放置在apply部分下面的build.gradle文件中也可以起作用: build.gradle
buildscript {   
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "eu.schnuckelig.gradle:gradle-jaxws-plugin:1.0.2"
    }
}

apply plugin: 'maven'
apply plugin: 'jaxws'

System.setProperty('javax.net.ssl.trustStore', 'cacerts')
System.setProperty('javax.net.ssl.trustStorePassword', 'changeit')

3
你提到的 build.gradle 选项,我之前尝试过了,但是它并没有起作用。 - yogsma
1
这个回复帮助了我,在 gradle 的一个问题上花了 3-4 天时间。非常感谢 @Opal。 - Fayaz
谢谢Opal!这解决了我在使用“maven-publish”插件发布到Nexus仓库时遇到的https问题。我尝试了两种方法,都像广告中所说的一样有效。 - Tom Rutchik

3

这应该可以工作

configurations {
    jaxws
}

dependencies {
    jaxws 'com.sun.xml.ws:jaxws-tools:2.1.4'
}

task wsimport {
    ext.destDir = file("${projectDir}/src/main/generated")
    System.setProperty('javax.net.ssl.keyStoreType', 'pkcs12')
    System.setProperty('javax.net.ssl.keyStore', 'client.pfx')
    System.setProperty('javax.net.ssl.keyStorePassword', 'xxxxxxxxx')
    System.setProperty('javax.net.ssl.keyPassword', 'xxxxxxxxx')
    System.setProperty('javax.net.ssl.trustStore', 'truststore.jks')
    System.setProperty('javax.net.ssl.trustStorePassword', 'xxxxxxxx')
    System.setProperty('sun.security.ssl.allowUnsafeRenegotiation','true')
    doLast {
        ant {
            sourceSets.main.output.classesDir.mkdirs()
            destDir.mkdirs()
            taskdef(name: 'wsimport',
                    classname: 'com.sun.tools.ws.ant.WsImport',
                    classpath: configurations.jaxws.asPath
            )
            wsimport(keep: true,
                    destdir: sourceSets.main.output.classesDir,
                    sourcedestdir: destDir,
                    extension: "true",
                    verbose: "false",
                    quiet: "false",
                    package: "com.example.client.api",
                    xnocompile: "true",
                    wsdl: 'https://test.com/test.asmx?wsdl') {
                xjcarg(value: "-XautoNameResolution")
            }
        }
    }
}

compileJava {
    dependsOn wsimport
    source wsimport.destDir
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接