Spring Boot from Maven Profile in POM - java

I have a spring boot application that has dev and prod settings. Following multiple instructions found during searches I have an application.properties file which has:
#-----------this will load the prod or dev according to the #activatedProperties# value which is set from the pom when built
spring.profiles.active=#activatedProperties#
Then there are two files:
application-dev.properties
application.prod.properties
I then have a pom which has the spring-boot pom as a parent
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
I then set the profiles up as follows:
<profiles>
<profile>
<id>dev</id>
<properties>
<activatedProperties>dev</activatedProperties>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>prod</id>
<properties>
<activatedProperties>prod</activatedProperties>
</properties>
</profile>
</profiles>
If I then execute a build as follows:
mvn -DskipTests -Pprod clean compile package
After doing this the application.properties file shows:
#-----------this will load the prod or dev according to the environment
spring.profiles.active=dev
Note that it did not use prod as requested but instead dev. In fact it simply execute the activeByDefault profile not matter what I do. Has anybody else seen this and have any ideas as to what is happening. As you can imagine it is really annoying to have the deploy instructions say 'edit the POM file to move the activeByDefault property from the dev to prod profile'!

Solved!
Eclipse is getting in the way; as the build happens eclipse is reinforcing the default profile; this happens in :
Version: 2019-09 R (4.13.0)
Build id: 20190917-1200
If you close eclipse and run from the command line it works just fine.
If you use eclipse then unchecking the Refresh on access under Preferences -> General -> Workspace then it works. The clue was from another stack overflow question : Eclipse + maven : profile ignored
Thanks to all who took the time to reply.

Related

IntelliJ resolve dependencies of profile

I have a Java project with multiple modules. In this project, one module has a dependency to another one, which is only needed for a specific profile and hence is defined like that:
<profile>
<id>myProfile</id>
<dependencies>
<dependency>
<groupId>MyGroupId</groupId>
<!-- ... -->
</dependency>
</dependencies>
<!-- ... -->
</profile>
This works fine when building manually with maven like that:
mvn clean install -P myProfile
When using the IntelliJ build however, the dependency doesn't get resolved.
I've tried the option to delegate IDE build/run actions to maven, adding a property for maven in Build, Execution, Deployment > Build Tools > Maven > Runner (namely -P -> myProfile), and much more which is most likely not of interest.
Is it possible to configure IntelliJ to resolve the dependencies for a specific profile?
To help Intellij Idea to understand about your maven profile and maven object mode, you set as default profile in maven so that by default it will be recognized and run by any IDE. I provide below the code snippet.
<profile>
<id>firstProfile</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
..... Other code goes
</profile>
So inside profile, use this <activeByDefault>true</activeByDefault>. It may solve the problem.

Maven build for Spring boot Application for windows and linux platforms

I need to generate my spring boot application binaries for windows and linux platforms using Maven build.
I am planning to have two different application.properties for windows and linux i.e
application-windows.properties
application-linux.properties
and in the pom.xml I will have this
<profile>
<id>dev</id>
<properties>
<activatedProperties>windows</activatedProperties>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>release</id>
<properties>
<activatedProperties>linux</activatedProperties>
</properties>
</profile>
Maven build will be triggered using Jenkins.But in this way I can have only one profile active at a time,So It will require two jenkins project for windows and linux.
Is there any better way of doing this?
Profile should be chosen by giving build argument while starting the spring boot application.
-Dspring.profles.active=$profile
If you are using jenkins to start the application , just add an option to choose profile during job execution which can add parameter to application.
https://docs.spring.io/spring-boot/docs/1.1.6.RELEASE/reference/html/boot-features-profiles.html

IntelliJ: activate Maven profile when running Junit tests

I have declared some properties that are specific to Maven profiles. A part of my pom.xml:
<profiles>
<profile>
<id>release</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<my.properties.file>foo.xml</my.properties.file>
</properties>
</profile>
<profile>
<id>ci</id>
<properties>
<my.properties.file>bar.xml</my.properties.file>
</properties>
</profile>
</profiles>
I encounter some problem to use the "ci" Maven profile when I start Junit tests via IntelliJ IDEA 2016.
I activate my profile via the "Maven Projects" panel, then I start tests. The problem is the "my.properties.file" property value is equal to "foo.xml", not "bar.xml".
I have no problem with command-line (I can use the "-Pci" flag). How can I tell IntelliJ to use the "ci" profile? Thx.
You should add the profiles to the Maven setting.xml file (you should find it in the path ${YOUR_MAVEN_HOME}\apache-maven-3.1.1\conf\setting.xml).
Then, you have to open intellij, click on View > Tool Windows > Maven Projects. There, you should see your profiles (ci and release) and select the correct one.
Hope this can help you.
Just finally solved it.
<profile>
<id>profile-to-be-activated-on-build</id>
<activation>
<activeByDefault>false</activeByDefault><!-- on your flavor -->
<property>
<name>mvn-profile-env-var-trigger</name>
</property>
</activation>
</profile>
Goto JUnit default profile (aka configuration template). Add into JVM args:
-Dmvn-profile-env-var-trigger
You may need to manually reload maven profiles in IDE.
Also make sure on [Settings > Build Tools > Maven > Running tests] envVars is checked (or better everything).

Is it possible to have multiple POM and TESTNG files in a project?

I have the following setup:
Jenkins points to -> Pom.xml which points to -> Testng.xml
My goal is to have multiple jobs in Jenkins, each pointing to a different environment. Those environments are currently separated by multiple TestNG files. I have parameters setup in each testng.xml file which point the different environments.
Is there a way to have multiple POM files, each pointing to a different testng file?
Thanks
You should use a single pom file which defines a "profile" for each environment. The profile for an environment will customize all the configuration that is required for that environment (including pointing to the proper testng file).
In your pom, you will include
<profiles>
<profile>
<id>foo</id>
<properties>
...
</properties>
<build>
...
</build>
</profile>
<profile>
<id>bar</id>
<properties>
...
</properties>
<build>
...
</build>
</profile>
</profiles>
and invoke it with
mvn -Pfoo
or
mvn -Pbar
As you say, you will want to have a Jenkins job per environment (with the -Pfoo in the configuration for that job) or you can have a single Jenkins job and pass the profile as a parameter to the job.

How to configure compiler level (1.6) in maven settings.xml?

I am familiar with configuring maven-compiler-plugin to use java 1.6 in pom.xml and using parent pom file.
Is there a way to configure java compiler level to java 1.6 in the settings.xml level (so that all my maven projects will use java 1.6)?
In this thread default maven compiler setting somebody told there is a way to configure it in settings.xml.
Can someone please explain how to configure?
PS: Another way to specify Java compiler level:
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
-Siva
Simple answer No. In the settings file you don't configure things like this, cause it doesn't make sense and would make your builds not reproducible. The information about configuring the compiler plugin in settings.xml is rubbish.
source/target level can be configured in settings.xml, like this:
<profiles>
<profile>
<id>jdk-1.6</id>
<activation>
// can be replaced with other conditions
<jdk>1.6</jdk>
</activation>
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
<maven.compiler.compilerVersion>1.6</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>

Categories

Resources