如何在build.gradle文件中设置属性以在Java中访问该属性。

huangapple go评论62阅读模式
英文:

how can i set property in build.gradle file to access that property in Java

问题

I am using "sourcemuse/GradleMongoPlugin" gradle plugin. It is just for managed instance of Mongo from our gradle build.
port 'RANDOM' running on random port. As per the documentation this port will be available in project.mongo.port.
how can I access this "project.mongo.port" in Java classes?

build.gradle

plugins {
id 'com.sourcemuse.mongo' version '1.0.7'
}
mongo {
port 'RANDOM'
logging 'console'
}

Ref: https://github.com/sourcemuse/GradleMongoPlugin

英文:

I am using "sourcemuse/GradleMongoPlugin" gradle plugin. It is just for managed instance of Mongo from our gradle build.
port 'RANDOM' running on random port. As per the documentation this port will be available in project.mongo.port.
how can I access this "project.mongo.port " in Java classes ?

build.gradle

  plugins {
    id 'com.sourcemuse.mongo' version '1.0.7'
  }
  mongo {
    port 'RANDOM'
    logging 'console'
   }

Ref : https://github.com/sourcemuse/GradleMongoPlugin

答案1

得分: 1

"It looks like this may help you: https://stackoverflow.com/a/40869506/12833948"

As @riccardo.cardin stated, you can autowire IMongodConfig and it will provide you the port number - mongoConfig.net().getPort()

If that doesn't work, try: System.getProperty("project.mongo.port")

If this property is not set, you can try to set it yourself.

In build.gradle, something like:

System.setProperty("project.mongo.port", project.mongo.port)

In Java

System.getProperty("project.mongo.port")
英文:

It looks like this may help you: https://stackoverflow.com/a/40869506/12833948

As @riccardo.cardin stated, you can autowire IMongodConfig and it will provide you the port number - mongoConfig.net().getPort()

If that doesn't work, try: System.getProperty("project.mongo.port")

If this property is not set, you can try to set it yourself.

In build.gradle, something like:

System.setProperty("project.mongo.port", project.mongo.port)

In Java

System.getProperty("project.mongo.port")

huangapple
  • 本文由 发表于 2020年7月29日 00:36:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63138826.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定