How to activate a profile in a maven submodule using a property? - maven

I am using Maven 3.6.0 and OpenJDK8 on Ubuntu 18.04 (also tested with Alpine Linux).
I have a pom.xml in the root of my project that includes my submodules :
...
<modules>
<module>mysubmodule</module>
</modules>
...
In the mysubmodule folder, the pom.xml has a profile that I want to activate based on a property passed to the mvn executable:
...
<profile>
<id>my-profile</id>
<activation>
<property>
<name>activateMyProfile</name>
</property>
</activation>
...
</profile>
...
I then execute mvn to start the build, but the profile is never activated:
If I run mvn -DactivateMyProfile release:prepare from the root of my project, the profile is never activated and never runs
If I run mvn release:prepare from the root of my project, the profile is never run.
I also tried the inverse:
...
<profile>
<id>my-profile</id>
<activation>
<property>
<name>!doNotActivateMyProfile</name>
</property>
</activation>
...
</profile>
...
If I run mvn -DdoNotActivateMyProfile release:prepare from the root of my project, the profile is still executed
If I run mvn release:prepare from the root of my project, the profile is also executed
It looks like mvn is not able to see the properties being passed through the command line. What is the correct way to activate a profile in a submodule using a property?

As I am using the maven release plugin, parameters must be passed using the -Darguments argument.
For example, instead of using mvn -DactivateMyProfile release:prepare, the correct invocation is: mvn -Darguments=-DactivateMyProfile release:prepare
If there are multiple arguments, use mvn -Darguments="-DactivateMyProfile -DsomeOtherArg -DanotherArg=something" release:prepare

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

Upload artifacts to multiple nexus instances without running build twice using maven

We have some legacy reasons to upload our software artifacts to two instances of nexus (one internal and the other with the cloud solution we are hosted with)
Currently, we accomplish the same running the build twice with different settings files
mvn clean deploy -s=internal_nexus_settings.xml
mvn clean deploy -DskipTests=true -s=external_nexus_settings.xml
Is there a possibility of uploading artifacts to both without running the build twice
Currently, settings file contain
external_nexus_settings.xml
<id>external_cloudprovider</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<nexus.release.url>https://cloudvendor/nexus/content/repositories/releases/</nexus.release.url>
<nexus.snapshot.url>https://cloudvendor/nexus/content/repositories/snapshots/</nexus.snapshot.url>
<nexus.site.url>dav:https://cloudvendor/nexus/content/repositories/sites/${project.groupId}/${project.artifactId}/${project.version}/</nexus.site.url>
<nexus.username>admin</nexus.username>
<nexus.password>mycreds</nexus.password>
</properties>
internal_nexus_settings.xml
<profile>
<id>internal_nexus</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<nexus.release.url>https://internal/nexus/content/repositories/releases/</nexus.release.url>
<nexus.snapshot.url>https://internal/nexus/content/repositories/snapshots/</nexus.snapshot.url>
<nexus.site.url>dav:https://internal/nexus/content/repositories/sites/${project.groupId}/${project.artifactId}/${project.version}/</nexus.site.url>
<nexus.username>admin</nexus.username>
<nexus.password>mycreds</nexus.password>
</properties>
Tried adding multiple profiles (activating the profile via default and command line params) but maven picks the settings of the latest profile only
mvn clean deploy -DskipTests=true -s=external_nexus_settings.xml -P internal,external

What is the difference between -Pprod command and spring.active.profile=prod?

When i execute this using command line,
mvnw clean install spring-boot:run -Dspring.profiles.active=prod -Dmaven.test.skip=true -Djava.util.Arrays.useLegacyMergeSort=true
Project run properly
But when i try same thing using -Pprod command it gives an error
(Run properly but main page can't be displayed),
mvnw clean install spring-boot:run -Pprod -Dmaven.test.skip=true -Djava.util.Arrays.useLegacyMergeSort=true
Currently i have three profile dev,test and prod.
I want to know, what is the difference between both of them?
Please give me some idea regarding -Pprod command.
-Pprod is a property for the Maven call and activates the Maven profile with the name prod, spring.profiles.active=prod activates the Spring profile. They are not the same, so it depends where you have configured the 3 profiles (i guess you have configured them as Spring profiles, so the Spring property is the one that you have to use). It is possible though to configure Maven profiles in a way, so that also a Spring profile is activated in that run (basically in the Maven profile you would set the Spring profiles property to the value). You can find more about this here. Short example:
<profiles>
<profile>
<id>prod</id>
<properties>
<spring.profiles.active>prod</spring.profiles.active>
</properties>
</profile>
</profiles>

Questions about pom.xml in Jenkins to run sonarQube through maven project

I'm trying to run sonarQube through Jenkins but I have some difficulties right now. When I build a new job, I use Maven Project and inside the configuration I have to give à pom.xml path but what does it correspond to ?
Thank you in advance
You should find in any jenkins job a post action for sonarqube analyse.
The pom.xml you mention is the pom.xml for your maven project, because sometimes you can put your parent pom.xml in a subdirectory and this is the way for helping jenkins to find it.
Instead of adding Sonar Task to each project why not just configure Sonar at Global Level configuring the settings.xml for your maven configuration, just go to $HOME/someUser/.m2/settings.xml (if you don't have it created yet) with this content:
<settings>
<pluginGroups>
<pluginGroup>org.sonarsource.scanner.maven</pluginGroup>
</pluginGroups>
<profiles>
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<!-- Optional URL to server. Default value is http://localhost:9000 -->
<sonar.host.url>
http://myserver:9000
</sonar.host.url>
</properties>
</profile>
</profiles>
</settings>
After you you have done that you will be able to run sonar in all the projects this way:
mvn clean verify sonar:sonar
 
# In some situation you may want to run sonar:sonar goal as a dedicated step. Be sure to use install as first step for multi-module projects
mvn clean install
mvn sonar:sonar
 
# Specify the version of sonar-maven-plugin instead of using the latest. See also 'How to Fix Version of Maven Plugin' below.
mvn org.sonarsource.scanner.maven:sonar-maven-plugin:3.2:sonar
You may find more information in sonar official documentation:
https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Maven

Maven Build specific childs from Parent

Can we build specific child from a parent Pom in Maven.May be using a file that mentions the name of the modules to be included.
If you in root location of your project you can simply define that on command line of Maven like this:
mvn -pl ModuleYouWouldLikeToBuild package
Excerpt from the command line help:
-pl,--projects <arg> Comma-delimited list of specified
reactor projects to build instead
of all projects. A project can be
specified by [groupId]:artifactId
or by its relative path.
If this module what you like to build is used by an other module you can decide to let maven analyze which one and build that depending module also by using:
mvn -pl Module --also-make-dependents
or short version:
mvn -pl Module -amd
If you have modules which are used by the module you triggered to build you can also add:
mvn -pl Module --also-make
or short version:
mvn -pl Module -am
Assume your root project has 3 modules: module-A, module-B and module-C.
Run the commands below from your root project.
If you want to build module-C, run: mvn clean install –pl module-C
In case module-C depends on module-A, run:
mvn clean install –pl module-C –am to build module-A and module-C
You can use maven profile to achieve this.
<project>
...
<profiles>
<profile>
<id>build1</id>
<activation>
<property>build1</property>
</activation>
<modules>
<module>module1</module>
</modules>
</profile>
<profile>
<id>build2</id>
<activation>
<file>
<exists>test2.file</exists>
</file>
</activation>
<modules>
<module>module2</module>
<module>module3</module>
</modules>
</profile>
</profiles>
</project>
You can activate profile by some conditions, eg. present or missing files, existing properties and so on.
More info about maven profile:
http://maven.apache.org/guides/introduction/introduction-to-profiles.html

Resources