Showing posts with label tomcat. Show all posts
Showing posts with label tomcat. Show all posts

Sunday, 5 April 2009

Maven Eclipse Tomcat "There are no projects that can be added or removed from the server"

I started to give up my MyEclipse IDE and turn to use a simple Eclipse instead. The main reason is because I found Eclipse actually do what I want it to do, like hot deploy to tomcat. Frankly it's not as good as MyEclipse, but it does the job.

I hit the wall as soon as I start to use it. When I tried to get rid of all eclipse project files (e.g. .project and .classpath) by using mvn eclipse:clean command, I regreted 2 minutes afterward. It was because once the command mvn eclipse:eclipse finished, I switched back to Eclipse, imported the project once again and then added the project to Tomcat, Eclipse prompt me a message "There are no projects that can be added or removed from the server". No matter what I tried to do, like adding "EAR Libraries", "Web App Libraries" and even "Tomcat Server Runtime Library", it does not help a bit. And I could not find a single hint from the net to solve it.

After struggling a whole day, I looked at the eclipse maven plugin. I found the solution.

Since Eclipse is using WTP for web project, and Eclipse needs the WTP configuration in order to recognise it is a web project. If you create a dynamic web project from Eclipse, it works nicely. However, it does not work when you start your project using maven. In order to enable WTP using maven, first we have to add maven-eclipse-plugin and make sure the version of maven-eclipse-plugin is specified (2.6 is the latest version at the moment). Then we have to put a configuration property <wtpversion> and set it to either 1.0, 1.5 or 2.0. Other setting like none or R7 does not work. So here is my setting:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.6</version>
<configuration>
<buildOutputDirectory>src/main/webapp/WEB-INF/classes</buildOutputDirectory>
<downloadJavadocs>true</downloadJavadocs>
<downloadSources>true</downloadSources>
<wtpversion>2.0</wtpversion>
</configuration>
</plugin>

There are other settings like <wtpmanifest>, <wtpapplicationxml>, <manifest>. But they are not necessary in order to deploy to Tomcat. This trick also apply on other server like glassfish.

After I put the setting in, it works nice and sweet!

The environment:
Eclipse 3.4
Maven 2.0.8
maven-eclipse-plugin 2.6
Tomcat 5.5, 6.0
Glassfish v3-prelude
MacOSX 10.5

For more information: http://maven.apache.org/plugins/maven-eclipse-plugin/index.html

Monday, 12 February 2007

MyEclipse, Maven2, Tomcat5.5 Hot deploy

Thanks to my colleague who find out how to do a hot deploy under using these tools combination - MyEclipse, Maven and Tomcat. It is a problem which last long for a certain period of time.

In his solution, what he does is basically

1. create a Java Build Path Library which point to local maven repository
2. change the project web root directory to maven build structure, which is "/src/main/webapp", in .mymetadata file.
3. use maven-eclipse-plugin in your pom.xml

So next time you run "mvn eclipse:eclipse", you should be able to locate all classes from your local maven repository. And after that, whenever you press "save" in eclipse, it will automatically deploy all the classes to your tomcat webapps folder.

In here, I will give a bit more amendment.

Since the hot deploy will copy all needed jar file from your local repository to Tomcat webapps folder, sometimes you don't want some classes to be deployed to the Tomcat, for example, servlet-api.jar. It is because it is provided by Tomcat already. You don't want any conflict between your application and Tomcat. Please note that deployment in MyEclipse will copy every jar from maven local repository even if you specify "provided" in the dependency scope from your pom.xml.

Luckily, Tomcat is clever enough on not to use your servlet-api.jar. Instead, it will use it's own. However, if you are using servlet 2.4 and jsp 2.0, since you need to provide jsp-api.jar file. Tomcat will not be able to determine not to use it. Therefore, a conflict will happen, the error usually will happen like this if you are using JSTL:

org.apache.jasper.JasperException: Unable to read TLD "META-INF/c.tld".

In this case, the current solution that I have is to delete the jsp-api.jar file from your application deployed folder manually, which is in /webapps/{your app}/WEB-INF/lib

Then the problem will be solved. However, as you can see, the trouble is whenever you redeploy using the hot deploy solution above. You will need to delete the necessary file manually.

-----------------------------------------------------
-----------------------------------------------------
Further amendment:

As I mentioned above, Tomcat is NOT clever enough to skip the jsp-api.jar deployment. However, for my further investigation, I found that this symptom only happened in Tomcat 5.5.12 and 5.5.17. (I assume it happens in between these version as well). So if you are using the most updated Tomcat version, which I have updated to 5.5.23, it will work perfectly fine.

Happy programming!!!