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

问题描述:

我正在尝试通过gradle脚本为SOAP webservice生成类。我正在使用一个插件gradle-jaxws-plugin,它在maven central中可用。如何在gradle构建脚本中传递trustStore属性

我的脚本看起来像下面:

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,但那也行不通。有没有一种简洁的方式来传递这些属性?另外我想知道如何在生产中处理这个问题,当我有一个自动构建。

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

您可以通过gradle.properties配置系统性能,但他们should be prependsystemProp前缀,所以:

gradle.properties

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

而且下面的代码放在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') 
+0

的build.gradle选项,我已经试过之前,但它不工作。 – yogsma

这应该工作,你提到

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 
}