Multiple Spring profiles and Maven profiles in one war - java

I got parser Spring Batch sheduled jobs. I got many jobs and here are just two of them:
#Profile("eodT0")
#EnableBatchProcessing
#Configuration
public class JobConfigurationEodT0 { ... }
#Profile("eodRepo")
#EnableBatchProcessing
#Configuration
public class JobConfigurationEodRepo { ... }
My application.yml config:
spring:
profiles.active: #activatedProperties#
And pom.xml:
Some profiles from big list of them:
<profiles>
<profile>
<id>eodT0</id>
<properties>
<activatedProperties>eodT0</activatedProperties>
</properties>
</profile>
<profile>
<id>eodRepo</id>
<properties>
<activatedProperties>eodRepo</activatedProperties>
</properties>
</profile>
</profiles>
I want it to be like
spring:
profiles.active: eodT0, eodRepo instead of #activatedProperties#
If I check more than one maven profile only one will work. I need to check profiles depending on what jobs I want to do to make them all active to make war and put it on server. How is it to be done?

Why not pass it as a system property.
mvn package -DactivatedProperties=eodT0,eodT1

Related

Activate profiles for spring boot test using application.properties file

I have been using Spring Boot and TestNG for my test framework and so far my tests were configured to use only one default application.properties file which is under src/main/resource. Now I want to configure them for different environments - ci/stage etc. I have used spring documentation to activate the profiles from pom.xml file.
<profiles>
<profile>
<id>ci</id>
<properties>
<activeProfile>ci</activeProfile>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
I have for two properties files under src/main/resources - application.properties and application-ci.properties. (The is the naming convention suggested by spring documentation. application-{activatedprofile}.properties).
The application.properties have got a placeholder -
spring.profiles.active=#activeProfile#
The #activeProfile# will get replaced with the value of activeProfile in the pom.xml file.And uptil that it is working.
In my #Configuration class I have a annotation as below and I am expecting that the ${spring.profiles.active} value gets replaced with value - ci.
#PropertySource("classpath:application-${spring.profiles.active}.properties")
I am getting following error:
java.lang.IllegalArgumentException: Could not resolve placeholder
'spring.profiles.active' in value
"classpath:application-${spring.profiles.active}.properties"
I am using maven and testng to run my project. I am doing something incorrect let me know how can I resolve it.
First of all, the maven profile is not the same as the spring profile. In the code snippet provided you are setting the maven profile, not the spring profile.
To pass a specific spring profile during your test phase you can use the surefire plugin. In the code snippet below you would be passing in the system property spring.profiles.active as ci. This is equivalent to setting the value in your application.properties file.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
<configuration>
<systemPropertyVariables>
<spring.profiles.active>ci</spring.profiles.active>
</systemPropertyVariables>
</configuration>
</plugin>
Secondly, spring will automatically load the property sources based on the active spring profile. In your example, spring will first load application.properties then it will apply application-ci.properties on top of it. As a result
#PropertySource("classpath:application-${spring.profiles.active}.properties")
is not needed.
If you have a configuration class that is specific to an active profile then you can add #ActiveProfiles("ci") to your configuration class and it will only use that class when the profile ci is active.
Lastly, you do not need the property spring.profiles.active=#activeProfile# in your application.properties files as this be passed in from the surefire plugin in maven.

get value config server spring boot

I use spring boot and jboss eap 6.4 for deploying application. in my pom.xml set config server. the file name is letter-printing-eap-generator.yml. this file contains value. how to get the data from this file? or can you give me the references? because I had find but no one match with my case.
pom.xml:
<properties>
<config.server>http://10.170.49.103/configserver</config.server>
</properties>
<plugin>
<groupId>org.jboss.as.plugins</groupId>
<artifactId>jboss-as-maven-plugin</artifactId>
<version>7.9.Final</version>
<configuration>
<jbossHome>${jboss.home}</jbossHome>
<serverArgs>
<serverArg>-Dspring.profiles.active=${run.profiles}</serverArg>
<serverArg>-Dspring.cloud.config.uri=${config.server}</serverArg>
</serverArgs>
</configuration>
</plugin>
application.properties:
spring.application.name=letter-printing-eap-generator
bootstrap.yml:
spring.jmx.default-domain: letter-printing-eap-generator
#Service
public class SomeServiceServiceImpl implements SomeService{
#Value("${letter-printing-eap-generator}")
private String letterPrintingEapGenerator;
//methods
}
In spring boot thereis annotation #Value
You can use it to get values from you properties files. It works like this: #Value("${letter-printing-eap-generator}")

Why use -D instead of -P to activate a maven profile?

e.g.
<profiles>
<profile>
<id>rofileX</id>
</profile>
</profiles>
The profile could have been activated using -ProfileX, why am I seeing people bother to add in an activation property, e.g.
<profiles>
<profile>
<id>rofileX</id>
<activation>
<property>
<name>oX</name>
</property>
</activation>
</profile>
</profiles>
to allow activation using -DoX?
Is there any reason to use -D insteal of -P? Or you can achieve more things with -D as oppose to -P?
Thank you
One usage of -Dmyproperty instead of -Pmyprofile is to activate several profiles using only one parameter.
Moreover, you can reuse the -Dmyproperty in other parts of your POM, using ${myproperty}.
The property does not necessarily has to be set via the -D maven option, it may also be a system property. So it is possible to switch between profiles without changing the invocation.
[edit]
from theMaven documentation:
-D, --define <arg>
Defines a system property
...
Properties defined on the command line are also available as properties to be used in a Maven POM or Maven Plugin

Configure Java EE 6 for dev/QA/prod

I have a Java EE 6 app that I build with Maven, code in NetBeans 7 and deploy on GlassFish 3.1.2. As I near completion, I find myself deploying demo builds.
The problem is that I don't have any dead easy way to build for different environment such as dev, QA, demo, prod, etc. For some stuff, I've been using a Java class with a bunch of static getters that return values based on the value of an environment constant. But this doesn't help me with conditionally setting
javax.faces.PROJECT_STAGE (web.xml)
database credentials (glassfish-resources.xml)
mail servers (glassfish-resources.xml)
JPA logging level (persistence.xml)
and probably a number of other things I can't think about now that are scattered across XML files.
Is there any way to define multiple versions of these configuration files and just set a flag at build time to select the environment, while defaulting to dev when no environment is specified? Is there a way I could make Maven work for me in this instance?
You can use maven to achieve that. Especially using resource filtering.
First, you can define list of profiles:
<profiles>
<profile>
<id>dev</id>
<properties>
<env>development</env>
</properties>
<activation>
<activeByDefault>true</activeByDefault> <!-- use dev profile by default -->
</activation>
</profile>
<profile>
<id>prod</id>
<properties>
<env>production</env>
</properties>
</profile>
</profiles>
Then the resources that you need to filter:
<build>
<outputDirectory>${basedir}/src/main/webapp/WEB-INF/classes</outputDirectory>
<filters>
<filter>src/main/filters/filter-${env}.properties</filter> <!-- ${env} default to "development" -->
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
And then your custom properties based on profiles in src/main/filters directory:
filter-development.properties
# profile for developer
db.driver=org.hsqldb.jdbcDriver
db.url=jdbc:hsqldb:mem:web
and
filter-production.properties
# profile for production
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/web?createDatabaseIfNotExist=true
to use production profile, you can package war using mvn clean package -Pprod command.
Here you can see the sample project that use profile in maven.
This is not direct response to question. This explain diff strategy to manage env properties
One other way to manage properties for diff env is using the database to store the properties. This way you have only need to manage the config of the DB. Based on which DB you are pointing you can load the properties from that DB. If you are using spring than spring provides PropertyPlaceholderConfigurer which can initialize the properties from DB. This approach allows you to change the property value without doing a build.
This approach is useful if you want to promote the artifact tested by QA\Testing team. In this case DB configuration will not be part of artifact generated by build process.
If you need to configure web.xml check this how-to:
https://community.jboss.org/docs/DOC-19076
It uses same method (resource filtering) as described in another answers.

Maven/Netbeans Have different testing profiles and environment variables

We are trying to do Selenium tests for our java web application. We would like to be able to quickly configure our tests with a combination of the java function "System.getProperty" and modifying the profiles available for the test.
The reason for doing this is so that we can test different servers with a simple change of the dropdown box in netbeans (from < default config > to "server name/details"). Our servers run things like a Snapshot, RC, and hotfix branch so this would be very helpful in tracking down when bugs are introduced.
Our current method for doing this is modifying the test variables before each run to target the server we want (yuck!).
Any thoughts would be helpful
The simplest mechanism might well be to create profiles in your POM that are activated by a system property or environment variable.
An example from the Maven documentation (with small modifications for clarity):
<profiles>
<profile>
<activation>
<property>
<name>myProperty</name>
<value>test</value>
</property>
</activation>
...
</profile>
</profiles>
Which you'd activate manually with:
mvn groupId:artifactId:goal -DmyProperty=test
You'd enable this based on an environment variable by using the fact that Maven maps environment vars into properties with the name env.… (with case normalization on Windows):
<profiles>
<profile>
<activation>
<property>
<name>env.ENVIRONMENT</name>
<value>test</value>
</property>
</activation>
...
</profile>
</profiles>
Which you'd activate with:
export ENVIRONMENT=test # Does not need to be done every build, just per session
mvn groupId:artifactId:goal

Categories

Resources