Maven project.build.testSourceDirectory property not working from profile - maven

I want to make testing for different folders in a maven project and I need to change the maven's project.build.testSourceDirectory property.
I'm using a maven profile for this problem.
My profile looks like this:
<profiles>
<profile>
<id>sahi_UI_testing</id>
<activation>
<property>
<name>sahiTesting</name>
<value>true</value>
</property>
</activation>
<properties>
<maven.test.skip>false</maven.test.skip>
<project.build.testSourceDirectory>src/test/java/org/package1/package2/sahi</project.build.testSourceDirectory>
</properties>
</profile>
</profiles>
The project.build.testSourceDirectory isn't changed, only remains the default /home/username/workspace/projectName/core/src/test/java (I've tested this with maven-antrun-plugin and gives that path).
I have multiple pom.xml in the projects, so in the path I have the ../core/.. folder (this is the project's core pom.xml).
The maven-antrun-plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>******** Displaying value of property ********</echo>
<echo>${project.build.testSourceDirectory}</echo>
<echo>${maven.test.skip}</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
Executing mvn help:active-profiles -f core/pom.xml -Dtest=JavaClientTest -o -e test -DsahiTesting=true with <maven.test.skip>true</maven.test.skip> in maven-antrun-plugin this gives me:
[INFO] Executing tasks
[echo] ** Displaying value of property **
[echo] /home/username/workspace/projectName/core/src/test/java
[echo] true
and with <maven.test.skip>false</maven.test.skip> in maven-antrun-plugin this gives me:
[INFO] Executing tasks
[echo] ** Displaying value of property **
[echo] /home/username/workspace/projectName/core/src/test/java
[echo] false
So, we can see that the another variable was changed.
I know that the profile was activated because I used maven-help-plugin to identify this.
The maven-help-plugin gives me this result:
The following profiles are active:
sahi_UI_testing (source: pom)
I've tried without maven's profile to change the project.build.testSourceDirectory property only in the <build> tag.
...
<build>
<testSourceDirectory>src/test/java/org/package/package2/sahi</testSourceDirectory>
...
</build>
There the property was changed (but I need to assign more than one value to that property).
I've tried the maven-surefire-plugin and doesn't work too.
The question is that why the project.build.testSourceDirectory isn't changed when using the profile?

I found a not very nice solution to this question but it's a solution. For me it's working and it changes the testSourceDirectory variable.
In the pom.xml file I've declared an own variable in the properties tag initialized with the testSourceDirectory's default value:
<project ...>
...
<properties>
<myTestSourceDirectory>${project.basedir}/src/test/java</myTestSourceDirectory>
...
</properties>
...
</project>
My profile is:
<profiles>
<profile>
<id>sahi_UI_testing</id>
<activation>
<property>
<name>sahiTesting</name>
<value>true</value>
</property>
</activation>
<properties>
<maven.test.skip>false</maven.test.skip>
<myTestSourceDirectory>src/test/java/org/<package1>/<package2>/sahi</myTestSourceDirectory>
</properties>
</profile>
</profiles>
At the start of build tag I've set the testSourceDirectory to
<project ...>
<build>
<testSourceDirectory>${myTestSourceDirectory}</testSourceDirectory>
...
<build>
...
</project>
So, when we don't use the profile we have a default value to the myTestSourceDirectory variable and when we use a the profile the variable will be changed to the requested testing directory. The variable always exists, and in the build tag we change the testSourceDirectory property to the desired value.

You can't overwrite the project model with properties, Maven doesn't allow that. The only option you have is by specifying the testSourceDirectory in the plugin you want to use (assuming it's available).

Related

activate spring boot profile from maven profile

I want to separate my junit test and integration test separate. So I created a separate profile in pom.xml for the integration test as follows:
<profiles>
<profile>
<id>integration-test</id>
<properties>
<test>IntegrationTestTrigger</test>
<spring.profiles.active>integration-test</spring.profiles.active>
</properties>
</profile>
<profiles>
The when I run the maven command mvn test -Pintegration-test, it is picking the test class as defined in the <properties> tag shown above as IntegrationTestTrigger. But it is not setting the spring.profiles.active property. So the test is starting with default profile. It is working fine with the maven command mvn test -Dtest=IntegrationTestTrigger -Dspring.profiles.active=integration-test
But as per my organisations jenkins setting, I need to run mvn test -Pintegration-test for the integration test, so I cannot add any extra environment variables to mvn command
Indeed as #gtivari333 said, profile/properties section is only to be used for substitution in POM files (and other files processed by maven, if so desired).
To set JVM properties aka "system properties" in POM directly, for use during test execution, you need to set them using surefire plugin configuration like this:
<profiles>
<profile>
<id>integration-test</id>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<configuration>
<systemProperties>
<foo>bar</foo>
<spring.profiles.active>integration-test</spring.profiles.active>
</systemProperties>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
The properties at the is meant for property substitution at the .properties/.yml file inside resources folder.
Example:
application.yml:
spring:
profiles:
active: '#spring.profiles.active#'
pom.xml:
<profile>
<id>dev</id>
<properties>
<spring.profiles.active>dev</spring.profiles.active>
</properties>
</profile>
Here, the #spring.profiles.active# will be replaced with dev during compile(by maven-resources-plugin plugin). Spring Boot uses # as the resource delimiter at the spring-boot-starter-parent pom. You can change it to any character by changing the following property
//pom.xml
<project .....>
<properties>
<resource.delimiter>#</resource.delimiter>
...
</properties>
See https://github.com/gtiwari333/spring-boot-blog-app/blob/master/pom.xml#L436 for an complete example
See also: https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto-automatic-expansion-maven

Default property value in plugin configuration when parent POM not define that property

Here is my scenario
I have a POM file which using a custom plugin we have developed lets say mycutom-plugin and that plugin use another plugin called asciidoctor-maven-plugin
So I'll note down my concern here
1. I have a parameter(asciidoctor.sectnumlevels) in asciidoctor-maven-plugin as a configuration property please see below xml and this parameter value is passed by parent POM that this plugin being used.
Now what I wanted have a default value set for ${asciidoctor.sectnumlevels} from mycutom-plugin's POM when I don't pass value from parent POM that plugin being used
<plugin>
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctor-maven-plugin</artifactId>
<version>${asciidoctor-maven-plugin.version} </version>
.......
<attributes>
<toc2>left</toc2>
......
<sectnumlevels>${asciidoctor.sectnumlevels}</sectnumlevels>
</attributes>
</configuration>
<executions>
<execution>
<phase>none</phase>
<goals>
<goal>process-asciidoc</goal>
</goals>
</execution>
</executions>
</plugin>
Please see how i defined property in parent POM
<properties>
<asciidoctor.sectnumlevels>4</asciidoctor.sectnumlevels>
</properties>
Additionally i've tested below solution as well,but this way it's taking default value but when i pass the value from POM as a property plugin end, it's not overriding
<profiles>
<profile>
<id>asciidoctor.param.default</id>
<activation>
<activeByDefault>true</activeByDefault>
<property>
<name>!asciidoctor.sectnumlevels</name> <!--if not defined from parent POM-->
</property>
</activation>
<properties>
<asciidoctor.sectnumlevels>3</asciidoctor.sectnumlevels> <!--set default value-->
</properties>
</profile>
</profiles>
Not quite sure to understand your setup here. It seems to me that you are mentioning 3 pom files : parent, children and mycutom-plugin.
You can not pass default value from mycustom-plugin pom file to your project. You can define default values in your plugin code though.
To pass defaut value from your parent to your children, you don't need to define a profile, simply define the defaut value in parent pom, then override the same value in your children pom if needed.
Both declaration in parent and children are done the same way :
<properties>
<asciidoctor.sectnumlevels>4</asciidoctor.sectnumlevels>
</properties>

In maven, how to override plugin configuration in settings.xml

I want to override a particular plugin configuration that's defined in the pom.xml. I don't want to modify the pom.xml for various reasons. Is there a way to define a config attribute for that plugin in settings.xml that override corresponding pom.xml plugin config?
In the below example, you'll notice that the plugin xx-plugin is defined in profile1 in pom.xml. In my settings.xml I've already defined profile2 to override property prop1 from pom.xml. But how to override config3. I apologize if this is a silly question. I am a little new to maven.
This is what my pom.xml looks like:
<profile>
<id>profile1</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>com.xx.yyy</groupId>
<artifactId>xx-plugin</artifactId>
<executions>
<execution>
<id>xx-install</id>
<phase>install</phase>
<goals>
<goal>xx-install</goal>
</goals>
<configuration>
<config1>AAA</config1>
<config2>BBB</config2>
<config3>CCC</config3> <!-- I want to override this with value DDD -->
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
This is what my settings.xml looks like:
<profile>
<id>profile2</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<prop1>overriden-value</prop1> <!-- This works -->
</properties>
<!-- Somehow override config3 here -->
<!-- <config3>DDD</config3> -->
</profile>
AFAIK you can only override properties with settings.xml profiles. You'd have to change your plugin's configuration to use a property instead of a fixed value:
<!-- define your property -->
<properties>
<prop1>CCC</prop1>
</properties>
<profile>
<id>profile1</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>com.xx.yyy</groupId>
<artifactId>xx-plugin</artifactId>
<executions>
<execution>
<id>xx-install</id>
<phase>install</phase>
<goals>
<goal>xx-install</goal>
</goals>
<configuration>
<config1>AAA</config1>
<config2>BBB</config2>
<config3>${prop1}</config3> <!-- I want to override this with value DDD -->
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
Remember that profiles with activeByDefault set to true will get deactivated if any other profile gets activated in your build invocation. See http://maven.apache.org/guides/introduction/introduction-to-profiles.html
If you do not want to change the pom.xml for a plugin you can set the configuration as JVM parameter when running maven as stated in the Generic Configuration chapter of the Maven Guide to Configuring Plugins.
Example:
mvn my-plugin:my-goal -Dplugin.property=ABC
Example for the wildfly plugin (this is where I needed it and did not want to change the pom.xml of a demo project when deploying to a server group in a domain context):
mvn clean install wildfly:deploy -Dwildfly.serverGroups=<server-group-name>
The maven documentation also states that most plugins define help goals to explain users how to configure them.
Exaple for the wildfly plugin:
mvn wildfly:help -Dgoal=deploy -Ddetail

setting active profile defined in parent pom in project pom

We have a corporate wide Super Pom we use to define many of the defaults we use. For example, the Super Pom defines what version of the JDK to use, and other parameters. This is inherited by our projects as the parent pom.
Most of our projects use JDK 1.7, but one set of projects is still on version JDK 1.6. I've put the following profile definitions in my parent pom:
<properties>
<travelclick.snapshot.repo>artifactory/libs-snapshot-local</travelclick.snapshot.repo>
<old.javac.source>1.5</old.javac.source>
<old.javac.target>1.6</old.javac.target>
</properties>
<profiles>
...
<profile>
<id>jdk1.6</id>
<build>
<plugins>
<plugin>
<groupId>com.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<...>
<source>${old.javac.source}</source>
<target>${old.javac.target}</target>
<...>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<...>
</profiles>
Now, I have a profile called jdk1.6 and I'd like to specify in the project's pom that it should use this one by default. How do I do this?
I've tried adding into the project's pom:
<profiles>
<profile>
<id>jdk1.6</id>
<activations>
<activeByDefault>true</activeByDefault>
</activations>
</profile>
</profile>
But that redefines my jdk1.6 profile.
I've tried putting in this:
<activeProfiles>
<activeProfile>jdk1.6</activeProfile>
</activeProfiles>
But that only works in settings.xml.
How do I specify a profile in the parent pom, and then say that this is the active profile in the child pom?
More Attempts
I've tried using properties. In my parent pom.xml:
<profiles>
<profile>
<id>jdk1.6</id>
<activation>
<property>
<name>use-jdk1.6</name>
</property>
</activation>
<profile>
</profiles>
And the following in my local pom:
<properties>
<use-jdk1.6>true</use-jdk1.6>
</properties>
But, it doesn't pick up the profile. And, this does work:
$ mvn -Puse-jdk1.6 clean package site
So, I know that the parent profiles do work.
Profiles
Could you add yours profile details exectuing goal help:all-profiles
[INFO] Listing Profiles for Project: xxxx
Profile Id: artifactory (Active: true , Source: settings.xml)
Profile Id: jdk1.6 (Active: false , Source: pom)
Profile Id: arse-version (Active: false , Source: pom)
Profile Id: urge (Active: false , Source: pom)
I can activate jdk1.6 from the command line. I just want to activate it as the default in my child poms.
AAAAHGGGGHHHH!
That's me screaming.
I found the issue and why this wasn't working.
In my parent pom, I had the following:
<properties>
<javac.source>1.7</javac.source>
<javac.source>1.7</javac.source>
<old.javac.source>1.7</old.javac.source>
<old.javac.source>1.7</old.javac.source>
...
</properties>
<profiles>
<profile>
<id>jdk1.6</id>
<activation>
<property>
<name>use-jdk1.6</name>
</property>
</activation>
<build>
<plugins>
....
<plugin>
<groupId>maven-compiler-plugin</groupId>
...
<configuration>
<!-- This isn't doing what I think -->
<source>${old.javac.source}</source>
<target>${old.javac.target}</source>
...
</configuration>
</plugin>
</plugins>
<profile>
In my child pom, I had this:
<properties>
<use-jdk1.6>true</use-jdk1.6>
</properties>
And, it appeared that setting the use-jdk1.6 property just wasn't working. However, that wasn't the case. I was setting the profile.
What happens is if I have the system property javac.source and javac.target set, it overrides the configuration of the maven-compiler-pluing (even though I had explicitly set <source> and <target> not to use version 1.7).
So, I spent six hours on this issue before I realized it was due to me setting a property named javac.source rather than something like java-version.

Define maven profiles outside POM

Is there a way to define my maven profiles outside POM file but not in .m2/settings.xml?
I want to define them in a separate xml file inside the application (way to work efficiently with maven 2 and 3) because I am using maven 2 and intend to switch to 3 soon.
Until Maven 2.2.1 you could define your profiles into the profiles.xml file as a separate file but with Maven 3 this opportunity has been removed. The question ist why do you need a separate file for the profiles?
You may want to go through this maven documentation on build profiles, which describes the types of profiles and how each can be used.
As I see it, profiles cannot be defined outside pom.xml or settings.xml, if you want to use maven 3.
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<build.profile.id>dev</build.profile.id>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<build.profile.id>prod</build.profile.id>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<build.profile.id>test</build.profile.id>
</properties>
</profile>
</profiles>
And add a filter
<filters>
<filter>src/test/resources/${build.profile.id}/config.properties</filter>
</filters>
And add any directory (dev, prod, test)
I was recently migrating an application to maven3 from maven2. With maven 3 there is no possibility to have external profiles. But what can be done is to have external property files. This can be achieved by maven-properties-plugin
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<!-- Associate the read-project-properties goal with the initialize phase,
to read the properties file. -->
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>../com.tak/build.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
So here I have explained how to do that http://programtalk.com/java/migrate-from-maven2x-to-maven3x/

Resources