spring - 无法将参数从Gradle命令行传递到Spring项目(不是Boot)

标签 spring gradle command-line arguments parameter-passing

我正在开发Spring项目。
我想从命令行加载凭据,而不在代码中讲述它们。我正在尝试执行此gradle命令

gradlew build -Dspring.datasource.username=tester

当我启动Spring项目时,程序在断点处停止,然后查看是否声明了变量。我尝试使用-P而不是-D,但仍然没有帮助。

我使用尝试使用的bmuschko插件远程部署spring应用程序,但也没有成功。我使用System.getProperties()和Spring支持的Environment对象 checkin 了Java代码Properties。
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'
}

最佳答案

基本上是2期
1. 名称不匹配:在application.properties中,$ {}中的名称与命令行提供的名称不同。
解决方案:

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}
因为出现异常:

Execution failed for task ':processResources'.

Could not copy file '.\src\main\resources\application.properties' to '.\build\resources\main\application.properties'.


解决方案:
代替点使用_号
gradle build -Pspring_datasource_username=tester
和在应用程序属性
spring.datasource.username=${spring_datasource_username}

关于spring - 无法将参数从Gradle命令行传递到Spring项目(不是Boot),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46618892/

相关文章:

javascript - 使用 node.js 的 Windows 命令行解释器

java - Spring 轮廓组

java - 在 DAO 类上使用事务注释时 Hibernate session 关闭

gradle - 项目无法在依赖项目中使用 SDK 依赖项构建

java - 使用 Gradle 的 Spring Boot 项目不运行 spring

c++ - rpm -q -> 只查询描述

c - 尚未创建的文件

java - 如何在响应正文中返回 ConstraintViolationException 消息?

java - Mule 3 Web 服务不返回堆栈跟踪

kotlin - 如何在Intellij IDEA中使用Gradle创建Kotlin控制台应用程序