I'm running JUnit tests with Maven profile.
Maven profile looks so:
<profile>
<id>someProfile</id>
<properties>
...
<some.param>some_value</some.param>
...
</properties>
</profile>
Spring context file(testContext.xml):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
...
xmlns:p="http://www.springframework.org/schema/p"
...
xsi:schemaLocation="...">
<bean id="someBean" class="someClass"
scope="singleton"
autowire="byName"
init-method="init"
p:someBeanParam="${some.param}"/>
</beans>
And test class begins so:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = {"classpath:/testContext.xml"})
#Configurable
...
After running maven, I saw that testContext.xml wasn't changed - p:someBeanParam still had value ${some.param}.
Could you tell, please, what's the problem here and how to solve it?
Thank you in advance.
Enable resource filtering like this
<project>
...
<build>
...
<resources>
...
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
...
</resources>
<testResources>
...
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
...
</testResources>
...
</build>
....
Define variable for each profile and when no profile is used.
More information on Maven Resource Plugin page.
Related
I am trying to exclude my data.sql and schema.sql from the Spring boot application jar.
So far I have tried several options but they do not seem to work. This is my POM configuration.
<profiles>
<profile>
<id>dev</id>
<properties>
<activatedProperties>dev</activatedProperties>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>prod</id>
<properties>
<activatedProperties>prod</activatedProperties>
</properties>
<build>
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
<excludes>
<exclude>data.sql</exclude>
<exclude>schema.sql</exclude>
</excludes>
</resource>
</resources>
</build>
</profile>
</profiles>
Spring Boot enables it by default and loads SQL from the standard locations schema.sql and data.sql.If you want to disable this you can try,
spring.datasource.initialize=false
in application.properties or if you want to change default scheme or data scripts locations you can do it with,
spring.datasource.schema=
spring.datasource.data=
You can write this annotation on top of your test method
#TestPropertySource(locations = "classpath:application-test.properties")
Your application-test.properties should have something like this
spring.datasource.data=classpath:/database/seed.sql
I have multiple TestSuites files.
And I want to set parameter variable in my suite files from POM file. But I couldn't get any suggestion from Google search.
Could anyone help me how can I perform it.
For example.
ABC.xml [File #1]
<parameter name="property_file" value="C:/a.properties" />
DEF.xml [File #2]
<parameter name="property_file" value="C:/a.properties" />
And I want to set Property_file parameter value from Maven pom.
I want to set "C:/123.properties" for File #1 and "C:/456.properties" for File #2.
My suite file may look like_
<suiteXmlFiles>
<suiteXmlFile>ABC.xml</suiteXmlFile>
<suiteXmlFile>DEF.xml</suiteXmlFile>
</suiteXmlFiles>
I don't want to set parameter in TestNG file, I want to do it from POM file.
Is it possible?
Yes, it is possible. You can filter with the maven ressources plugin. Have a look:
https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html
If the article doesn't answer all your questions, don't hesitate to ask.
In summary you can do something like this:
<project>
...
<properties>
<suiteXmlFile>ABC.xml</suiteXmlFile>
</properties>
...
<build>
...
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
...
</resources>
...
</build>
...
</project>
and in your xml file within the resources folder you use
<parameter name="property_file" value="${suiteXmlFile}" />
What is not possible (at least in a simple way) is to implement a maven magic that interprets the same property differently within each xml file. In this case you have to use differently named properties. (e.g. suiteXmlFile1, suiteXmlFile2)
#niklas-p 's answer is good but in the case of test resources, you should use testResources instead.
<project>
...
<properties>
<property_file>C:/a.properties</property_file>
</properties>
...
<build>
...
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
...
</testResources>
...
</build>
...
</project>
Then, the filtered resources will be located in the ${project.build.testOutputDirectory} directory:
<suiteXmlFiles>
<suiteXmlFile>${project.build.testOutputDirectory}/ABC.xml</suiteXmlFile>
<suiteXmlFile>${project.build.testOutputDirectory}/DEF.xml</suiteXmlFile>
</suiteXmlFiles>
And in suite files:
<parameter name="property_file" value="${property_file}" />
mvn -P dev
If I build my project using profile dev, then I want to use dev.properties in my spring bean like below. Is it possible ? If so , how could I get profile name ?
<bean id="xyz" class="abc.xyz">
<property name="propertyFile" value="${maven_profile_id}.properties" />
</bean>
Thanks in advance.
You can use Maven profiles to add a 'profile' property to the build:
<profiles>
<profile>
<id>dev</id>
<properties>
<profile>dev</profile>
</properties>
</profile>
</profiles>
Then pass the value into your application using a system property, here's an example with surefire:
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<profile>${profile}</profile>
</systemPropertyVariables>
</configuration>
</plugin>
Finally this can be referenced in you application:
<bean id="xyz" class="abc.xyz">
<property name="propertyFile" value="${profile}.properties" />
</bean>
Alternatively, if you are using Spring 3.1 or later you might find the XML profile feature meets your needs (although it may be overkill).
Create a properties file that will be populated using Maven's resource filtering that specifies the profile you are using at build time.
build.properties
activatedProfile=${profileId}
pom.xml (You don't need to filter the complete directory, customise as required)
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resources>
</build>
Add a profileId (or whatever you want to call it) property under each different profile:
<profile>
<id>dev</id>
<properties>
<profileId>dev</profileId>
</properties>
</profile>
<profile>
<id>qa</id>
<properties>
<profileId>qa</profileId>
</properties>
</profile>
You can then use ${activatedProfile}.properties as value for a bean
<bean id="xyz" class="abc.xyz">
<property name="propertyFile" value="${activatedProfile}.properties" />
</bean>
When copying resources with filtering in Maven, I want to add the unique snapshot version to a resource file.
I have enabled filtering:
...
<name>My Application</name>
<version>0.1-SNAPSHOT</version>
...
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
...
</resource>
</resources>
...
</build>
This is my resource file:
application.name=${name}
application.Version=${version}
After running mvn resources:resources, this is the result:
application.name=My Application
application.Version=0.1-SNAPSHOT
But what I actually want is this:
application.name=My Application
application.Version=0.1-20120310.143733-1
Since version 2.1.0-M1, Maven supports special syntax to get build timestamp in pom.xml, check out the doc here:
<project>
...
<properties>
<maven.build.timestamp.format>yyyyMMdd.HHmmss</maven.build.timestamp.format>
</properties>
...
</project>
Alternatively, you can use buildnumber-maven-plugin.
at my project there is 2 profiles and each profile has one property.
But I could not use master's properties at child's resources.
Here is described clearly but it seems that there is only one pom file and the sample shown at there is not an multi-module maven project.
All I want to do is use this properties at spring level like changing the location of properties file.
<profile>
<id>p1</id>
<properties>
<properties.location>file:/apps-core1.properties</properties.location>
</properties>
</profile>
<profile>
<profile>
<id>p2</id>
<properties>
<properties.location>file:/apps-core2.properties</properties.location>
</properties>
</profile>
<profile>
and I want to use "properties.location" at every pom file I had, either main-resources or test-resources.
here is the spring usage
<context:property-placeholder location="\${properties.location}" />
i got it working with a multi-module project with:
parent pom with <modules /> config and your profiles
module pom with following config
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
see maven resources and filtering