Maven Websphere JNDI Resource Binding - java

I have a small issue in which my maven project does not have a Target Resource JNDI Name.
A brief overview of this project. I have three java projects:
The Maven Ear, the back end (base livraries) project and the UI project. All of which are maven, have dependencies I need, compile and run with a main app which emulates the WAS.
So far the most relevant help I could find was to make sure I have a ibm-web-bnd.xml
I do have this file as I converted an existing UI project to a maven project. The project looks like this.
In my web.xml I also have
<resource-ref>
<description>
</description>
<res-ref-name>jdbc/DataSource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
In my ibm-web-bnd.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-bnd
xmlns="http://websphere.ibm.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-web-bnd_1_0.xsd"
version="1.0">
<virtual-host name="default_host" />
<resource-ref name="jdbc/DataSource" binding-name="jdbc/DataSource"/>
As it has been pointed out, I did not ask a question.
My question is how do I set a JNDI Resource Reference for my Maven UI project in WebSphere. I have all the required files and bindings for the project yet I can not run the maven EAR with the UI project in it.
I always get the error:
A resource reference binding could not be found for the jdbc/DataSource resource reference, defined for the codecoverageUI component.

Related

Generate Deployment Descriptor Stub menu missing in Eclipse webapp project

The menu in the title is missing.
I created a Maven webapp project as instructed in my book, and my book is now saying generate a web.xml by right-clicking on the project , Java EE Tools -> Generate Deployment Descriptor Stub , but that submenu is missing.
I have checked project facets and Dynamic Web Module is checked, with version no. 2.3.
Do I need to convert this Maven webapp project to a Dynamic Web project? It seems a Maven webapp project is somehow not a Java EE project. Is that correct?
Thanks in advance.
Edit: I have checked a lot of similar questions but have not been able to find an answer to this apparently trivial problem.
Edit: FWIW there is some information about conflicts in the Project Facets window:
Solved. The things I did to solve this were:
1, edit [projectname]\.settings\org.eclipse.wst.common.project.facet.core.xml. I changed the "jst.web" version to 4.0:
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<runtime name="Apache Tomcat v9.0"/>
<fixed facet="wst.jsdt.web"/>
<installed facet="jst.web" version="4.0"/>
<installed facet="wst.jsdt.web" version="1.0"/>
<installed facet="java" version="1.8"/>
<installed facet="jst.jsf" version="2.2"/>
</faceted-project>
2, tried the usual, F5, Project Clean.., Maven->Update Project, etc to no avail.
3, restarted eclipse, to no avail
4, noticed there was now a WEB-INF/web.xml but it needed editing to bring it into line with the dynamic web module version (4), so I changed it to this:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<display-name>Archetype Created Web Application</display-name>
</web-app>
5, F5, Project Clean.., Maven->Update Project, "mvn clean install" and it seems eclipse is happy.

Websphere resource binding

We have a project that have quite a lot of dependencies. I am confused in Websphere resource binding and resource definition at all now.
ejb-jar.xml describes resources.
persistance.xml describes database resources.
We can have web.xml, where we describes resources also.
We can have ibm-web-bnd.xml, where we can have binding for resources.
Lets say, I have a *.jar that has definition of data source in persistance.xml (jta-data-source), same data source in ejb-jar.xml (enterprise-beans/session/resource-ref), and also has binding for this data source ibm-web-bnd.xml (resource-ref).
Now I want to use this *.jar in my *.war.
Lets assume that I have configured all resources on my Websphere Liberty server correctly.
During application start up, I've got following error for this *.jar:
Unable to use JDBC Connection to create Statement
java.sql.SQLException: Unsupported use of GenericConnection. A
GenericConnection is provided during application start when creating an
EntityManagerFactory for a persistence unit which has configured one of its
datasource to be in the component naming context; java:comp/env. During
application start, the component naming context will not exist, and the
correct datasource cannot be determined. When the persistence unit is used,
the proper datasource and connection will be obtained and used.
Questions:
Why my *.jar do not see Data Source, that I have defined on my Websphere Libery (in server.xml)? Do I miss some binding in my *.war?
Why do we describe resources in web.xml? I thought that this is definition of web application, like a servlet mapping, filters, etc.
Why same data source is described in both persistance.xml and ejb-jar.xml (is it not enough with persistance.xml?)?
Does resource binding delay somehow dependency injection?
Code in *.jar that I can't modify, but have to use in my *.war:
ejb-jar.xml
...
<enterprise-beans>
<session id="MyEntityManagerBean">
<ejb-name>MyEntityManagerBean</ejb-name>
<ejb-class>somepackage.MyEntityManagerBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
<resource-ref id="some_id_goes_here">
<res-ref-name>jdbc/my_ds</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
</session>
</enterprise-beans>
...
persistance.xml
<persistence-unit name="MyPersistentUnit" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>java:comp/env/jdbc/my_ds</jta-data-source>
...
</persistence-unit>
ibm-ejb-jar-bnd.xml
...
<session name="MyEntityManagerBean">
<resource-ref name="jdbc/my_ds" binding-name="jdbc/my_ds"/>
</session>
<session name="MyEntityManagerBean2">
<resource-ref name="jdbc/my_ds" binding-name="jdbc/my_ds"/>
</session>
...
MyEntityManagerBean.java (also same *.jar)
#PersistenceContext(unitName="MyPersistentUnit")
protected EntityManager entityManager;
And problem starts, when I add this *.jar as Maven dependency to my *.war.
Thanks in advance.

Can a class in one WAR be referenced in web.xml of another WAR in the same EAR, in Wildfly 10?

I have an ear with 2 .war files.
In war #1, under WEB-INF/classes/com/my there is a BatchTriggerBuildServlet.class
In war #2 i have the following in its web.xml (in its WEB-INF)
(a reference to a class in war #1):
<web-app id="WebApp">
<!-- other stuff -->
<servlet>
<servlet-name>BatchTriggerBuildServlet</servlet-name>
<display-name>BatchTriggerBuildServlet</display-name>
<servlet-class>com.my.BatchTriggerBuildServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BatchTriggerBuildServlet</servlet-name>
<url-pattern>/BatchTriggerBuildServlet</url-pattern>
</servlet-mapping>
<!-- other stuff -->
</web-app>
This is deployed in Wildfly 10.
I also have a jboss-deployment-structure.xml in the META-INF folder of the containing .ear containing the following:
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
<!-- Make sub deployments NOT isolated by default, so they can see each others classes without a Class-Path entry -->
<ear-subdeployments-isolated>false</ear-subdeployments-isolated>
</jboss-deployment-structure>
Is this legitimate? Because I'm getting a ClassNotFoundException for the above class when i try to deploy the ear, along with the message that it
"Failed to start service ... from [Module "<my ear name>.<my war #2 name>:main" from Service Module Loader]"
Is there a way to get this to work?
thanks in advance.
ear-subdeployments-isolated does not apply to web modules, which are always isolated from each other. See Class Loading in WildFly.
Try moving the classes and their dependencies into a jar in the EAR/lib directory.

How to load an external property file to Spring Boot in Wildfly

I wonder how can I load an external property of my application running inside Wildfly 9 as WAR, I tried to add a java parameter to Wildfly execution but it seems the application did not recognize the properties.
-Dspring.config.location=file:///C:\Temp\config\application.properties,classpath:application.properties
Is there any way how Spring Boot could read the external property file? I am trying to load and run one Spring Boot application as WAR inside Wildfly.
I'd appreciate any help.
Thanks.
In my linux system i have an entry in the standalone.xml:
<system-properties>
<property name="spring.config.location" value="file:/opt/jboss/wildfly/standalone/configuration/"/>
</system-properties>
I just defined the directory here, so it has to end with an "/".
In the directory "/opt/jboss/wildfly/standalone/configuration/" there is my application.yml.
If your springboot application is running inside Wildfly you don't need to read standalone.xml as an external file.
Get the property value directly with System.getProperty(PROPERTY_NAME);
I know it's a bit late for the answer, but maybe this helps:
Within your deployment-descriptor (web.xml), create environment-variables pointing to the spring config location file, i.e.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<env-entry>
<env-entry-name>spring.config.location</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>file:[path-to-file]/[your-properties-file]
</env-entry-value>
</env-entry>
</web-app>
Working on Wildfly 25.0.0

java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet in spring project

one hello.jsp
web.xml is
<?xml version="1.0" encoding="UTF-8"?>
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- The front controller of this Spring Web application, responsible for
handling all application requests -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
dispatcher-servlet.xml is
<?xml version="1.0" encoding="UTF-8"?>
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
xmlns:p="http://www.springframework.org/schema/p">
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean name="/hello.html" class="com.spring.HelloWorldController"></bean>
</beans>
JAR Files are:
spring.jar
spring-webmvc.jar
spring-aop
spring-beans
spring-context
spring-context-support
spring-core
spring-jdbc
spring-orm
spring-source
spring-test
spring-tx
The ClassNotFoundException clearly indicates that you are missing org.springframework.web.servlet classes.
If you are not using Maven, make sure you include all the appropriate Spring JARs.
If you are using Maven, make sure you include the spring-web dependency:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version><!-- Your spring version here --></version>
</dependency>
If none of these work, take a look at this thread.
In my case I used Ivy and I faced the same issue. You can do either of the two
Either move your libraries to WEB-INF/lib . Because this is the
folder from where Eclipse searches for corresponding jars. OR
Let Eclipse know that it can search the jars from ivy library folder
which is not same as WEB-INF/lib i.e Change java build path in Deployment Assembly via Project properties.
For 2nd approach you can refer details post with screenshots (Link to my personal blog with more detials). Or you can also go through similar question I asked here.
Problem:
java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet in spring project
The issue is necessary jar are not present in the proper classpath
Solution
Place all the necessary jars in the classpath .Since the project is dynamic webproject place all the spring jars in the WEB-INF/Lib folder
The issue will be resloved
I had a similar issue and I resolved it this way. If all the required libraries are added and you are still getting this error. Try running this in command line:
mvn eclipse:eclipse
then
mvn clean install
If this doesn't resolve it, rightclick on your eclipse project,
go to >>properties >> targeted runtime
then click the checkbox next to
apache tomcat v8.0
depending on the version of you tomcat.
If you are running jboss, choose a jboss version instead.
and then after this run the above 2 commands(mvn eclipse:eclipse and mvn clean install) again.
Problem: java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet in spring project.
By adding the below jars into WEB-INF/Lib folder we can resolve this issue.
org.springframework.asm-3.1.4.RELEASE.jar
org.springframework.aspects-3.1.4.RELEASE.jar
org.springframework.beans-3.1.4.RELEASE.jar
org.springframework.context-3.1.4.RELEASE.jar
org.springframework.context.support-3.1.4.RELEASE.jar
org.springframework.core-3.1.4.RELEASE.jar
org.springframework.web.struts-3.1.4.RELEASE.jar
org.springframework.web.servlet-3.1.4.RELEASE.jar
org.springframework.web-3.1.4.RELEASE.jar
Of Course you will be added in Build Path but it will take up to Compile time only. So we have to added above jars into WEB-INF/Lib folder

Categories