jetty maven plugin in multi-module maven app - java

I have the maven spring application with such structure:
root
-core
-ws
-endpoints
-webapp
Core module contains business logic
Endpoints module contains classes #WebService
Webapp module contains web.xml
Endpoins module has several beans.
My web.xml has link to main context:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
applicationContext.xml imports context of Endpoints module
<import resource="classpath:ws-context.xml"/>
I can launch the application in two ways:
By Idea run configuration (jetty with artifcat)
By jetty-maven-plugin (eclispe)
When I run it by Idea everything is ok. When I use jetty-maven-plugin I have following problem:
Spring finds ws-context.xml, but he doesn't read it. I have found out it by two experiments:
1.I remamed ws-context.xml and spring failed with error.
2.I made ws-context.xml invalid and spring didn't fail.
Unfortunately, I've lost my beans. What is wrong?
Jetty version 9+ (I tried different). My jetty config is below:
<configuration>
<war>${project.basedir}/target/${project.build.finalName}.war</war>
<systemProperties>
<force>true</force>
<systemProperty>
<name>jetty.home</name>
<value>/</value>
</systemProperty>
</systemProperties>
<webApp>
<contextPath>/</contextPath>
<extraClasspath>${basedir}/../../config/</extraClasspath>
</webApp>
</configuration>

Related

#ComponentScan for external dependency package is not working in 2.1.x at the time of building the jar and running that jar

I'm facing an issuing while using the spring boot 2.1.x.RELEASE as it is not scanning the basepackages in component scan but It is working perfectly fine in 2.0.x.
#ComponentScan(basePackages = { "com.app.base*", "com.app.child.*" })
When creating a build using
mvn clean package
It's jar has been created but when running the jar it is not loading the beans from the base external repository.
java -jar child.jar
Exception:
ConfigServletWebServerApplicationContext : Exception encountered during context initialization
- cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException:
Failed to parse configuration class
[com.app.child.UserApiApplication];
nested exception is java.io.FileNotFoundException:
class path resource [com/app/base/service/BaseService.class] cannot be opened because it does not exist
I am adding base.jar in the lib/base.jar of the child project. Following is the pom.xml
<dependency>
<groupId>com.app</groupId>
<artifactId>base</artifactId>
<version>0.2</version>
</dependency>
<repository>
<id>myRepo</id>
<url>file://${basedir}/lib</url>
</repository>
For deploy base.jar into my local repository
mvn deploy:deploy-file -DgroupId=com.app -DartifactId=base-Dversion=0.2 -Durl=file:./myRepo -DrepositoryId=myRepo -DupdateReleaseInfo=true -Dfile=lib/base-0.2.jar
The component scan is well working when I run the application from STS. But when I am creating build and trying to run the jar It is throwing error.
I have also exploded the child.jar using
jar -tf child.jar
base.jar was there in the list.
Difficult to say what exactly is the issue.
First of all for you can use ComponentScan like this
#ComponentScan(basePackages = { "com.app.base", "com.app.child" })
See Spring Framework ComponentScan
Next to it, it looks like that you're trying to run a java file which is not a fat jar or you're not setting the expecting classes into your classpath.
you can add this class by adding a classpath to your call.
java -cp <path/to/base.jar> -jar child.jar
Give it a try. If this is working, at least you have a hint, that you base.jar isn't loaded correctly.
As of 2.1.x the repackage execution has an identifier. we are adding a second repackage so end up with two fat jars.
<executions>
<execution><!--
<goals>
<goal>repackage</goal>
</goals> -->
<id>repackage</id>
<configuration>
<classifier>exec</classifier>
</configuration>
</execution>
</executions>

Why cannot run Java web app on Tomcat 8 if on maven pluggin Tomcat 7 it is working?

I have got a simple project based on Maven. I am using plugin for tomcat 7 in pom.xml:
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/</path>
<port>9090</port>
</configuration>
</plugin>
When I run my project through Intellij using this Tomcat 7 plugin everything work. The problem is when I configured into Intellij e.g. Tomcat 8 and when I run it I have got communicat like below:
HTTP Status 404 – Not Found
Type Status Report
Message Not found
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
What can be the reason that on plugin Tomcat 7 everything work, but does not work on Tomcat 8 (no plugin, just simple configuration)?
My web.xml file looks like:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<servlet>
<servlet-name>ServletAddAdvertisement</servlet-name>
<servlet-class>com.tom.servlets.ServletAddAdvertisement</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletAddAdvertisement</servlet-name>
<url-pattern>/ServletAddAdvertisement</url-pattern>
</servlet-mapping>
</web-app>
I think that during the deployment Tomcat does not see the war file. There should be some way to specify/configure it by deployment descriptor. When I deploy my myPorject.war file manually on Tomcat 8 everything work.
Thank you.
Configuring the server in Intellij there is an option Edit Configuration -> Tomcat Server -> Deployment -> there you need to choose appreciate Artifact.

java web application deployed to google app engine using maven,deployment successful,but for all back end services getting 404 error

am trying to deploy my java/spring based web application to google app engine using maven following below steps
1) In pom.xml added appengine-maven-plugin
<plugin>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>1.9.42</version>
<configuration>
<version>1</version>
<oauth2>false</oauth2>
</configuration>
</plugin>
2) added appengine-web.xml
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<application>XXX-XXX-14807</application>
<version>1</version>
<threadsafe>true</threadsafe>
<env-variables>
<env-var name="env" value="prod" />
</env-variables>
</appengine-web-app>
3) deployed application using mvn appengine:update command
the application deployed successfully,welcome page getting displayed,but
services failing throwing errors like
"The requested URL XXX-XXX-14807.appspot.com/serach.do was not found on the server"(working in my local tomcat).
PS: not used google app engine eclipse plugin for deployment, trying to deploy java project from maven followed https://cloud.google.com/appengine/docs/java/tools/uploadinganapp.
do we need any other configuration?

In EAR project EJBs appear twice when deployng

When I deploy EAR project, I noted that same EJBs appear twice - in WAR and JAR(EJB) modules. Where can be problem?
I build project using Maven. And didn't explicitly define any ejb-jar.xml.
In pom.xml of EJB project I added maven-ejb-plugin:
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ejb-plugin</artifactId>
<version>2.3</version>
<configuration>
<ejbVersion>3.0</ejbVersion>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
<generateClient>true</generateClient>
<clientExcludes>
<clientExclude>**/core/</clientExclude>
<clientExclude>**/utils/</clientExclude>
</clientExcludes>
</configuration>
</plugin>
And from client I refer to generated EJB-client:
<dependency>
<groupId>orgstructure</groupId>
<artifactId>orgstructure-ejb</artifactId>
<version>1.0.4-SNAPSHOT</version>
<type>ejb-client</type>
<scope>compile</scope>
</dependency>
P.S. I deploy project to WebSphere 8.
UPDATE
Generated .ear file have standard layout:
-project.ear
--web-module.war
--ejb-module.jar
--lib
--META-INF
And generated applcation.xml:
<display-name>orgstructure-ear</display-name>
<module>
<web>
<web-uri>orgstructure-web-1.0.3.war</web-uri>
<context-root>/orgstructure</context-root>
</web>
</module>
<module>
<ejb>orgstructure-ejb-1.0.3.jar</ejb>
</module>
<library-directory>lib</library-directory>
Are you building (except for the EJB and web modules) a standalone Java application that is going to use your EJB? I'm guessing not, since you don't mention it anywhere.
If I'm guessing right, don't generate the EJB client. Remove the option
<generateClient>true</generateClient>
from your POM, and all configuration related to it.
An EJB client in this context means an application deployed outside the application server that wants to use your EJBs directly and therefore it needs a client jar to handle remote calls to the beans on the server.
If I'm guessing wrong, you still should not include a dependency to your a client jar in your web archive, Java EE web applications connect to EJBs using built-in app server mechanisms, not generated client stubs. In that case, remove the option
<type>ejb-client</type>
from your WAR's dependency.

FileNotFoundException for bean definition file happens only online, not on localhost

When I published a war file for an application that works locally with Eclipse WTP, I had a FileNotFoundException for the bean.xml file with my beans definitions.
SEVERE: Exception sending context initialized event to listener instance of
class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanDefinitionStoreException:
IOException parsing XML document from class path resource
[META-INF/spring/beans.xml]; nested exception is java.
io.FileNotFoundException: class path resource [META-INF/spring/beans.xml]
cannot be opened because it does not exist
at Caused by: java.io.FileNotFoundException: class path resource
[META-INF/spring/beans.xml] cannot be opened because it does not exist
...
I created the war file with mvn war:war and copied in the webapps folder of Tomcat 7.
beans.xml is located in src/main/resources/META-INF/spring/beans.xml and I've the following in my pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<webResources>
<resource>
<directory>src/main/resources</directory>
</resource>
</webResources>
</configuration>
</plugin>
In the war file beans.xml gets packaged in META-INF/spring/beans.xml
In my web.xml I've:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:META-INF/spring/beans.xml</param-value>
</context-param>
However the file is not found. How to solve the problem?
UPDATE: as Matthew Farwell suggested, is bean.xml is not packaged in the right location, so it's not in the class path, I think it's specified with maven-war-plugin parameters, now I try to look at its documentation. If someone knows it would be helpful.
UPDATE 2: As explained in maven-war-plugin documentation, there is an optional parameter called targetPath. I tried and after changing maven-war-plugin configuration adding targetPath it gets packaged correctly.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<webResources>
<resource>
<directory>src/main/resources</directory>
<targetPath>WEB-INF/classes</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
UPDATE 3: About Ryan Stewart's suggestion, I started my initial pom setup using roo, but after that I've done many changes and I'm not using roo any more. The directory src/main/resources is not mentioned in any other places in pom.xml (I've used grep), however the only setting that looks suspicious to me is:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<configuration><encoding>UTF-8</encoding></configuration>
</plugin>
I've commented out that plugin, but nothing changed, then I commented out the configuration part of maven-war-plugin, but src/main/resources was not added to the war anymore, so for now I've added it back and I'm uploading it to test it online (it's still a staging server actually, not the final one anyway).
UPDATE 4 Ryan Stewart suggested that the problem was that I was running "mvn war:war" instead of "mvn package", and that was indeed the problem. With my targetPath, the resources appeared in WEB-INF/classes, but there weren't any classes there.
I was fighting an uphill battle, while instead the simpler solution was to remove the configuration part as in update 3, and use "mvn package" to build the war file. Thank you to both of you Ryan and Matthew, not only I solved my problem, but I've also learnt something more about Maven.
I have to assume you have another part of the POM that's excluding the file in question from being processed as a classpath resource, else it should be working. Either
Stop doing that, and it'll work fine--the content of src/main/resources becomes classpath resources by default--or
remove the classpath: from your path. Without that prefix, the path given in contextConfigLocation will be resolved against the root of the WAR file, and it will correctly find your file in META-INF/spring.
If you take path 1, then you should remove the webResources section, or you'll end up with the file in two places--not problematic, but potentially confusing.
In a war, / is not part of the classpath for a webapp. The classpath includes /WEB-INF/classes and all of the jars in /lib. See Apache Tomcat 6.0 - Class Loader HOW-TO
WebappX — A class loader is created for each web application that is
deployed in a single Tomcat instance. All unpacked classes and
resources in the /WEB-INF/classes directory of your web application,
plus classes and resources in JAR files under the /WEB-INF/lib
directory of your web application, are made visible to this web
application, but not to other ones.
The other web servers will have similar rules. If you wish to reference something as part of the classpath, put it in WEB-INF/classes.

Categories