I have two maven web application, let's say ProjectA and ProjectB which are packed as WAR files. Both applications are using third maven application ProjectC which contains some shared classes. I added ProjectC as dependency in ProjectA and ProjectB.
ProjectA and ProjectB are running in Eclipse using maven jetty plugin, but in production it is deployed as WAR files to the Tomcat web server.
This approach used to work fine until the moment I started to share some hibernate entities. Due to the problem I described in this question, jar-file from persistence.xml is not found in eclipse, it's not possible to link persistence.xml with entities in ProjectC.jar so I have to find some alternative solution.
I thought that it would work if I could compile ProjectC classes when ProjectA and ProjectB are compiled, so the structure looks like:
projectA
-WEB-INF
-classes
-project-a.classes
-project-c.classes
projectB
-WEB-INF
-classes
-project-b.classes
-project-c.classes
rather then:
projectA
-WEB-INF
-classes
-project-a.classes
-lib
-project-c.jar
projectB
-WEB-INF
-classes
-project-b.classes
-lib
-project-c.jar
I don't know if this is possible to do with maven multimodule approach, but some other ideas which will solve the problem are also welcome.
Thanks,
Follow this link to know a way you could use to unpack the resources of some third Project (like your Project C) into another project as resources. It is a excellent and simple guide.
You can take the idea to include the entities as classes of you projects A and B, but it will be easier put the persistence.xml and other common files in a new project and keep the java classes (your entities) into separate jar projects.
Try to set addClasspath=true in war manifest and package your war classes to jar with archiveClasses.
You'll have war structure like this
projectA
-WEB-INF
-lib
-project-a.jar
-project-c.jar
Related
I have a CXF WS project from which I need to create a war file of the whole project and a jar file for a class with main method (along with the dependent classes and jar files) (for code re-usability) using maven. Please help me how to do so. I need to know what I need to give in the pom.xml file Please guide me.
The Maven way of doing things is that you create one artifact (e.g., either a war or a jar) per module. Since you want a jar and a war, you want to utilize at least two modules. Each module goes in its own directory with its own pom.xml file. Presumably, the war would have a dependency on the jar.
Maven has lots of support for multi-module projects to ease the maintenance and sharing of configuration between the multiple modules.
I have two Maven projects (inside NetBeans IDE) that produce war files. Let us call them Project-A and Project-B.
Project-A is a big web application. It consist of many packages, classes and dependencies.
Project-B is some kind of sub-project of Project-A. It is an application that does a specific task that requires classes from Project-A.
My problem lies in my inability to configure Maven in Project-B to import classes from Project-A along with their dependencies. All my tries ended in either compilation errors or ClassNotFound exceptions.
I would want to kindly ask for help how can I configure Maven in Project-B, so I can use the classes from Project-A and the make the resulting WAR contain all the required dependencies.
Thank you for your time,
Adam
I assume the classes from Project-A get into WEB-INF\classes. In this case they are meant to be used only from that war.
Extract classes from Project-A in a jar.
Use the jar in both Project-A.war and Project-B.war
In my opinion, if it's possible, there will be a JAR containing all the business services and classes that project A and B are sharing.
Both projects have this Jar has dependency.
And may be, if it's your need, i will create an EAR not a WAR, with an EAR maven project with project A and B in dependencies.
I have the enterprise project (*.ear), what packaged by maven.
Ear file have include many files:
final.ear
|-lib
|-META-INF
|--web.war
|--bla-bla.jar
|--web-bla.war
Each file (jar, war) packaged by maven.
1) How put all required libraries from all files (war, jar) into final.ear/lib?
2) How group libraries from /lib, for example: lib/axis, lib/logging?
Using the Maven EAR Plugin - Skinny Wars feature has worked well for me for this problem.
1 EJB jar) in the pom.xml of the jar make the dependency "not provided" so Maven knows the dependency is needed on the runtime classpath and thus to be deployed with the application - that will trigger it to add it to the ear/lib.
1 war) Basically what you describe here is that "instead of packaging the dependency with the war, package it in the parent EAR". In Maven terms you then add the dependency to the pom.xml of the ear (so it is on the runtime classpath there), and you mark the dependency as provided in the pom.xml of the war (so it is on the compile time classpath but not on the runtime classpath there). OR use the skinny wars feature as Steve C suggests.
2) The only way I would see this happening is if you micromanage the dependencies in the pom.xml of the ear entirely:
http://maven.apache.org/plugins/maven-ear-plugin/examples/customizing-module-location.html
I have a spring project and using ANT to compile/deploy my war to Tomcat. So basically it just creates a war file and moves it to tomcat folder.
folder structure is this:
Spring
-src
ALL MY SRC (JAVA) files
-war
-WEB-INF
-jsp
-lib
ALL MY LIBRARIES
-properties
web.xml
spring-servlet.xml
-META-INF
build.xml
pom.xml // putting my pom.xml here
With ant I download all my libraries manually. Now as it seems, maven downloads all libraries automatically, thanks to pom.xml. Questions/problems:
Must I change my folder structure with maven?
Will maven include all downloaded libs to WAR? Is that default?
When maven downloads its WARS, can it put all libraries to WEB-INF/lib? How?
Does it make sense what I am doing, if no, then why?
Have few questions, because I have never ever used maven.
Yes better change the structure, see the maven site. It saves a bit of hassle, should you start using maven-plugins with complex things like using XSLT to generate java sources. Also IDE support might be better. Like:
src/main/java, src/test/java, src/main/resources.
Yes.
Yes. Automagically.
Yes otherwise you should use Ivy with ant.
Standard Maven Web Project Structure
As millimoose commented its better to go for standard project structure.
I have a project which imports another projects to its build path. When I clean the project to compile classes, the classes imported from external projects are not showing up as part of the compiled classes directory.
Do anyone know how to make sure these external import classes gets compile and included into my build classes?
Thanks
I don't think it's possible. But when you deploy your project, you'll certainly want to create a jar file to hold all your classes. And Eclipse has a wizard to export a jar file and choose to embed dependencies in the jar.
The usual way, however, is to have each project generate its own jar, and to use all the jar of the project + the jars of the dependencies as the classpath of the deployed project.
#Bitmap,
is this a WAR or EAR project?
In EAR project you have to specify clearly the "JAVA EE Module dependency" to include the referenced projects.
If you include a project as a build dependency, it will be for compile-time only.
If these are simple java projects you may want to look at "JAR JAR" enter link description here
to achieve this.
HTH