Automaticly add 3rd pary jars to Tomcat's lib folder - eclipse

I'm using Eclipse Neon 4.6 and Tomcat 8. I have my deployment assembly set with external jars via user libs and dependent projects. When I clean and build the project only the dependent project's jar files are added to Tomcat's WEB-INF/lib folder, not the 3rd party external jars even though they are specified in user libs and the lib is in the deployment descriptor. Any ideas as to why Tomcat isn't automatically coping the user lib jar files? One interesting note is that if I put the jar files in the project's WEB-INF/lib folder they are not picked up at run time either. I have also tried adding WEB-INF/lib to the deployment descriptor without any luck.

So I figured this. Turns out I correctly had libraries as dependencies in the project but did not mark them for export in Project | properties | Java build path | Order and Export. I had to check the libs for export. Now they are included in the WEB-INF/lib folder when I create the project WAR file.

Related

Making Eclipse Maven repository inside the project

I'm a real beginner to Maven and Spring framework in eclipse so excuse me if this sounds trivial..
I have created a MVC web project in eclipse using the STS plugin on my windows computer, but my problem is that the location of the jar and configuration files that maven is set to by default is: C:\Users\MyName.m2\repository
This is a problem for me because eventually I'm going to make a WAR file out of my project and deploy it on a Linux machine, which does not have Maven installed on it.
My question is, is it possible to take all the jar files in my Maven repository, and make a local repository on the project itself rather than on an absolute path on the machine (sort of like a lib folder within the WEB-INF in the project that will hold all the maven jars and that maven will direct itself to it), and thus making the transition to the Linux machine smooth
I understand there is this classpath variable named M2_REPO, but doesn't seem to be editable or accept relative path rather than absolute.
Thanks in advance
Since you are using maven and you are adding your dependencies on your pom.xml, then all the dependencies with a scope of "compile" will be added to your final war file created by the maven war plugin under the WEB-INF/lib folder, this means that you should not worried about having maven in your linux server, since the war file that you are going to deploy will contain all the necessary dependencies. If you want to confirm this, just create a package of your project using "mvn clean package" and check the "target" folder generated by maven, you will see a "war" file, and you can unzip your file to check what contains.

How to include JAR to Liferay portlet in Eclipse

How to import jsoup.jar to Liferay portlet, through Eclipse IDE?
just copy your jar file into docroot/WEB-INF/lib folder of your portlet. if you create your portlet correctly using eclipse and Liferay IDE(plugin), eclipse automatically recognize your lib folder and jar files under it into classpath
liferay has ivy settings. you could just locate the ivy.xml file in your liferay-plugin-sdk-6.2 folder (this normally reflects in eclipse as a project folder with your version). add this line <dependency org="org.jsoup" name="jsoup" rev="1.8.1"/>
and then build "all". You'll see it download the dependency. When done, navigate to liferay-plugins-sdk-6.2/.ivy folder, look up the downloaded jar and copy it to your project's lib folder i.e /lib. Go to project properties, then Java Build Path. In the libraries tab, click Add JARs and navigate to /lib and add it.

Adding jars to Tomcat classpath

I have an Eclipse project running on Tomcat, with my dependency jars sitting in the WEB-INF/lib directory. Everything is great. But the war file created on export is huge, and I'm going to need to send it back and forth to the test server, which will be time consuming. It seems like a waste.
So I put all my dependency jars in a folder, dependency_jars, in my file system, and configured the build to point there. The build works, but when I run it (still on my local machine) the app is throwing runtime errors.
Reading around, I went to the project properties and added the jars to the Web Deployment Assembly. Now the runtime works, but the exported war once again has all the jars packaged with it.
So how do I have my dependencies available for the runtime environment, without having Eclipse package them inside the war?
OK I'm not sure this is the most elegant solution but what I did is configure Tomcat to use an external library, put the dependency jars there, and then let eclipse create the war with everything in it, explode it from the command line, remove the dependency jars, repackage the war, and finally send it out.
Well, definitely not an elegant solution. But it works. Here are the steps:
Configure Tomcat to use an external resources library. Go to tomcat_home/conf/catalina.properties and add your directory under the comment that starts with "List of comma-separated paths defining the contents of the "common" classloader..."
Export war through Eclipse.
Explode the war (pun probably merited here:) jar -xvf MyWar.war
Remove your dependency jars (which are now in the external directory from step 1 above.)
Repackage the war: jar cvf MyWar.war WEB-INF

Can I add predefined jars to WEB-INF/lib in Eclipse project?

When I am designing normal Java project in Eclipse, I can add predefined libraries to it's Build Path. Can I do the same way when putting jars into WEB-INF/lib folder of Web project? I.e. can I ask Eclipse to put some predefined library jars there?
Just copy / import a Jar there; it will be added to the build path automatically. You will find it in the package explorer within the Web App Libraries (or in the build path in the project properties). The project has to have the Dynamic Web App nature.
This is what I did. I copied the jar files into my WEB-INF/lib folder. Then, I added these jars into my build path using: Properties -> Java Build Path -> Add Jars -> Select the jars from the WEB-INF/lib folder.

ClassNotFoundException when using User Libraries in Eclipse build path

I'm using Eclipse 3.7 (STS) with Tomcat 7 running inside the IDE. I've created a new Dynamic Web project and added a single JSP file to the web content root folder. I can run Tomcat and access the JSP from within Eclipse with no problems.
I've added a few 3rd party JAR's to the project from User Libraries (I'm not using maven or auto dependecies managment). In the JSP I reference a class from the project's JAR file, I can compile this with no problem, but when I deploy on Tomcat the JSP throws ClassNotFoundException. Clearly, Tomcat can't find the JAR's from my library settings. I tried creating a Run As configuration for Tomcat Server and I set the classpath to match the classpath settings of the project, but I still get the same classnotfound problem.
I could get around the issue by manually copying all project JARs to the WEB-INF/lib directory so the webapp can find all dependencies, but that's absurd and I don't expect that to be the solution since it's a maintenance nightmare.
Am I missing something?
In project's properties, go to Deployment Assembly. Add there the buildpath entries as well which you've manually added as user libraries. It'll end up in /WEB-INF/lib of the deployed WAR.
You'll need to copy the jar files to the WEB-INF/lib folder: that is where they are supposed to be.
Eclipse should offer you the option of generating a WAR file that includes all the dependencies: I haven't used Web Tools for a good while but one way or another all dependencies have to be in WEB-INF/lib or the class loader won't be able to find them.