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

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>

Related

Having trouble getting springboot app to run/build specific profile

I am using Maven as my build tool.
For profile management in SpringBoot I am using yml files.
For my SpringBoot app, I have the following application-*.yml files set up:
application.yml
application-local.yml
application-foobar.yml
My corresponding pom.xml profile configuration:
<profiles>
<profile>
<id>local</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<activatedProperties>local</activatedProperties>
</properties>
</profile>
<profile>
<id>foobar</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<activatedProperties>foobar</activatedProperties>
</properties>
</profile>
</profiles>
Whenever I attempt to either package or run my app via Maven:
> mvn package -P <any configured profile>
> mvn spring-boot:run -P <any configured profile>
The app runs, however it only runs falling back to the default profile (application.yml).
I get the following log entry every time I attempt to run the application under any of my configured profiles:
: No active profile set, falling back to default profiles: default
I can't seem to find any clear information on the internet regarding this issue.
Any help would be greatly appreciated. Thanks!
Maven build profiles and Spring bean profiles are two completely separate concepts that happen to use the same name but do not interact with each other.
The XML you showed with <profiles></profiles> configures Maven build profiles, but will not have any effect on Spring bean profiles.
Per the Spring Framework documentation on activating a profile and Spring Boot documentation on passing arguments when running an application, you can select the active Spring bean profile when running the app using Maven with a command like this:
> mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Dspring.profiles.active=local"

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 can we add a temporary dependency when running "mvn install" from the command line?

I cannot run my application on my machine unless I add a dependency. Since this is only a hotfix, I do not want to add the dependency on the git repo.
I would like a way to add a temporary dependency, without having to edit the pom.xml everytime I git pull/push.
You can achieve this by using maven profiles.
Just put the dependency that is required only for hotfix on the profile named hotfix and put other dependencies without any profile.
<profiles>
<profile>
<id>hotfix</id>
…
<dependencies>
<dependency>…</dependency>
</dependencies>
…
</profile>
</profiles>
<dependencies>
<dependency>...</dependency>
</dependencies>
To activate the profile supply profile name with -P option on any maven command.
For example, to activate hotfix profile while doing clean and install use command
mvn clean install -Photfix
Also, there are other ways to activate maven profiles.
Please see link for more information: https://maven.apache.org/guides/introduction/introduction-to-profiles.html

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

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

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

Resources