How to share common code blocks between maven profiles? - maven

I have two maven profiles, the only difference is a line. Other than that the two profiles are identical. Is there a way to share the common code blocks in the POM without copy/paste? For example, declare a base profile and inherit it, and change the corresponding line in the children? I do not want to use any environmental variable approach.

<profiles>
<profile>
<id>parent-profile</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<!-- common config -->
</profile>
<profile>
<id>profile-a</id>
<!-- specific config for a -->
</profile>
<profile>
<id>profile-b</id>
<!-- specific config for b -->
</profile>
</profiles>
And just run:
mvn args -P profile-a or mvn args -P profile-b
And if you dont want the parent-profile to be active by default, just delete the specified line and every time you wish to activate one profile, add the parent profile to mvn like:
mvn args -P parent-profile, profile-a or mvn args -P parent-profile, profile-b

Related

maven command line for running multiple profile

i have 2 profiles in my pom.xml and need to package 2 WAR files simultaneously
this is pom
<profiles>
<profile>
<id>TEST</id>
-- scripts
</profile>
<profile>
<id>DEV</id>
-- scripts
</profile>
</profiles>
i tried
clean package -P Dev,Test
but alywas the generated WAR is the last one (Test) and Dev profile not run
You can use multiple P arguments:
mvn clean package -P Dev -P Test
If you want to do that, you need to separate Maven runs.
After a lot of investigation i figure out it can't be done

How do I use cucumber profile to dynamically use different environment hostname, testdata, testGroup. I am using cucumber, java, maven

I have test cases written in feature files. The test cases are tagged per requirements.
I want these test cases to be executed on different environments, using maven command, by passing profile name.
Each profile will have hostname, username, password for particular environment.
How do I achieve this?
#stage_ready
Feature: GUI Tests
Background:
Given the site <hostname> is reached using "Chrome" browser
When maximize window
And credentials <user_host> is typed into the "sd_username_editbox"
And credentials <password_host> is typed into the "sd_password_editbox"
And the "sd_login_button" button is clicked
CommandLine:
mvn test -Dcucumber.options=”–tags #stage_ready” -p profile_name
I want to know how to write profile, which will have parameters like hostname, user_host, password_host for different profiles and be dynamically used in the cucumber steps, that I have marked in angle brackets for reference.
From the Cucumber documentation:
"Profiles are not available in Java."
If you want to pass arguments to Maven, you can use Maven profiles
For example (from the Maven link above):
<project>
...
<profiles>
<profile>
<id>appserverConfig-dev</id>
<activation>
<property>
<name>env</name>
<value>dev</value>
</property>
</activation>
<properties>
<appserver.home>/path/to/dev/appserver</appserver.home>
</properties>
</profile>
<profile>
<id>appserverConfig-dev-2</id>
<activation>
<property>
<name>env</name>
<value>dev-2</value>
</property>
</activation>
<properties>
<appserver.home>/path/to/another/dev/appserver2</appserver.home>
</properties>
</profile>
</profiles>
..
</project>

Maven - Is it possible to build a project specifying multiple profiles simultaneously?

We are developing a Maven archetype for applications that only consume web services. This archetype offers three profiles, one for each environment (dev, pre, pro).
The point is that we would like to offer the possibility of having ORM dependencies included optionally (JPA, Hibernate) for those project that may require them in the future. We had though of creating an additional profile containing those dependencies.
When we build our project we use mvn package -Denvironment=dev. Is it possible to specify more than one profile such as: mvn package -Denvironment=dev,orm?
Yes, this is possible. But it seems you are confused about how profiles are activated in the first place.
The command
mvn package -Denvironment=dev
will not activate any profile without further configuration. In your case, it works because there must be a profile definition in your POM that is activated by the presence of the system property environment with a value of dev. The configuration you have would look like:
<profiles>
<profile>
<activation>
<property>
<name>environment</name>
<value>dev</value>
</property>
</activation>
</profile>
</profiles>
This is the magic that makes the profile activates when you pass the system property with -Denvironment. With that in mind, you can activate multiple profiles with the same idea: declare multiple <profile> element that are activated by the presence of a system property.
<profiles>
<profile>
<activation>
<property>
<name>myAwesomeProperty1</name>
<value>true</value>
</property>
</activation>
</profile>
<profile>
<activation>
<property>
<name>myAwesomeProperty2</name>
<value>true</value>
</property>
</activation>
</profile>
</profiles>
The above configuration would activate both profile if myAwesomeProperty1 and myAwesomeProperty2 is a system property with the value true.
In this particular case though, it seems that what you want is to activate a build depending on your environment so it could perhaps be a better idea to activate the profiles based on the -P command line switch, instead of a system property.
From Introduction to Build Profiles:
Profiles can be explicitly specified using the -P CLI option.
This option takes an argument that is a comma-delimited list of profile-ids to use. When this option is specified, the profile(s) specified in the option argument will be activated in addition to any profiles which are activated by their activation configuration or the <activeProfiles> section in settings.xml.
mvn groupId:artifactId:goal -P profile-1,profile-2
With this solution, you invoke Maven with multiple profile ids. That is to say, if you have in your configuration
<profiles>
<profile>
<id>profile-1</id>
<!-- rest of config -->
</profile>
<profile>
<id>profile-2</id>
<!-- rest of config -->
</profile>
</profiles>
The above invocation would activate both profile-1 and profile-2.

maven override profile from parent in child

I have a multi-module maven project.
parent-module-->child-submodule1
\->child-submodule2
Children are both submodules AND children of parent-module.
Child-submodule1 has a build profile my-profile very specific that should be shared with his sibling (submodule 2) as it setup the environment for both.
I moved this "my-profile" to the parent.
Now when I build activating this profile it is executed three times, thus the build fails (as this profile does very specific things here).
I needed the profile to be executed only once in the parent, and being skipped in the children.
I tried removing the parent-children relationship and this way it woud work, but I have other problems to solve (dependencies carried out from the parent).
How can I do it?
Can I override the profile in the children deactivating it?
You cannot deactivate a profile in children or prevent it from run.
You could however set <inherited>false</inherited> on all executions in my-profile in parent.
Alternatively, you can active the profile using one of these methods:
Having an activation policy of a specific property value only set on the two sub modules
<profiles>
<profile>
<activation>
<property>
<name>doit</name>
<value>true</value>
</property>
</activation>
...
</profile>
</profiles>
Using the presence of a specific file or folder, which is probably in your scenario anyway.
<profiles>
<profile>
<activation>
<file>
<exists>src/main/xcopy</exists>
</file>
</activation>
...
</profile>
</profiles>

How can I exclude a project from a mvn clean install? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I exclude certain modules from a maven build using the commandline
I am running a maven clean install in a pom file which includes several modules (and sub-modules). I was wondering if it is possible to run a maven build but specifying on command line to skip a module from the build ( at the moment I exclude them manually from the build, but Id prefer to do it via command line).
I know that with -pl you can selectively choose projects, but what I would like is to selectively exclude (in a blacklist fashion) some.
You could have a separate <modules> section in a profile, and activate the profile you need in the command line.
Example:
<profiles>
<profile>
<id>profile-1</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<modules>...</modules> <!-- module set 1 -->
</profile>
<profile>
<id>profile-2</id>
<modules>...</modules> <!-- module set 2 -->
</profile>
</profiles>
Now, dependent on your current need, execute
mvn install
mvn install -P profile-2
Note that you'd have to think it over carefully, there must be no cross-profile dependencies on the excluded module.

Resources