Having trouble getting springboot app to run/build specific profile - spring-boot

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"

Related

pom.xml selecting profile from multiple profiles

I have a application with multiple build profiles all stated in my pom.xml, I have something like this below.
<profiles>
<profile>
<id>sit</id>
</profile>
<profile>
<id>uat</id>
</profile>
</profiles>
how do I now specify, during running, i want to use sit or uat profile? when it compiles and run, how does spring boot decide which profile to pick?
You can provide the active profile name in application properties or at the command line while staring the application with --spring.profiles.active=sit
Please check this for more information https://docs.spring.io/spring-boot/docs/1.2.0.M1/reference/html/boot-features-profiles.html

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>

Goal specific javax.net.ssl.trustStore

I've got a Maven project for which i use org.apache.tomcat.maven:tomcat6-maven-plugin to deploy to a remote Tomcat.
This tomcat is configured so that i need to specify:
-Djavax.net.ssl.trustStore=mykeystore.jks -Djavax.net.ssl.trustStorePassword=mypassword
My problem is that by doing so, I can't download dependencies anymore from remote repositories through my company proxy as it tries to establish a secure connection using this truststore and it fails...
I'm looking for a way to connect to both ends (maven repo and my remote tomcat) without having to set/unset my MAVEN_OPTS variable every time...
I've seen that I can have a <configuration /> element in my settings.xml, but I can't find what to put in it.
Thanks...
Using different profiles with maven:
Define the active profiles in your pom (you can also define profiles in settings.xml but I think this should work for your case):
<profiles>
<profile>
<id>TOMCAT_DEPLOY</id>
<activation>
// Rules to active the profile
<activeByDefault>true</activeByDefault>
</activation>
<properties>
</properties>
// Add rest of profile specific configuration
</profile>
</profiles>
For executing maven with an specific profile, basically you have a list of active profiles and you can execute one of them according to different triggers:
A profile can be triggered/activated in several ways:
Explicitly, Through Maven settings, Based on environment variables OS settings or based on some Present or missing files
Please, read this link where you can have all information about profiles and how activate them for any execution

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.

Resources