Autowired dependencies in a Maven multi-module project - eclipse

I have a single Maven project that has the "core" and the "web" pieces in it. I'm attempting to convert it into multiple modules.
I've taken the "web" packages and put them into the new project. I've added the core project as a dependency and I can reference its classes within Eclipse. The "core" project correctly shows up in WEB-INF\lib in the "web" project when I build it.
The problem comes when classes from the "core" project are #Autowired in the "web" project.
In this particular case, I'm autowiring a service class from "core" into one of the web service classes in the "web" project. If I add the package that the core service is in to my context:component-scan in my "web" application config, it finds that service, but then the service references a repository, which references an entity, which is looking for an entity manager factory that I've got configured in my application-context in my "core" project.
So it seems that maybe the context:component-scan in the "core" project isn't happening maybe? Or maybe the classes that are picked up are not being made available to the "web" project?
I'm entirely new to multi-module projects, so if anyone can get me started in the right direction to resolve this, I'd greatly appreciate it.
I'm using m2e in Eclipse with the Run Jetty Run plugin if that matters; however, I seem to have the same issues when I do a mvn jetty:run from the command line.

You will need to load the core module application-context from your web module application context. Otherwise it will not be aware of beans defined in the core application context.
Take a look the answers for Application context from other maven module cannot be loaded and Spring import application context from another project for proper solutions.
You can also have a look at this post nicely describing how to create a multi-module Spring project using Maven.

Related

Strange issue between Eclipse Mars and Maven

Some context
I am creating a Spring Boot base code application that is in fact separated in three projects/modules:
springbase-core
contains:
custom annotations
custom exceptions
custom generic Spring components
model (entities)
metamodels (generated by maven through the maven-processor-plugin)
utils
depends on: nothing
springbase-data
contains:
data related annotations
services
repositories
data related util classes
depends on: springbase-core
springbase-web
controllers
form classes
filters
listeners
interceptors
JSPs
JSP Tags
etc.
depends on: springbase-data, springbase-core
The problem I am having is that if I have springbase-core in my Eclipse workspace, I'm not able to import metamodels in repository classes in springbase-data. If I remove the springbase-core project (where metamodels reisde) from my Eclipse workspace, it works.
I have also tried copying springbase-core-0.0.1-SNAPSHOT.jar from my .m2 repository, directly at the root of my project (and taking care of adding it in .classpath), and I was able to import metamodels.
When opening springbase-core-0.0.1-SNAPSHOT.jar with WinZip, I can see the metamodels. Building springbase-core with Maven also shows the generated metamodels under /target/metamodel as configured in my pom.xml.
Looks like a glitch between Eclipse and Maven.
Any thoughts?
Here is a solution to my problem: Right click on all projects, click on Maven, and then Disable Workspace Resolution.
That way Maven is going to look for the dependency only from .m2, regardless of what's in my workspace.

Make a deployable Java EE project from a java project

I have coded a small Java EE project that consists only of EJBs, helper classes and their tests.
The project is a maven project and creates a jar.
Right now, all tests are happening with JUnit and JMockit.
Now I want to deploy my application to a local glassfish 4.
What do I have to do, to deploy the project? I know that normaly a root Project with a root-Pom is created, that holds a EAR Project with the ear-Pom and the EJB Project with the ejb-jar.
I checked out the jee-simple-archetype, which was - for my kind of project - much to sophisticated and not simple at all. Could somebody explain how I package and deploy my app correctly?
I know that normaly a root Project with a root-Pom is created, that holds a EAR Project with the ear-Pom and the EJB Project with the ejb-jar
This is not necessarily the "normal" way. You can deploy a .jar containing EJBs on its own. Typically you would do this if the EJB beans are either called remotely (via RMI/#Remote or via SOAP/#WebService) or if the application contains some job-like logic (#Singleton/#Startup, or maybe JSR 352 Batching).
Could somebody explain how I package and deploy my app correctly?
In your pom.xml you should use <packaging>ejb</packaging>, that's basically it (assuming you have an otherwise valid pom.xml). The resulting jar can be deployed on its own. For GlassFish you would use the asadmin deploy command for this.

Can't get eclipse kepler to deploy dependency projects of dynamic web project

I've recently upgraded from Eclipse Juno to Kepler, and have imported some old projects into a new workspace, but unfortunately I cannot get publishing of one of my web projects to work. It has dependencies on a couple of other projects in the same workspace, and these projects are listed in the 'deployment assembly' tab with entries like
Source Deploy Path
/eventserver WEB-INF/lib
that I have added using the 'Add/Project' option (I'm pretty sure I've previously used 'Add/Entries from Classpath' but that option does not list the projects, so I don't know what's going on). But neither the projects' code nor their dependencies are being deployed to WEB-INF/lib when I publish the main project. Also, I have the following warning showing up:
Description Resource Path Location Type
Projects must be referenced by an EAR or a WAR to use classpath publish/export dependencies whose runtime path (../) maps into the parent component. eventserver P/eventserver Classpath Dependency Validator Message
which I only used to get if I didn't have a main project that referred to the utility project.
Any ideas what's going on?
Create an EAR project using this link:
http://www.eclipse.org/webtools/jst/components/j2ee/scenarios/application_creation_tutorial.html
After creation, try to Add the Project in server entry.

Setup a Eclipse Workspace With One Dynamic Web Project and Multiple Java Projects

Can someone guide me to setup a web app project in Eclipse. I want to have a Dynamic Web Project and multiple Pure Java Projects. My Web project will contain JSPs and Java projects will contain Struts Action classes, Spring Bean, Hibernate Entities etc. I want to use those Java Projects as dependency to the WAR. I have done work in such environments, but haven't setup a workspace from the scratch. Can you please guide me or share a link?
Note: I want to use Apache Tomcat or Glassfish as server
In every project you can set the JavaBuild Path properties.
So suppose you have project1 that is dependent on project 2 and some other libraries provided as jars. Go to that option for project1, choose tab Projects and add project2 (that of course is already a eclipse project), than go to tab Libraries and clic on Add external jars.. You are done. This is not dependant by the nature of the project.
Specifically, since you have a Dynamic Web Project, you'll need to go also to Deployment Assembly and add project2 and your jars to the list of files needed by the server.

Grails Project - Common Java Project Dependency in Eclipse

All,
We have standard java projects that contain our common data model, common util classes, etc...
What I can’t seem to figure out is how, to make my Grails project (in Eclipse) have a dependency on the other standard (non Grails) java project in the workspace. When I add the project in the “Java Build Path” the project doesn’t show any compiler errors, but it when I run “Grails Tools -> Refresh Dependencies” or attempt to run the project (run-app) it fails.
This seems like it would be a pretty common thing, but I have searched all over the web and have been unable to find a solution. We are not using maven in our environment. Since we build Eclipse RCP applications on the client side, we use OSGI manifests to manage our project / bundle dependencies.
You can use linked source directories to include the Java or Groovy dependencies in the grails project and you need to set the output directory to Project/target/classes (these configurations go into the grails project preferences in Java Build Path options -> Source Tab).
The change in the output directory allows Eclipse to detect changes in the dependencies and auto load it when running the app using grails run-app.
You can use autocomplete and debug the sources of the dependencies with this configuration.
I prepared a step and step process that includes screenshots for Java and Groovy here
The documentation includes information on customizing the build.
You can add this to your BuildConfig.groovy:
grails.compiler.dependencies = {
fileset (dir:"/path/to/jars")
}
That should get you started.
If the dependencies are not stored in an Ivy/Maven repository then AFAIK your only option is to copy the dependencies to the lib directory of the Grails project.