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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43483514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:28:01  来源:igfitidea点击:

How to pass trustStore property in gradle build script

javasslgradlepkix

提问by yogsma

I am trying to generate classes for a SOAP webservice through a gradle script. I am using a plugin gradle-jaxws-pluginwhich is available in maven central.

我正在尝试通过 gradle 脚本为 SOAP webservice 生成类。我正在使用gradle-jaxws-pluginmaven central 中提供的插件。

My script looks like below:

我的脚本如下所示:

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()
}

If I use this script as it is, I get following error

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

[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

One way of resolving this error, I tried is gradle build -Djavax.net.ssl.trustStore=cacerts -Djavax.net.ssl.trustStorePassword=changeit. This worked. But I want to pass these jvm properties in build script.

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

I tried systemProperty.set(), but it didn't work. I am trying with gradle.properties, but that doesn't work either. Is there a clean way to pass these properties? Also I am wondering how I will handle this in production when I will have an automated build.

我试过了systemProperty.set(),但没有用。我正在尝试使用gradle.properties,但这也不起作用。有没有一种干净的方法来传递这些属性?我也想知道当我有一个自动构建时,我将如何在生产中处理这个问题。

回答by Opal

Typically, since such data are sensitive they should be passed via command line or - if you have an automated build in production - should be configured in system via e.g. environment variables (this is how it's handled most often).

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

You can configure system properties via gradle.propertiesbut they should be prependwith systemPropprefix, so:

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

gradle.properties:

gradle.properties

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

Also the following piece of code put in build.gradlejust under applysection should work as well: build.gradle

也把下面这段代码在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')

回答by RanPaul

This should work

这应该工作

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
}