Spring web application with profile - spring

I have a spring application with multiple profile like dev,prod. application running on external tomcat.profile are setting in the pom.xml file
pom.xml
<profiles>
<profile>
<id>dev</id>
<properties>
<repoManager>Manager1</repoManager>
..........
</properties>
</profile>
</profiles>
Project have applicationContext.xml file.
One of the bean definition, we are taking repoManager the value from dev profile.
when i run the application, throwing the following error
rg.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name '<Class name>' defined in class path resource [applicationContext.xml]: Circular placeholder reference 'repoManager' in property definitions
How to run spring web application with profile dev
I have done following changes
Added spring.profiles.active=dev in catalina.properties file
Added setenv.sh in tomcat bin folder
Added spring.profiles.active=dev on STS run configuration
Above 3 solution are not working.
How to solve this problem.
Thanks in advance.

I Got the answer
Right click on the project -> Maven -> Select Maven Profiles... and pickup your profile.
No Need to do above steps.

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"

Could i use maven profile to swith between different value application.properties

I have application-prod.yml application-dev.yml, and application.properties which containing just one line code like below
spring.profiles.active=dev
for maven production build, it should use spring.profiles.active=prod , then it will build with application-prod.yml, for development build, it should use spring.profiles.active=dev, then maven
will use application-dev.yml to build
could I use pom.xml's different profile to do switch for this value switch in applicaiton.properties?
You can use a Maven property for this, reference it in your yml file (with ${...}) and filter the resource (i.e. the yml file) with the maven resources plugin.
It seems that what you're after is "externalized configuration". According to the excellent 12factor guidelines, it is best not to keep such config inside your code-repository.
Refer to the relevant section in the Spring Boot manual to see which options you have (and there are many). What it comes down to is that you provide your application.yml/properties file on the filesystem and your application will read it from there, rather than from the classpath.
Also, note that spring-profiles are not meant to be used to distinguish between development environments, but rather to put the application in different functional modes (e.g. to enable or disable specific features).
If you want the content of your properties file changed at build time, then you can use Maven filtering. Maven filtering allows to replace a placeholder in your properties (or yaml) file by values from Maven properties.
Assuming you have a property in your POM called targetEnv, which might have either the value dev or prod (depending on the active Maven profile), then you can refer it in your properties file (or yaml file) by using the following syntax :
spring.profiles.active=#targetEnv#
However, if you want to follow Spring Boot recommandations, it is better to enable and disable the Spring profiles by the means of environment variables in your target environment. For instance, you can use an environment variable spring.profiles.active with the desired value and it will override the value in your properties file.
You need to define a custom property in each of your Maven profiles and set their values to match with suffixes of corresponding properties files that you want to load with a particular profile.
<profile>
<id>dev</id>
<properties>
<activatedProperties>dev</activatedProperties>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>release</id>
<properties>
<activatedProperties>release</activatedProperties>
</properties>
</profile>
Next, in the build section of the same file, configure filtering for the Resources Plugin. That will allow you to insert properties defined in the previous step into any file in the resources directory, which is the subsequent step.
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
…
</build>
Finally, add the following line to the application.properties.
spring.profiles.active=#activatedProperties#
For more details, please see spring boot properties per maven profile
For official guide to load from external configLoad from external Config

How does maven replace text #env# in application.yml?

There is some profiles defined in pom.xml.
<profile>
<id>test</id>
<properties>
<env>test</env>
</properties>
</profile>
spring.profiles.active: #env# defined in application.yml and bootstrap.yml.
When I run mvn install -P test, text #env# in application.yml would be replaced by test.
How does it work?
Why it does't work for bootstrap.xml?
It works for application.yml because you are obviously using the Spring Boot Starter Parent. See the POM here: https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-starters/spring-boot-starter-parent/pom.xml
The magic part is the <resources> configuration within that parent POM. You see that the application config files are explicitly copied with filtering. That is why the maven-resources-plugin resolves placeholders in these files.
If you want to add more files to be handled like this you can add your own <resources> section to your POM and extend it by more file patterns.

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

Netbeans build failure

Brand new to netbeans and working on the address-book tutorial. I am able to run the program without issues but when I build I get the following error. I have yet to find any possible solutions to the issue
Failed to execute goal org.codehaus.cargo:cargo-maven2-plugin:1.4.4:redeploy (deploy) on project address-book: Execution deploy of goal org.codehaus.cargo:cargo-maven2-plugin:1.4.4:redeploy failed: Failed to create deployer with implementation class org.codehaus.cargo.container.glassfish.GlassFish4xInstalledLocalDeployer for the parameters (container [id = [glassfish4x]], deployer type [installed]). InvocationTargetException: The container configuration directory "c://glassfish4/glassfish/domains" does not exist. Please configure the container before attempting to perform any local deployment. Read more on: http://cargo.codehaus.org/Local+Configuration -> [Help 1]
How does Maven know which server to deploy to and where is it located?
It's all in the pom.xml file.
Sometimes you will have to trace up the parents of the pom.xml to find it.
In the case of Java EE tutorial, when I was building project hello1, Maven couldn't find the location of my GlassFish server.
I traced up the parents of pom.xml to find
C:\mine\tools_installation\glassfish4\docs\javaee-tutorial\examples\pom.xml(51):
<glassfish.home.prefix>c:/</glassfish.home.prefix>
Then I changed it to
<glassfish.home.prefix>c:/mine/tools_installation</glassfish.home.prefix>.
The container configuration directory "c://glassfish4/glassfish/domains" does not exist.
Maven / Cargo looks for a folder defined in pom.xml and it doesn't exist on your computer.
I the property you need to change is and you'll need to set it to your GlassFish installation directory.
So it is automatically taken from the parent examples folder pom.xml rather than from your open example. So you should edit the pom.xml from
YOUR_GLASSFISH_HOME\docs\javaee-tutorial\examples
It think it is a bug in the examples pom.xml
I encountered similar error, the solution is very simple You need to edit the pom.xml file as follows. Replace line 38:
${glassfish.home.prefix}/glassfish4
to your GlassFish home, I run GlassFish 4.1.1 installed in C:\ drive. So my modification is:
${glassfish.home.prefix}glassfish-4.1.1.
Please note the slash before glassfish was removed too. The slash after c: in line 51 suffice.
Build it again. Should be fine.
I resolved this error adding in pom.xml those lines:
<profiles>
<profile>
<id>windows</id>
<activation>
<os>
<family>windows</family>
</os>
</activation>
<properties>
<glassfish.home>C://Program Files//glassfish-4.1.1</glassfish.home>
</properties>
</profile>
</profiles>
I hope this help
I also usually install my glassfish development server under c:/opt so I had the same problem with the hello2 tutorial from the Java Platform, Enterprise Edition (Java EE) 8 Tutorial.
To solve this problem, I changed the "tutorial-examples/pom.xml" file which is the root parent of all the tutorial POM files. As I am using the J2EE8 tutorial, it is expected that glassfish5 is installed so this is reflected in the properties section:
<glassfish.home>${glassfish.home.prefix}/glassfish5</glassfish.home>
I not sure what tutorial version you are going through but as I see, it expects a glassfish 4 installation thus you need to edit the POM file accordingly.
Also (in the J2EE 8 tutorial), ${glassfish.home.prefix} is defined for every supported profile. For windows the windows profile, I had to change the line:
<glassfish.home.prefix>c:/</glassfish.home.prefix>
to
<glassfish.home.prefix>c:/opt</glassfish.home.prefix>
So the complete windows profile will be:
<profile>
<id>windows</id>
<activation>
<os>
<family>windows</family>
</os>
</activation>
<properties>
<glassfish.home.prefix>c:/opt</glassfish.home.prefix>
<glassfish.executables.suffix>.bat</glassfish.executables.suffix>
</properties>
</profile>
Of course, you'll have to edit the POM file accordingly to your own setup.

Resources