Ant: Not finding my #ContextConfiguration - spring-mvc

We have a Spring 3 MVC application and JUnit test cases for spring controllers. The Junit version is 4.8.1 which supports ContextConfiguration annotation.
Here is how I am adding context configuration in my test stub
#ContextConfiguration(locations = "file:WebContent/WEB-INF/myappconfig.xml")
I am able to run the junits locally, but when I am trying to run this with my ant build script, it throws an error java.lang.IllegalStateException: Failed to load ApplicationContext
I tried to set classpath element to the WebContent, then upto web-inf, nothing works out.

i'm using
#ContextConfiguration({ "classpath*:application-test.xml" })
and my application-test.xml resides under main/resources

Related

Need to provide addtional jars to for xsbt-web-plugin container

I am trying to run my war file using xsbt-web-plugin. My war itself does not contain tomcat-jdbc-pool jar and javax-servlet-api. I tried using container configuration. But sbt complains configuration does not exist.
If I use provided configuration, webapp-runner fails.
I am sure I am missing something. But unable to figure it out.
WARNING: Failed to register in JMX: [javax.naming.NamingException: Could not load resource factory class [Root exception is java.lang.ClassNotFoundException: org.apache.tomcat.jdbc.pool.DataSourceFactory]]
What version of xsbt-web-plugin are you using? Can you post the contents of your sbt build configuration?
The Tomcat plugin in xsbt-web-plugin relies on webapp-runner, which does not appear to bundle the tomcat-jdbc library. You can add it via containerLibs in Tomcat:
enablePlugins(TomcatPlugin)
containerLibs in Tomcat += "org.apache.tomcat" % "tomcat-jdbc" % "8.5.15"

how to set active profile for gradle build of spring boot application?

Here is the DbConfig of my spring-boot application.
#Configuration
#EnableTransactionManagement
public class DBConfig
{
#Bean
public LocalSessionFactoryBean sessionFactory()
{
....
}
#Bean
public DataSource aaDataSource()
{
.....
}
public PlatformTransactionManager transactionManager()
{
....
}
private Properties hibernateProperties()
{
....
}
}
Here is my test class
#RunWith(SpringJUnit4ClassRunner.class)
#SpringApplicationConfiguration(classes = Application.class)
#WebAppConfiguration
public class ApplicationTests {
#Test
public void contextLoads() {
}
}
Its a gradle project.
when I run gradlew clean build locally, I get successful build since my connection settings in application.properties matches my sql connection.
But when I run from jenkins box in our qa environment (the database is qa one), the build fails with following exception.
java.lang.IllegalStateException: Failed to load ApplicationContext
....
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Factory method 'entityManagerFactory' threw exception; nested exception is com.zaxxer.hikari.pool.PoolInitializationException: Exception during pool initialization
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(Abstract
.....
Caused by: com.zaxxer.hikari.pool.PoolInitializationException: Exception during pool initialization
at com.zaxxer.hikari.pool.BaseHikariPool.initializeConnections(BaseHikariPool.java:544)
at com.zaxxer.hikari.pool.BaseHikariPool.<init>(BaseHikariPool.java:171)
at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:60)
at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:48)
.....
Caused by: java.sql.SQLException: Access denied for user 'admin1'#'localhost' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:998)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3835)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3771)
I tried running gradlew clean build -Dspring.profiles.active=qa with application-qa.properties in the src/main/resources/ that have qa db settings. The build still failed with same exception.
I have two options.
run the build skipping datasource bean creation. I still need my units tests to be run since they don't rely on datasource
or pass the right settings to gradle build so that application context is created.
I prefer second option to get it working
If your requirement is that you control this externally (i.e., via the command line when launching Gradle), you can then modify your Gradle test task configuration as follows.
test {
systemProperty("spring.profiles.active", project.properties.get("springProfiles"))
// ...
}
And then you can set a value for springProfiles like this: gradlew clean build -PspringProfiles=ci (where ci is the name of the profile you want active on the CI server).
The above will make spring.profiles.active available as a JVM system property for your tests; however, you'd still need to set the active profiles for the Spring TestContext Framework.
To do that, you need to annotate your test class with #ActiveProfiles, but instead of passing in static profiles you'd need to implement a custom ActiveProfilesResolver and register it via #ActiveProfiles(resolver = MyCustomResolver.class). Your customer resolver could then read then simply return the value of the spring.profiles.active system property.
Another option is to implement a custom ApplicationContextInitializer that programmatically sets the active profiles (similar to the custom ActiveProfilesResolver). You can configure one of those via #SpringApplicationConfiguration(initializers = MyCustomInitializer.class).
And yet another option would be to programmatically set the active profiles directly in your SpringApplication -- for example, based on a system property or environment variable.
So, you have several options.

Spring web application using spring-integration-dsl-groovy throwing an error "java.lang.NoClassDefFoundError: org/springframework/integration/Message"

I am trying to run a simple spring web application with spring-integration-dsl-groovy, but i am getting an below error
org.springframework.web.util.NestedServletException: Handler
processing failed; nested exception is java.lang.NoClassDefFoundError:
org/springframework/integration/Message
Can anyone help me?
Here is my code
#RestController
class SampleController{
#RequestMapping("/welcome")
String start(){
def builder = new IntegrationBuilder()
def flow = builder.messageFlow{
transform {String payload -> "Welcome $payload - Powered by Groovy !!!"}
handle{ println payload }
}
return flow.sendAndReceive("to Spring MVC ")
}
}
Added all required dependecies, including spring mvc, groovy, spring integration
compile "org.codehaus.groovy:groovy-all:$groovyVersion"
compile "org.springframework.integration:spring-integration-core:$springIntegrationVersion"
compile "org.springframework.integration:spring-integration-dsl-groovy-core:$springInegrationDslGroovyVersion"
spring-integration-dsl-groovy-core 1.0.0 is not compatible with Spring Integration 4.1.4. spring-integration-dsl-groovy-core 1.1.0 is latest version available and its compatible with Spring Integration 4.1.4

seam solder (former weld-extensions project) is not initialized

I want to use logger in my java web application.
I'm using JBossAS 6.0.0.final, cdi (weld), jsf ... etc. Seam solder proposes to use an abstract logger is not tying to a concrete implementation (slf4j, log4j, etc) using jboss-logging api.
In order to get this logger in your code will need to write
# Inject
org.jboss.logging.Logger log
seam-solder.jar has the producer for this logger.
package org.jboss.seam.solder.log;
...
class LoggerProducers
{
# Produces
org.jboss.logging.Logger produceLog (InjectionPoint injectionPoint) {}
}
When I deploying my application, I get an error
15:51:18,300 ERROR [org.jboss.kernel.plugins.dependency.AbstractKernelController] Error installing to Start: name=vfs:///C:/Java/jboss-6.0.0.Final/server/default/deploy/kamis-web-client.5.0.0-SNAPSHOT.ear_WeldBootstrapBean state=Create: org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied dependencies for type [Logger] with qualifiers [#Default] at injection point [[field] #Inject private ru.kamis.suite.webclient.web.breadcrumbs.BreadcrumbsManager.log]
This is due to the seam-solder.jar has not META-INF/beans.xml file, and it is necessary for cdi container.
If to add beans.xml file in seam-solder.jar manually, then the application works WELL.
How to do without hacks?
To build my application I use maven, so my solution is not comfortable and NOT fine.
PS: Former weld-extensions project contained META-INF/beans.xml file in jar.
with seam-solder-3.0.0.Beta1 there should be no need to modify the jar

Where is class weblogic.jndi.WLInitialContextFactory?

when trying to execute my jar file I get an exception:
javax.naming.NoInitialContextException: Cannot instantiate class: weblogic.jndi.WLInitialContextFactory
[Root exception is java.lang.ClassNotFoundException: weblogic.jndi.WLInitialContextFactory]
I guess this is some kind of missing library on the classpath.
Can anyone tell me which jar-file is missing? I can't find the class weblogic.jndi.WLInitialContextFactory anywhere...
Thanks!
P.S.: I already have weblogic 10.0 jar included.
Check your server/lib/ folder to find wliclient.jar.
With Weblogic 12.1.3, you can find it here:
${INSTALL_DIR}/inventory/wlserver/server/lib/wlclient.jar
Step 1:
Go to E:\weblogic81\user_projects\domains\mydomain. Then type Setenv command. As follows
E:\weblogic81\user_projects\domains\mydomain>setenv
Step 2:
Weblogic.jar file is needed by your client application. It may contain in the following path E:\weblogic81\weblogic81\server\lib\weblogic.jar. so set the classpath for the this folder or copy this weblogic.jar file into your application-folder so that weblogic.jar file is available to your application first.
E:\weblogic81\user_projects\domains\mydomain>set CLASSPATH=%CLASSPATH%;E:\weblogic81\weblogic81\server\lib;.
Step 3:
Go to domain folder in command prompt as shown above and set classpath.
To not to disturb other classpaths set classpath as:
set CLASSPATH=%CLASSPATH%;E:\weblogic81\weblogic81\server\lib;.
Here (.) dot represents set classpath to current directory.
Step 4:
After classpath set run command STARTWEBLOGIC as follows:
E:\weblogic81\user_projects\domains\mydomain>STARTWEBLOGIC
Step 5:
Do not login to weblogic server. If you are already login just log out and write the following code in myeclipse or some other IDE.
Step 6:
package directory.service;
import java.util.*;
import weblogic.jndi.*;
import java.io.FileInputStream;
import javax.naming.*;
public class GetInitContext {
/**
* #param args
*/
public static void main(String[] args) {
try{
weblogic.jndi.Environment env=new weblogic.jndi.Environment();
weblogic.jndi.Environment environment = new weblogic.jndi.Environment();
environment.setInitialContextFactory(
weblogic.jndi.Environment.DEFAULT_INITIAL_CONTEXT_FACTORY);
env.setProviderUrl("t3://localhost:7001");
env.setSecurityPrincipal("agni");
env.setSecurityCredentials("agnidevam");
Context context=env.getInitialContext();
System.out.println("got the initial context for weblogic server---> "+context);
context.createSubcontext("sone");
context.bind("agni one",new Integer(10));
context.createSubcontext("sone/sctwo");
context.bind("agni two",new Integer(20));
context.createSubcontext("sone/sctwo/scthree");
context.bind("agni three",new Integer(30));
System.out.println("subcontex object created please check in admin server for more details");
}
catch(Exception e){
System.out.println("file inputstream exception ---> "+e);
}
}
}
Step 7:
Execute the above code and login to weblogic and right click on myserver>view jndi tree> you find the bound objects information.
it looks you are doing a JNDI lookup outside of WLS.
You need to use wlfulclient.jar or if your machine has a WLS installation then add to your classpath project: WL_HOME/server/lib/weblogic.jar
I faced the same issue and it's fixed now :)
The fix is, to go to WebLogic server and navigate to /Oracle/Middleware/wlserver_10.3/server/lib/ and execute the below command.
Command: java -jar wljarbuilder.jar -profile wlfullclient5
The above command creates a jar file with all the jar's inside WebLogic server /lib folder and place it in your client java code build path Eclipse and craetes runnable JAR file and place this wlfullclient5.jar file in server/lib folder as well.
Hope this helps! Kindly let me know if you have any issues.
Adding wlserver/server/lib/weblogic.jar is enough. I test it.
Check the following tag in your build.xml
property name="WLS_HOME" value="${env.WLS_HOME}"
where WLS_HOME=c:\weblogic\wls\wlserver if running on windows
i kept trying to run a simple hello world program and it kept throwing
*run:
[echo] Executing client class
[java] javax.naming.NoInitialContextException: Cannot instantiate class: weblogic.jndi.WLInitialContextFactory [Root exception is java.lang.ClassNotFoundException: weblogic.jndi.WLInitialContextFactory]*
once i changed the above mentioned tag it in the build.xml it worked fine
It is packaged inside of the weblogic.jar under your server/lib.
in version 12c it is located in weblogic-classes.jar in your lib directory:
C:\wls1213\wlserver\server\lib
For WLS 12.2, where WL_HOME is The BEA home directory of your WebLogic installation
(as defualt WL_HOME is Middleware\Oracle_Home\wlserver)
%WL_HOME%\server\lib\wlclient.jar
%WL_HOME%\server\lib\wls-api.jar
%WL_HOME%\server\lib\wls-api-part.jar
%WL_HOME%\server\lib\wlthint3client.jar
all these libs contains the: jar: weblogic\jndi\WLInitialContextFactory.class
see WLS doc.: https://docs.oracle.com/en/middleware/fusion-middleware/weblogic-server/12.2.1.4/wlprg/overview.html#GUID-FC14CC53-DE49-456F-B54C-D73CC6DBF818
I've faced the issue stated here and I've managed to solved by fixing WL_HOME enviroment variable.
In my case the wlserver_10.3 folder was moved to another drive (From D to E) and the guy who did the disk "migration" forgot to change the WL_HOME value at PATH\TO\Oracle\Middleware\wlserver_10.3\common\bin
By fixing the wlserver_10.3 path I was able to deploy JAR's at WebLogic

Resources