2017-10-07 69 views
0

我正在开发Spring项目。 我想从命令行加载凭据,而不是在代码中对它们进行叙述。我想执行这个命令的gradle无法将参数从gradle命令行传递到Spring项目(不是引导)

gradlew build -Dspring.datasource.username=tester 

,当我启动了Spring项目,该程序在断点处停止,我看变量是否宣战与否。我曾尝试使用-P而不是-D,但它仍然没有帮助。

我使用bmuschko插件远程部署spring应用程序我尝试过使用,但也没有成功。我通过使用Spring支持的System.getProperties()和Environment对象检查了java代码属性。

gradlew cargoredeployremote -Dspring.datasource.username=tester 

应用程序属性被成功加载。

重要事项:我看到很多教程如何制作它,但使用Spring Boot我只使用Spring中选定的组件。 例如:http://nixmash.com/post/passing-arguments-to-spring-boot - 这在我的情况下不起作用,因为我没有bootRun任务。

任何想法?我在步骤中错过了什么?

这里是我的build.gradle

group 'example' 
version '1.0.0' 


apply plugin: 'application' 
apply plugin: 'war' 
apply plugin: 'java' 
apply plugin: 'com.bmuschko.cargo' 
apply plugin: 'org.liquibase.gradle' 

compileJava.options.encoding = 'UTF-8' 

mainClassName = 'api.optic.config.WebAppInitializer' 
sourceCompatibility = 1.8 

buildscript { 
    repositories{ 
     jcenter() 
     mavenCentral() 
    } 
    dependencies{ 
     classpath 'com.bmuschko:gradle-cargo-plugin:2.2.3' 
     classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-RC3' 
     classpath 'org.liquibase:liquibase-core:3.4.1' 
     classpath "org.liquibase:liquibase-gradle-plugin:1.2.4" 
     classpath "mysql:mysql-connector-java:5.1.13" 
    } 
} 

project.ext { 
    springVersion = "4.3.6.RELEASE" 
    junitVersion = "5.0.0-RC3" 
} 

repositories { 
    mavenCentral() 
} 

dependencies { 

    compile "org.springframework:spring-core:${springVersion}" 
    compile "org.springframework:spring-context:${springVersion}" 
    compile "org.springframework:spring-context-support:${springVersion}" 
    compile "org.springframework:spring-beans:${springVersion}" 
    compile "org.springframework:spring-web:${springVersion}" 
    compile "org.springframework:spring-webmvc:${springVersion}" 
    compile "org.springframework:spring-orm:${springVersion}" 
    compile "org.springframework:spring-oxm:${springVersion}" 
    compile "org.springframework:spring-jdbc:${springVersion}" 
    compile "org.springframework:spring-test:${springVersion}" 
    compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.38' 

    compile group: 'javax.mail', name: 'javax.mail-api', version: '1.5.6' 


    compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25' 
    compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.2' 

    compile group: 'com.fasterxml.jackson.module', name: 'jackson-module-parameter-names', version: '2.9.0.pr2' 
    compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jdk8', version: '2.9.0.pr2' 
    compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310', version: '2.9.0.pr2' 
    compile 'javax.servlet:javax.servlet-api:3.1.0' 

    testCompile "org.junit.jupiter:junit-jupiter-api:${junitVersion}" 
    testRuntime("org.junit.jupiter:junit-jupiter-engine:${junitVersion}") 
} 

cargo { 
    containerId = 'tomcat8x' 
    port = 8080 

deployable { 
    context = 'example' 
} 

remote { 
    hostname = 'host.name.com' 
    username = 'tomcat' 
    password = 'pass' 
} 

回答

0

基本上是2个问题

名称不匹配:在application.properties中的$内部名称{}是与此不同的命令提供-line

解决方案:

application.properties 
spring.datasource.username=${username} 

和gradle这个命令行

gradle build -Pusername=tester 

2.点问题与gradle这个:

不能把

gradle build -Pspring.datasource.username=tester 

即使你有application.properties

spring.datasource.username=${spring.datasource.username} 

,因为您收到异常:

任务':processResources'的执行失败。

无法将文件'。\ src \ main \ resources \ application.properties'复制到'。\ build \ resources \ main \ application.properties'。

解决方案:

相反的点使用_标志

gradle build -Pspring_datasource_username=tester 

和应用性能

spring.datasource.username=${spring_datasource_username} 
相关问题