Spring profiles with Gradle - java

I'm running a spring boot project with three different property files (application.properties, application-prod.properties, application-uat.properties. I'm using Maven and if I don't specify any argument, it will pick the default property file which is application.properties. Here's how the related files look.
application.properties
#Set active profile
spring.profiles.active=#activatedProperties#
#MongoDB configuration
spring.data.mongodb.host=${MONGODB_HOST:localhost}
spring.data.mongodb.port=${MONGODB_PORT:27017}
spring.data.mongodb.database=database
spring.data.mongodb.username=username
spring.data.mongodb.password=password
pom.xml
<profiles>
<profile>
<id>UAT</id>
<properties>
<activatedProperties>uat</activatedProperties>
</properties>
</profile>
<profile>
<id>PROD</id>
<properties>
<activatedProperties>prod</activatedProperties>
</properties>
</profile>
</profiles>
How can I achieve the same with Gradle? How can I let Gradle to pick the default (application.properties) if I don't specify any arguement?

I think this link will be useful for you...
https://docs.gradle.org/current/userguide/migrating_from_maven.html#migmvn:profiles_and_properties
Sample:
if (!hasProperty('buildProfile')) ext.buildProfile = 'default'
apply from: "profile-${buildProfile}.gradle"
task greeting {
doLast {
println message
}
}
#profile-default.gradle
ext.message = 'foobar'
#profile-test.gradle
ext.message = 'testing 1 2 3'
#profile-prod.gradle
ext.message = 'Hello, world!'
This link can be useful too:
https://www.baeldung.com/spring-profiles

Related

Maven activeByDefault profile being overwritten by another profile

I am trying to use Maven for a Java native project, using it to determine whether the current system is 64 bit or 32 bit. So that if the system is 32 bit, the following tag
<linkerStartOption>\foo\${the.folder}\Microsoft SDK\lib</linkerStartOption>
has the value \foo\Program Files\Microsoft SDK\lib, or if the system is 64 bit, have the link be the value \foo\Program Files (x86)\Microsoft SDK\lib.
The problem is currently my project uses <activeByDefault> tag to set a release version profile, which is being overwritten when the profile for determining 32 bit or 64 bit system is activated. This caused the properties determined by the release profile to not be available and cause the Java native build to fail. The release profile is used for release and there is also a debug profile used for debugging, which uses different <linkerStartOption> flags, so I don't want to just have release profile flags always be active.
My question is if there is a way for my native Java application to determine the 32-bit Windows Program Files path without deactivating the release profile triggered as <activeByDefault>?
Here is my profile for determining system architecture:
<profile>
<id>architecture</id>
<activation>
<os>
<family>Windows</family>
<arch>amd64</arch>
</os>
</activation>
<properties>
<the.folder>${env.PROGRAMFILES(X86)}</the.folder>
</properties>
</profile>
Here is the release profile being overwritten:
<profile>
<id>release</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<options>/foo -bar</options>
</properties>
</profile>

How to define a system property in maven profile? [duplicate]

I have 2 maven2 profiles, selenium and jspc. Now for "selenium" id'd like to have an implicit activation of "jspc", so that I don't have to write mvn -Pselenium,jspc from the command line. Is this possible ?
You can't "chain" profile activations (maven reference) but you can activate them both through the same property:
<activation>
<property>
<name>profile.selenium</name>
</property>
</activation>
And the run mvn -Dprofile.selenium

How use maven profile propertied in project config.properties

I'd use different environments to run testng tests. In pom.xml I specified my profiles like:
<profiles>
<profile>
<id>local</id>
<properties>
<browser.name>firefox</browser.name>
<site.url>my_url1</site.url>
</properties>
</profile>
<profile>
<id>remote</id>
<properties>
<browser.name>chrome</browser.name>
<site.url>my_url2</site.url>
</properties>
</profile>
</profiles>
In config.properties I set following:
browser=${browser.name}
siteUrl=${site.url}
In java code I read properties like:
public static String getProp(String name) {
Properties properties = new Properties();
try {
properties.load(PropertyReader.class.getResourceAsStream("/config.properties"));
} catch (IOException e) {}
String value = "";
if (name != null) {
value = properties.getProperty(name);
}
return value;
}
When I try to run tests using mvn test -Plocal I got errors because properties are not read correctly and browser is equal "${browser.name}" instead of "firefox". How could I fix this?

How to switch conf tomcat with maven

I have ,in a java high envirgure project using Maven, to add integrations test. To do that, I must add a new test's database (minimalist true copy of the database). Until then it's good.
By against the problem is that I have two differents configurations in my file:
\web\target\tomcat\conf\context.xml
So I always put a ressource in a comment.
exemple of ressource :
<Resource
auth = "jdbc / soarepo"
driverClassName = "oracle.jdbc.driver.OracleDriver"
logAbandoned = "true"
maxActive = "32"
maxIdle = "32"
maxWait = "10000"
name = "jdbc / soarepo"
password = "..."
username = "..."
removeAbandoned = "true"
removeAbandonedTimeout = "60"
type = "javax.sql.DataSource"
url = "jdbc: oracle: thin: # ..."
/>
Q: How do I switch from one to another ressource with Maven?
I already use a profile for integration tests. So I have to change what resources when the profile is called ?
If you are using the Maven Tomcat Plugin to run your web application, you can specify the path of a custom server-dependent deployment descriptor, context.xml, using the contextFile property:
<profiles>
<profile>
<id>integration-tests</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<contextFile>/path/to/context/file</contextFile>
</configuration>
</plugin>
</plugins>
</build>
</profile>
.. Other profiles ..
</profiles>

How to read properties file from one project into another project?

I have two java projects A and B, If I have a credentials.properties file in A and I want to access the same properties file in project B, Is there a way I can achieve this?
The two projects are maven build.
Try this:
<resource>
<directory>${other projects dir}/src/main/resources</directory>
<includes>
<include>*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
Give the path as a full path
Keep the property file in the class path of B and set Project B as a dependency to project A.
Commiting cleartext passwords into your source control is normally a bad idea...
How about using a shared Maven profile in your settings file? ($HOME/.m2/settings.xml):
<settings>
..
<profiles>
<profile>
<id>credentials</dev>
<activeByDefault>true</activeByDefault>
<properties>
<password1>XXXXX</password1>
<password2>YYYYY</password2>
..
..
</properties>
</profile>
..
</profiles>
..
</settings>
This approach is more Maven friendly and encryption is supported.
If you use Jenkins to build your code, you can use a plugin to manage the settings file centrally:
How to manage maven settings.xml on a shared jenkins server?
Your project can still have a default value, the key point is that the real passwords are set externally to files under source control.

Categories