I have a custom web application project, where project and webapp are Eclipse projects. Assume a structure like the following
/project
/webapp
/_src
/com.webapp.servlets
/com.webapp.domain
/com.webapp.services
Now I want to create java command line based tool to simulate the operations of a servlet. It's going to require classes from my domain package. I also need to split this command line application from the web application project. I would also need to extract those shared classes into another project as well.
I would like to have this type of structure
/project
/webapp
/_src
/com.webapp.servlets
/com.webapp.services
/commandline
/_src
/com.commandline.simulation
/shared
/_src
/com.webapp.domain
How can I achieve this nested project structure with shared resources in Eclipse (Indigo)?
This was pretty simple. Create a new folder in your root project project. Then Create a Java Project but don't use default location, use the new folder you created. This will create a new entry in your project explorer, but on the file system you will have nested projects.
In projects that require resources from other projects just configure the build path by adding the projects you need,
Related
I started to learn JSP and Tomcat Container, and I know how the directories must be stored, but I'm confused by several things:
If we use only web app, why do we need the rest of the folders here? (highlighted blue). Can I just delete them and load my project with "web" as root?
How can I initialize Gradle/Maven or use any other framework inside Tomcat web application? (e.g Where to put pom.xml file?)
The folder .idea and the file Tomcat-web-app.iml are necessary for your IDE, configuration is stored there. If you delete them the project will fall apart in your IDE. You can delete the src folder as long as you're not using it.
pom.xml should be put on the root level of your project, so right under Tomcat-web-app/. Afterwards you can initialize it as a Maven project by adding framework support for Maven - right click on your project (Tomcat-web-app) and you should see it there. Refer to this guide if you're having issues. It is much more straight forward however to just create a Maven project from scratch and then copy in your web files.
I've seen different packages in source code such as com.website.package These packages are used across multiple applications, and I've been trying to accomplish something similar.
The only way I know of to achieve using the same packages in different projects is to copy each source file into the new project which would create the new packages. this probably isn't the preferred method, as it could possibly drag thousands of files into the project (see Java's library).
How would this be achieved?
TL;DR: How can I use a custom written package in multiple projects without copying many files? (aka Java's libraries).
Take the class files and create a jar. Put the jar on the classpath of all the applications that need those classes.
How do you do that?
Inside an IDE, you can create multiple projects and put the shared java code in one project. Make the other projects depend on that project and you can share things within the IDE. That means you run inside the IDE and it will use the shared code.
For example, in Eclipse, you choose the project that will use the shared code/project. Right click on it in the package view or navigator view and choose "properties". Select the option to set the build path and there is a tab for selecting projects that this project depends on. Select the shared project and then it's code is callable from that project.
In this case, any change you make to the shared project will be immediately available, inside the IDE, to those projects that depend on it. Run them right then and the change will be in effect.
For running outside the IDE ...
If you are using Eclipse (or some other IDE) it will have an option for creating a jar. In Eclipse you create a java project and move the classes you want to be in the library/jar into this project. Then, once you have all the right classes, including those that others depend on, you will do some sort of build to create the jar.
Eclipse has an 'export' option on the 'file' menu. Use that and select to export as a "java" > "jar" and then select the project you just made.
You can also create a Maven project of type "jar" into which you put all those classes (as java files) and then building that project with Maven will create the jar. The "install" goal for Maven will deploy it to your local repository.
Any time your shared code is shared by way of a jar, you will have to rebuild the jar and copy it into the location(s) from which it is shared by other projects before changes inside the jar take effect.
Maybe export the package to some place on the drive and create linked folders to it in the projects?
I've two Spring MVC projects in Eclipse, but the web content is in different positions.
In one app it's located under:
/WebContent
In the other app under:
/src/main/webapp
Why that? What's the difference?
Is there any way I can see how each project was created in order to create a new one in the same fashion?
Project directory structure depends on how it was created and the build tool which you are using.
If you create by selecting File -> New ->Dynamic web project in eclipse, then you find the /WebContent directory.
If it is created using Maven, directory strutcure will be /src/main/webapp.
Please go throuh here for more information on Maven project strutcure.
If someone like me messed up with the project and ended up with both folders but wants to keep the maven structure just disable/re-enable the Dynamic Web Module facet and configure the content directory
Finally, delete the WebContent folder.
I have an existing project that I'm trying to convert correctly into an Eclipse Dynamic Web Project. I've mostly got it working, however there is on issue that I'm not sure how to fix.
The project has something like 2 modules that share the same Java classes, but need to be deployed as separate webapps into the app server (Tomcat 5.5).
So the folder structure is something like
/Webcontent_root
----/app1
..../WEB-INF etc...
----/app2
..../WEB-INF etc...
Is this something that can be controlled within eclipse?
Thanks
I guess, eclipse can't control project with more than one context.
Approach I.
I think you should create three projects: regular java project with classes, one dynamic web project for first webcontent and one for second. Than tune up "Java Build Path" and "Deployment Assembly" settings.
Approach II.
Or you can create two web projects, first with classes and webcontent, second with webcontent and link to first project ("Java Build Path" and "Deployment Assembly" settings).
Create 2 web project and 1 java project for the shared java source. Both web project can use the shared project as a jar file in their web-inf/lib
As #Nantipov says, use the deployment assembly screen to make web projects get the shared java project as a jar in the library filder.
Yes, it can--just not with the UI in WTP (deployment has always been a sore spot and ongoing effort to improve). The .settings/org.eclipse.wst.common.component in your project controls what Eclipse/WTP thinks of as web content root(s) in your project. Odds are you just need to create a new folder in the project and add it as a sibling to the existing element in that file, but with a correct source-path value.
I have a Java Project, for which I'm now creating a Web interface, using a Dynamic Web Project from Eclipse. The Web project consists of a single servlet and two JSP's. Something like this:
/JavaApplication
/src
/lib
/resources
/WebApplication
/src
/Servlet.java
/WebContent
/WEB-INF
index.jsp
other.jsp
Now, I need to reference JavaApplication from WebApplication, in order to use its classes to process web requests. What's the best way to accomplish this ? My idea is to create a .jar of the JavaApplication, containing all the .class files, /resources, and /libs. In this way, I could include the .jar in the web application, and I could have a single .war file that contained the entire application.
What do you think? How is this problem typically solved ?
Note: I don't want to convert the Java Project into a Web project.
In Eclipse project properties, add the project to the Java EE Module Dependencies (Eclipse 3.5 or older)
or Deployment Assembly (Eclipse 3.6 or newer) entry in the project properties.
This way Eclipse will take care about doing the right thing to create a WAR out of this all (it will end in /WEB-INF/lib). No other configuration is necessary, even not some fiddling in Build Path.
Under Eclipse, you can declare Project References for a given project, the web application in your case. To do so, right click on your web application project, then go for Properties > Project References and select the JavaApplication project. This should allow you to call code from the JavaApplication project from the WebApplication without having to build a WAR. This is a solution for development.
For standard deployment (outside the IDE), you should indeed create a standard WAR. To do so, you'll have to package your JavaApplication as a JAR including the .class files and the files under /resources but not the libraries it depends on (JARs under /lib). These dependencies will actually end up in the WEB-INF/lib directory of the WAR, beside the JAR of your JavaApplication. These steps are typically automated with tools like Ant or Maven.
Connecting java app to web app for development :
right click on web project :
properties>project references> add the java project you want to refer
Now in properties tab of web project go to
properties>deployment assembly> add the project manually and run the app
Consider moving up to EAR level, if your web container supports that.
The tricky part with shared code is where should the common code be put. A copy pr web application? A copy in the web container? Overdoing the "share these classes" might end up in class loader problems.
If you are creating two separate web applications refactor common java code into a separate Eclipse project and refer to it from both WAR projects.
EDIT: Apparently I have misread the problem description, and thought you asked about an existing and a new web application sharing code.
If you have an Eclipse project with your application, and another with your web frontend, then you can let your application export the necessary resources which the "Export WAR" in Eclipse Java EE can wrap up in a jar file and put in WEB-INF/lib for you. You need to say this explicitly with a checkmark in Properties -> Java EE Module Dependencies for your web project. Expect you have to experiment a bit - this took me a while to learn.
Typically you would create an API interface using remote service beans from the Java application that expose the methods that you want to invoke in the web application. You would include a proxy of the API interface with your web application that calls the remote service bean in the Java application. Remember that you will need to register the remote bean in the web.xml file.