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

yog*_*sma 5 java ssl gradle pkix

我试图通过gradle脚本为SOAP Web服务生成类.我正在使用gradle-jaxws-pluginmaven中心提供的插件.

我的脚本如下所示:

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()
}
Run Code Online (Sandbox Code Playgroud)

如果我按原样使用此脚本,则会出现以下错误

[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
Run Code Online (Sandbox Code Playgroud)

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

我试过了systemProperty.set(),但没办法.我正在尝试gradle.properties,但这也不起作用.是否有通过这些属性的干净方法?另外,我想知道如果我有一个自动构建,我将如何在生产中处理这个问题.

Opa*_*pal 14

通常,由于这些数据是敏感的,它们应该通过命令行传递,或者 - 如果你有生产中的自动构建 - 应该在系统中通过例如环境变量进行配置(这是最经常处理的方式).

您可以通过配置系统属性,gradle.properties但它们应该systemProp前缀为前缀,因此:

gradle.properties:

systemProp.javax.net.ssl.trustStore=cacerts
systemProp.javax.net.ssl.trustStorePassword=changeit
Run Code Online (Sandbox Code Playgroud)

下面的部分代码也应该在build.gradle下面的apply部分中运行: 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')
Run Code Online (Sandbox Code Playgroud)

  • 你提到的 build.gradle 选项,我以前试过,但它不起作用。 (2认同)