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.
Related
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}")
Is it possible to disable settings.xml for particular project build using pom.xml?
I want to avoid using repositories specified in settings.xml file but only for one project.
Repositories are defined inside profiles in settings.xml file. You can activate and deactivate profiles based on some criteria. See documentation regarding profile activation.
You could:
deactivate given profile per project by passing -P !profile-name
you could define the profile as
<activation>
<property>
<name>skipThisProfile</name>
<value>!true</value>
</property>
</activation>
and then run your project with -DskipThisProfile
define the profile as
<activation>
<file>
<missing>.doNotRunProfile</missing>
</file>
</activation>
and will not run for any project where there is .doNotRunProfile file
I want to use Maven to handle artifact generation for the different local and testing regions. I believe I can use different profiles but I am not certain.
In Maven can I select different directories to select files used on packaging (such as application.properties)? How would I set that up?
An idea of what I want is to have a the following folders for resources in my project
local
build server
dev
sys
prod
Each folder should contain a different version of application.resources which is a file in Spring that can be used to handle hard-coded strings for use in variables. For local builds- our developers also work on different operating systems. Should I require I want to make it seamless on different OS' also.
Key outcomes would be:
Control Maven lifecycle phases from inside the IDE (IntelliJ)
Not complicate phases and team processes
Keep things as consistent for each developer
Make the different configurations per developer/region appear invisible when running a phase e.g. install
Ideally I would have my project set up according to best practices (Duvall, Matyas, Glover).
We provide different properties currently but not by way of different folders. We do this
via a mix of
Spring's PropertyPlaceholderConfigurer
Maven profiles (something we use to build our Dev environment),
Build Server (TeamCity in our case)
Maven phases to produce the correct artifact
start-up and build arguments
My understanding of what we do is limited, but hopefully this serves as a useful example for others and maybe myself to consider.
We provide parameters, as you'll see below, to point to different property files.
Each property file has configuration for a region/environment. I'll explain the current use
as best I can in-case it provides some use to others.
To use Maven profiles we have created a profile in our pom identified as development which includes a region configuration property called env. I don't yet know entirely how that is being used yet in our project however you'll see below our POM includes the a Maven Compiler plugin and a Maven Tomcat plugin.
Day to day, as developers we run our our applications locally on Tomcat from within IntelliJ
and provide the env property. On start-up the env property is provided as an argument to
set to classpath*:dev-common.properties.
This file is a properties configuration file - setting placeholder values for our different
regions.
The value of env is made available to our PropertyPlaceholderConfigurer
Example 1 - Implementation of Maven profile in pom.xml:
The implementation of a profile in our pom is:
<profile>
<id>development</id>
<activation>
<property>
<name>env</name>
<value>development</value>
</property>
</activation>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0-SNAPSHOT</version>
...
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
Example 2 - Property placeholder configurer for normal build:
We also make use ofa Spring component, a PropertyPlaceholderConfigurer. We use this in collaboration with a build argument to set up a classpath pointer to resource files.
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>
${env}
</value>
</list>
</property>
Example 3 - Property placeholder configurer for test:
We have Spring Contexts specifically set up for integration testing which also use the PropertyPlaceholderConfigurer. These are picked up by a integration testing class using a combination of #ContextConfiguration(locations = {"classpath:test-dataexchange-application-context.xml"}) and #RunWith(SpringJUnit4ClassRunner.class)).
In the testing context we configure the PropertyPlaceholderConfigurer as follows to pick up the properties of an integration testing region:
<bean id="testpropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:dev-local-common.properties</value>
</list>
</property>
Other notes:
We make use of Team City for build management but I have not seen how these settings are
used there, if they are at all. I can conceive there's an ability to combine the above approaches together to aid Continuous Integration and Delivery.
I do not see where the profile identified as development is being used. It is something I
must follow up with my fellow team members.
Resources:
Building for different environments at the Maven Project site.
Maven 3 does not allow configuration of a profile outside of a pom or settings.xml (the Maven configuration file) and says that users who used these external settings should now put them inside of settings.xml
If you are using Spring boot, there is an easy way of doing this.
Create two profiles in maven, and set a property in each profile with the name of the Spring profile you want to execute.
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<!-- Default spring profile to use -->
<spring.profiles.active>dev</spring.profiles.active>
<!-- Default environment -->
<environment>develop</environment>
</properties>
</profile>
Inside your application.properties, add this property:
spring.profiles.active=${spring.profiles.active}
Create an application.property for each profile, using this pattern application-profile.properties. For example:
application-dev.properties
application-prod.properties
Be sure to active filtering in the resource plugin:
...
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
...
Another way is to create a file during the execution of maven called activeprofile.properties. Spring boot looks this file to load the active profile. You can create this file as follows:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>prepare-package</phase>
<configuration>
<target>
<echo message="spring.profiles.active=${spring.profiles.active}" file="target/classes/config/activeprofile.properties" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<configuration>
</configuration>
</plugin>
Aim to generate an artifact for each environment at one time on the central server (CI/Build server), aim to generate an artifact and start/test the application with one click locally, provide a consistent easy to learn way to check out and run your build, and check in and configure your CI.
You can use profiles in Maven and utilize Maven targets to achieve the right build using a build server which in our case is TeamCity.
Use property placeholder configurer in Spring context with an application.resources file for each region and a filename mask e.g. application-resources-${region}.
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.
I'm preparing a maven2 web project for continuous integration. I use the maven cargo plugin to automatically deploy the WAR to Tomcat6x before running integration tests.
My code depends on some system properties which are set with MAVEN_OPTS=-Dfoo=bar. Unfortunately these properties are missing when the application is deployed to Tomcat:
System.getProperty("foo"); // null, when deployed to container by maven-cargo
How can I pass these properties to Tomcat?
You should be able to do this by using the systemProperties tag in the container definition of the plugin:
<container>
[...]
<systemProperties>
<MAVEN_OPTS>-Dfoo=bar</MAVEN_OPTS>
</systemProperties>
</container>
Or you can set this in a setenv.sh (on linux) file in your $CATALINA_HOME/bin/ directory. If this file does not exist you should create it and add the following line:
MAVEN_OPTS=-Dfoo=bar
Hope this helps.
You should be able to do this by using the systemProperties tag in the container definition of the plugin:
<container>
<systemProperties>
<foo>bar</foo>
</systemProperties>
</container>
this is equivalent to pass -Dfoo=bar in command line or in another option.