How can I configure flyway in build.gradle to get url ,username, password from other properties file?
Instead of this:
flyway {
url = 'jdbc:postgresql://localhost:5432/db'
user = 'a'
password = 'a'
locations = ['filesystem:db/migration']
}
something like this:
flyway {
path = ['filesystem:src/main/resources/data-access.properties']
locations = ['filesystem:db/migration']
}
You can do something like this:
ext.flywayProps = new Properties()
flywayProps.load(new FileInputStream(this.projectDir.absolutePath + "/src/main/resources/data-access.properties"))
In the root of your build script, it will load a properties file into local variable of Properties type. After that you can use this properties the way you need it, like so for example:
flyway {
url = 'jdbc:postgresql://flywayProps['dbIp']:flywayProps['dbPort']/db'
user = flywayProps['dbUsername']
password = flywayProps['dbPassword']
locations = ['filesystem:db/migration']
}
And in your data-access.properties you need to specify it as follows:
dbIp=localhost
dbPort=5432
dbUsername=a
dbPassword=a
Related
I was recently working on a MySQL database and wanted to use the database as my data source in KTOR. To use the database, I decided to use the org.jetbrains.exposed.sql.Database
and javax.sql.DataSource imports. I'm working in IntelliJ.
My test code looks like this:
import org.jetbrains.exposed.sql.Database
import javax.sql.DataSource
fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)
val databaseUrl = "jdbc:mysql://localhost:3307/databaseName"
val username = "root"
val password = " "
// Create a DataSource object
val dataSource: DataSource = Database.connect(
url = databaseUrl,
driver = "com.mysql.jdbc.Driver",
user = username,
password = password
)
Somehow, I can't import the org.jetbrains.exposed.sql.Database , even though I added the dependency in my build.gradle.kts file:
dependencies {
implementation("com.mysql.jdbc:mysql-connector-java:8.0.22")
implementation("org.jetbrains.exposed:exposed:0.18.7")
implementation("io.ktor:ktor-server-core:$ktor_version")
implementation("io.ktor:ktor-server-netty:$ktor_version")
implementation("io.ktor:ktor-server-content-negotiation:$ktor_version")
implementation("io.ktor:ktor-serialization-kotlinx-json:$ktor_version")
implementation("ch.qos.logback:logback-classic:$logback_version")
testImplementation("io.ktor:ktor-server-test-host:$ktor_version")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version")
implementation(kotlin("stdlib-jdk8"))
}
I tried syncing the gradle file, rebuilding the project and cleaning the project. Am I missing something? Thanks!
Ok so I solved it by using a different dependency in my build.gradle file:
implementation("org.jetbrains.exposed:exposed-core:0.41.1")
Instead of:
implementation("org.jetbrains.exposed:exposed:0.18.7")
I'm using the Spring Boot Gradle plugin to build and push Docker images to a remote Docker registry
bootBuildImage {
imageName = "docker-repo/app-name"
publish = true
docker {
publishRegistry {
username = project.property('repoUsername')
password = project.property('repoPassword')
}
}
}
The Docker repository credentials are stored in ~/.gradle/gradle.properties.
Is this secure? Would I need to store a similar ~/.gradle/gradle.properties file in the CI/CD environment?
What are the best approaches from a security perspective?
What I do think about this ( Opinion ) is NO .
Storing the credentials with gradle.properties as plain text is a bad idea . but still it widely usable .
For me , I'd rather use the environment variables as its one level above the plain text , where it get tricky to find .
I can assure you that both ways are bad , but i guess that environment variables is a bit more tricky than plain text .
this is my gradle method to retrive env variables or to look at gradle.properties
artifactoryUser = getConfigurationProperty("ARTIFACTORY_USER", "artifactoryUser", null)
artifactoryPwd = getConfigurationProperty("ARTIFACTORY_PWD", "artifactoryPwd", null)
String getConfigurationProperty(String envVar, String sysProp, String defaultValue) {
def result = System.getenv(envVar) ?: project.findProperty(sysProp)
result ?: defaultValue
}
I use it and I am sure that there is a more secure way to do so .
I use gradle with the two plugins com.jfrog.artifactory and io.swagger.core.v3.swagger-gradle-plugin .
Now I want to configure as described here https://github.com/swagger-api/swagger-core/tree/master/modules/swagger-gradle-plugin the generation of code. But it seems that the resolve task has already been defined from artifactory. How do I adress the method of swagger-plugin directly?
This is in my build.gradle:
resolve {
outputFileName = 'bananas'
outputFileName = 'PetStoreAPI'
outputFormat = 'JSON'
prettyPrint = 'TRUE'
classpath = sourceSets.main.runtimeClasspath
resourcePackages = ['io.test']
outputDir = file('test')
}
and this is the error message: Could not set unknown property 'outputFileName' for object of type org.jfrog.gradle.plugin.artifactory.dsl.ResolverConfig.
There is indeed a clash between Artifactory resolve extension and Swagger plugin resolve tasks (of type import io.swagger.v3.plugins.gradle.tasks.ResolveTask)
One way to solve this is to reference the swagger tasks explicitly using fully-qualified name, as follows:
io.swagger.v3.plugins.gradle.tasks.ResolveTask swaggerResolve = tasks.getByName("resolve")
swaggerResolve.configure {
outputFileName = 'PetStoreAPI'
outputFormat = 'JSON'
prettyPrint = 'TRUE'
classpath = sourceSets.main.runtimeClasspath
resourcePackages = ['io.test']
outputDir = file('test')
}
EDIT
Simpler solution , see Lukas's comment
tasks.resolve {
outputFileName = 'PetStoreAPI'
// ....
}
I have a grails application that reads from a property file. I created an administration page, that allows the user to view the settings in this file. I want to allow the user to update the various properties and save them back to the property file using Grails.
My problem is I cant seem to figure out how to save the changes back to the property file.
Here is my code that reads from the file and makes changes to the properties:
def env = params.getAt("environment");
if (env != null){
//Read from Property file based on user selection
def props = new Properties()
def cl = Thread.currentThread().contextClassLoader
def filepath = env+'.properties'
props.load servletContext.getResourceAsStream("/WEB-INF/"+ filepath)
Enumeration e = props.propertyNames();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
def input = params.getAt(key)
if (input != null){
props.setProperty(key,input)
}
}
//props.store(propFile.newWriter(), null)
}
Any Ideas on how to save/overwrite the example.properties file in /WEB-INF/example.properties?
I think that you might be looking for the getRealPath method in ServletContext
http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html#getRealPath(java.lang.String)
Simple example that shows you how to use it
http://82.157.70.109/mirrorbooks/javaservletjspcookbook/0596005725_jsvltjspckbk-chp-14-sect-7.html
In a maven war project, i use jetty-maven-plugin as developpement container.
i filtered some resources files, and in particular let's name it "bddconf.xml".
This file is filtered by maven and put in target/classes directory.
An old home-made bdd fwk search after this file with this snippet :
Properties properties = new Properties();
InputStream inputstream = properties.getClass().getResourceAsStream("/bddconf.xml");
When i run this snippet, in my webapp (in jetty), inputStream is null.
Whereas with this snippet, in the same method :
import com.google.common.io.Resources;
String file = Resources.getResource("bddconf.xml").getFile();
File file2 = new File(file);
logger.info("Does bdd file exists : [" + file2.exists() + "] file : [" + file2.toString() + "]");
// Does bdd file exists : [true] file : [..path..\target\classes\bbdconf.xml]
It works, so what's the difference between this two methods ?
May be Properties class and guava Resources class have different ClassLoaders (with different privileges), because Class.getResource() and Resources.getResource() do the same job for you. You can look at the sources of guava:
public static URL getResource(String resourceName) {
URL url = Resources.class.getClassLoader().getResource(resourceName);
checkArgument(url != null, "resource %s not found.", resourceName);
return url;
}
Class.getResource:
name = resolveName(name);
ClassLoader cl = getClassLoader0();
if (cl==null) {
// A system class.
return ClassLoader.getSystemResource(name);
}
return cl.getResource(name);