Change mongo dbname through build script - spring

What do I want:
I want to set up an automated deployment pipeline - my plan is as follows:
I create two .war files, let's call them prod.war and test.war, using a gradle build script, and deploy them to my tomcat server.
My problem:
The prod.war needs to access the production database (mongoprod), and the test.war to the test database (mongotest).
My attempt:
I thought I could use Spring profiles and simply...
...change a line in my application-context.xml from:
<mongo:db-factory id="mongoDbFactory" dbname="mongoprod"/>
to:
<mongo:db-factory id="mongoDbFactory" dbname="${mongo.db.name}"/>
...create two files, in the same folder (<filename>:<content>):
application-production.properties:mongo.db.name=mongotest
application-test.properties:mongo.db.name=mongoprod
...execute some gradle build script with an argument that can change the profile to production or test and then use the corresponding .property file to insert the desired mongo dbname...but that's where I ran out of luck!
I tried to add this to my build.gradle file:
bootRun { args = ["--spring.profiles.active=" + profiles] }
and then run it with $ ./gradlew bootRun -Ptest
but I just get the error:
Main class name has not been configured and it could not be resolved
and besides that I haven't used bootRun before, only a war task so far to create my .war files:
war {
archiveName = 'application.war'
dependsOn 'lessc', 'webpack'
from "$buildDir/webapp"
exclude 'WEB-INF/js/main.js'
rename 'main\\.min\\.js', 'main.js'
}
Does anyone know how I can get my problem to work or could give me useful information on this?

You need to provide a main class name to be run by the bootRun task, for example as follows:
springBoot {
mainClassName = 'org.baeldung.DemoApplication'
}
You can read more about it in thos tutorial
Update:
configuration for application plugin could be done as follows
application {
mainClassName = 'org.gradle.sample.Main'
}
As it's said in the plugin docs

I found out that for my case it turned out to be much easier (and I think better) to decide during runtime wether a test or a production DB should be used and I solved it the following way:
In my application-context.xml I created two beans instead of one, one for each DB (I had to nest them below a "root"-beans, otherwise I got errors during building):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.5.xsd">
<beans profile="prod">
<mongo:db-factory id="mongoDbFactory" dbname="mongoprod"/>
<mongo:mapping-converter id="mongoConverter" base-package="my.company.project">
<mongo:custom-converters base-package="my.company.project"/>
</mongo:mapping-converter>
<mongo:gridFsTemplate bucket="images" converter-ref="mongoConverter" db-factory-ref="mongoDbFactory"/>
</beans>
<beans profile="test">
<mongo:db-factory id="mongoDbFactory" dbname="mongotest"/>
<mongo:mapping-converter id="mongoConverter" base-package="my.company.project">
<mongo:custom-converters base-package="my.company.project"/>
</mongo:mapping-converter>
<mongo:gridFsTemplate bucket="images" converter-ref="mongoConverter" db-factory-ref="mongoDbFactory"/>
</beans>
</beans>
In order for the .war file creation to work properly I had to set enabled to true in my build.gradle file here:
war {
enabled = true //without this no .war file was created before - https://stackoverflow.com/a/52315049/4120196
dependsOn 'lessc', 'webpack'
from "$buildDir/webapp"
exclude 'WEB-INF/js/main.js'
rename 'main\\.min\\.js', 'main.js'
}
I then created a .war file, copied it and renamed it - so I had two: prod.war and test.war and put them into my Tomcat 8.5_Tomcat8.5.23\webapps folder (I'm working on Win10 in case this differs on other systems). Then I created two .xml files with the same name as the .war files where I defined the spring profile that decides which bean to take in Tomcat 8.5_Tomcat8.5.23\conf\Catalina\localhost:
prod.xml
<Context>
<Environment name="spring.profiles.active" value="prod" type="java.lang.String" override="false" />
</Context>
test.xml
<Context>
<Environment name="spring.profiles.active" value="test" type="java.lang.String" override="false" />
</Context>
I started up tomcat and finally everything worked as I wanted it to.
If you have any hints how my solution can be improved feel free to comment :)
I feel like my application-context.xml could be shortened somehow for example...

Related

How to configure Spring facet in IntelliJ IDEA

I'm new to Intellij 14.0 . I was using Netbeans, but my colleagues told me to shift to intellij and so I did.
I need to run the same project that I ran on netbeans in Intellij. The project that I'm working on is made of grails and spring. When I tried to run the project using Intellij this is what I get.
Spring Configuration Check
Unmapped Spring configuration files found.
Please configure/setup Spring facet for modules:
......... (1 file)
And this is the file.
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="grailsApplication" class="org.codehaus.groovy.grails.commons.GrailsApplicationFactoryBean">
<description>Grails application factory bean</description>
<property name="grailsDescriptor" value="/WEB-INF/grails.xml" />
<property name="grailsResourceLoader" ref="grailsResourceLoader" />
</bean>
<bean id="pluginManager" class="org.codehaus.groovy.grails.plugins.GrailsPluginManagerFactoryBean">
<description>A bean that manages Grails plugins</description>
<property name="grailsDescriptor" value="/WEB-INF/grails.xml" />
<property name="application" ref="grailsApplication" />
</bean>
<bean id="grailsConfigurator" class="org.codehaus.groovy.grails.commons.spring.GrailsRuntimeConfigurator">
<constructor-arg>
<ref bean="grailsApplication" />
</constructor-arg>
<property name="pluginManager" ref="pluginManager" />
</bean>
<bean id="grailsResourceLoader" class="org.codehaus.groovy.grails.commons.GrailsResourceLoaderFactoryBean" />
<bean id="characterEncodingFilter" class="org.springframework.web.filter.CharacterEncodingFilter">
<property name="encoding">
<value>utf-8</value>
</property>
</bean>
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean" />
</beans>
Honestly, I dont have any idea how to fix it. Based on my understanding it is looking for some modules but I could not fix it. I even tried this "Add framework support" . But I couldn't find spring in the list.
Could you help me fix this? Thanks
Go to File/Project Structure/Modules, click the green plus icon, select Spring from the dropdown and select your module in the next dialog.
Then click the green plus in the right pane, click plus and select your Spring configuration files and classes and click OK.
Also take a look at IntelliJ Help for Spring Facet.
Just ran into this issue out of the blue today—my build was working last night, and this morning it stopped working—so I figured I'd post my solution using IntelliJIDE CE 2019.1.
Error Messages (to help people find this answer)
package org.springframework.transaction.annotation does not exist
package org.springframework.boot does not exist
Unknown facet type: 'Spring'
Unknown facet type: 'web'
Solution
Step 0:
Update IntelliJ
Menu: IntelliJIDE > Check for Updates (Mac OSX)
Step 1:
Clean out Maven repo and re-download dependencies to ensure that the sources are not corrupt. This will take 5+min.
$ cd [project_directory]
$ mvn dependency:purge-local-repository
Step 2:
Preform a clean install.
$ mvn clean install
Step 3:
Update IntelliJIDE's Repository Indexes:
Open IntelliJ Settings/Preferences
Build, Execution, Deployment > Build Tools > Maven > Repositories
Select repositories one by one (by clicking on the table row) and click the update button. Specifically, do it for https://repo.mave.apache.org/maven2. The download is ±700mb so it will take a while. (10+min)
Related Questions
getting package org.springframework.transaction.annotation does not exist error while packaging app
package org.springframework.boot does not exist
IntelliJ Ultimate 2018 - the manual way:
Menu File -> Project Structure
Right-click on your module and choose "Add..." -> Spring
Optionally click "fix" if spring is not listed as a dependency
Click the + icon at the top to add your spring configuration .xml file(s)
The automatic way:
Click the body of the popup notification
Click the "Create Default" option

Glassfish and Spring Boot: .war re-deployment failed

I am trying to deploy an application build from https://github.com/spring-guides/gs-convert-jar-to-war to local glassfish4. And it delpoys well for the first time (if I can call some exceptions from the framework good behaviour). At least, I can open the project's index page in browser.
But if I try to undeploy it and re-deploy, or just overwrite .war file in autodeploy directory, deployment fails. And nothing seems to be added to glassfish log file.
The projects I published from eclipse using some its mechanisms deployed and re-deployed successfully.
Here is the log with related contents:
http://pastebin.com/zSeMw5tC
What can be the problem?
I did some experiments with Glassfish a while ago. The CDI implementation is really broken IMO (it shouldn't load classes to scan them for annotations), but the apps I tried worked on GF 4 if there is a WEB-INF/classes/META-INF/beans.xml containing
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee http://docs.jboss.org/cdi/beans_1_0.xsd"
bean-discovery-mode="none">
<scan>
<exclude name="org.springframework.**" />
<exclude name="org.apache.**" />
<exclude name="com.google.**" />
</scan>
</beans>
Nothing I did ever worked in GF 3 (older CDI spec and no way to exclude things I think).

How to locate the properties file in the spring context configuration file

I am using spring web mvc project, and I put all the spring related files under WEB-INF\spring, including a ormlite.xml and a jdbc.properties.
Now I want to locate the jdbc.properties file in the ormlite.xml,Like this:
<context:property-placeholder location="/WEB-INF/spring/jdbc.properties"/>
But when I run the application ,it will told me that :
Could not load properties
It can not find the properties file.
What is the problem?
From Spring forum:
The problem is that /WEB-INF isn't accessible as it isn't in the root
of the path, you must use the same path as you use in your test case
(include the src/main/webapp part but that will break your application
from running).
I suggest you move the jdbc.properties to the src/main/resources
directory and simply use classpath: prefix to load the properties.
Code:
<context:property-placeholder location="classpath:jdbc.properties"/>
The code above assumes they are on the root of the classpath (which is
where they are when they are in src/main/resources).
I hope this can help someone else.
I had the same problem - property files outside the classpath.
My solution:
First define a properties bean:
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location">
<value>/WEB-INF/your.properties</value>
</property>
</bean>
Then reference it in the property-placeholder:
<context:property-placeholder properties-ref="configProperties" />
Works perfectly well for me!
Instead of:
<context:property-placeholder location="/WEB-INF/spring/jdbc.properties"/>
Use:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/spring/jdbc.properties"/>
And your properties will be available in Spring file, and don't forget to add a namespace: xmlns:p="http://www.springframework.org/schema/p"
I think you're missing the prefix to instruct Spring how to attempt to load the properties. I think your definition needs to be:
<context:property-placeholder location="file:/WEB-INF/spring/jdbc.properties"/>
Note the addition of the file: prefix.

Overriding System property in Spring PropertyPlaceHolderConfigurer for integration testing

I'm using a System property to define the location for an environment-specific properties file. However, I would like to override that value to something different for integration tests.
Here's my production spring setup. I'm using a custom PropertyPlaceholderConfigurer to resolve some encrypted property file values, but that's not important here:
<-- Spring configuration in file service-spring-beans.xml -->
<bean class="com.mycompany.MyPropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:properties/${MY_ENVIRONMENT}/${MY_ENVIRONMENT}.properties</value>
</list>
</property>
<property name="ignoreResourceNotFound" value="false"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
At runtime, we define the value of MY_ENVIRONMENT as a Java system property. This all works as expected. However, for integration tests, I would like to define MY_ENVIRONMENT as "inttest", so the integration-test specific property file properties/inttest/inttest.properties is loaded.
I've tried to use a spring context loaded by the integration-test to set up a String bean with the id MY_ENVIRONMENT:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:component-scan base-package="com.mycompany.myclasses"/>
<bean class="java.lang.String" id="MY_ENVIRONMENT">
<constructor-arg value="inttest"/>
</bean>
<!-- this imports the production spring context -->
<import resource="classpath:service-spring-beans.xml"/>
</beans>
However, the value of MY_ENVIRONMENT is not resolved , and I get this error when running the integration tests.
Caused by:
org.springframework.beans.factory.BeanInitializationException: Could
not load properties; nested exception is
java.io.FileNotFoundException: class path resource
[properties/${MY_ENVIRONMENT}/${MY_ENVIRONMENT}.properties] cannot
be opened because it does not exist
How can I override MY_ENVIRONMENT at inttest time without passing a System property to the JVM?
Since I was using the maven surefire plugin to run integration tests, the simplest solution turned out to be setting the system property using the surefire plugin configuration, like this:
<configuration>
<systemPropertyVariables>
<MY_ENVIRONMENT>inttest</MY_ENVIRONMENT>
</systemPropertyVariables>
<!-- ... -->
<configuration>
Since you don't want to use profiles, you can create multiple contexts and just include the proper context files for what you want. So for integration testing you have have your application-context.xml and an integration-property-context.xml file while, in the prod environment you would include the application-context.xml with a production-property-context.xml file. I've seen this approach used heavily to switch datasources between dev and prod where dev would be a BasicDataSource implementation and the prod environment references a JDNI DataSource.
This approach will help you avoid ActiveProfiles, but you run in to the problem of managing duplicate beans possibly which ActiveProfiles really simplified down.
you could look at overriding your whole property placeholder implementation using active profiles. that is, the default (no profile) launches your property placeholder, but a test profile (e.g. 'test') could create a new test bean for the property placeholder.
one of the challenges with property placeholder is that its loaded at the very early stages of the application context startup, so a normal override bean may not work.

Weird behaviour of import tag in spring's configuration file

I am working on the spring spring-3.2.2. I have created two java projects in eclipse.
SpringTest
Testclasspath
SpringTest project is having the below beans.xml in which the one bean is defined.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="helloWorld" class="com.spring.HelloWorld" init-method="testUpdate" scope="prototype">
<property name="message" value="Hello World!"/>
</bean>
</beans>
I have created the jar springtest.jar of the project SpringTest and it is been added in the classpath of the project Testclasspath. Bean configuration file for the Testclasspath project is talentacquisition.xml and it is importing the beans.xml file of the Springtest project. Please find the below content of talentacquisition.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<import resource="Beans.xml"/>
<bean id="juggler" class="com.springaction.springidol.Juggler" />
</beans>
I am confused with behavior of the import tag in the talentacquisition.xml How it is able to locate the Beans.xml which is present in the jar (springtest.jar) in the classpath and able to load the beans? Why spring is not giving any error ? Don't I have to modify the import tag in the talentacqusition.xml to following
<import resource="classpath:Beans.xml"/>
If import is able to locate the file Beans.xml , then when should we use classpath: and classpath* :?
ResourceLoaders are responsible for how Spring loads the resource. From the reference manual
The location path or paths supplied to an ApplicationContext
constructor are actually resource strings, and in simple form are
treated appropriately to the specific context implementation.
ClassPathXmlApplicationContext treats a simple location path as a
classpath location. You can also use location paths (resource strings)
with special prefixes to force loading of definitions from the
classpath or a URL, regardless of the actual context type.
The ClassPathXmlApplicationContext you're instantiating "treats a simple location path as a classpath location", i.e. it treats "Beans.xml" as "classpath:Beans.xml". Similarly, FileSystemXmlApplicationContext would treat "Beans.xml" as "file:Beans.xml".
Section 6.7 of the manual has more details too.

Resources