Maven + Tomcat acceleration - java

I am writing a web application with Maven in the Eclipse IDE, and use Tomcat servlet container.
So, I run Maven like this: mvn clean compile. It is reasonable that after this operation I must re-run Tomcat so it can reinitialize the context (Sysdeo Tomcat launcher helps a lot).
The problem is Maven execution and subsequent Tomcat re-running takes noticable amount of time (like 10+ seconds for Maven and 20+ sec. for Tomcat, because of logging, O/R mappings, etc.) every time I do it.
Is there any automated and more faster solution for these operations? As I see it, a way better solution can be moving re-compiled classes only to the target dir.

Is there any automated and more faster solution for these operations? As I see it, a way better solution can be moving re-compiled classes only to the target dir.
Well, the question is why do you run clean each time? Doing incremental compilation would already speed up things a lot.
Update: I agree with #Carl about Eclipse WTP that provides very good support of Tomcat (I don't really see the added value of the Sysdeo plugin nowadays). Using Eclipse WTP for development and running Maven before to commit the changes to check that you didn't break the continuous build is a very typical workflow. And both the maven-eclipse-plugin and m2eclipse (the two alternatives for Maven and Eclipse integration) support the WTP i.e. can get your project recognized as a dynamic project than can be Run on a Server.

You may want to have a look at JRebel. It reloads your classes in a running tomcat, so your changes are near instantaneous. I haven't used it much, but it appears to solicit good comments.

Maven does two things: Dependency handling and build management. I usually find Maven's dependency management a big time-wasting annoyance that I usually don't need, so I do my build management with ant.
At the price of a hand-tuned build file, ant gives you very good control over which files go where when. If you copy newly compiled classes to your WEB-INF/classes directory and touch web.xml to trigger a reload, you don't have to stop and restart Tomcat. This brings my compile/reload time down to around one second.
This is how I prefer to work. Some Maven fans will disagree violently.
EDIT: That said, there's another method that allows me to skirt the build issue completely: I develop in Eclipse using the WTP functionality that's included with the Java EE developer's edition. When I make a code change, I simply hit Ctrl-S to save the changed file and Eclipse automatically copies the newly compiled class into the running Tomcat, so I can then immediately refresh my browser and see the newly changed Web app running. Thanks to Eclipse's incremental compilation, this method probably is probably unbeatable in terms of edit/run cycle time. Of course if you really need Maven then this is not an alternative.

There is Maven tomcat plugin can help you, you just execute "mvn tomcat:redeploy", and maven compile the source, package it and deploy it to your configured tomcat, see tomcat plugin for more information.

Eventually, I've solved that by using Eclipse feature called «Build Automatically» (Project → Build Automatically checkbox).
Every time you save a resource, Eclipse compiles it and moves .class file to the output folder.

Related

How to work efficiently with large Maven-project workspace in Eclipse?

I'm working on a large multi-module Maven-based system, with some 20 sub-modules and an additional small number of project-external dependencies that are also Maven projects in my Eclipse workspace. All in all, there are some 30 projects in the Eclipse workspace.
The projects are all imported maven projects with m2e, and we use Subversion (with Subversive plugin) for source control. Eclipse is a recently updated Kepler.
My workstation is quite capable, with 4-core Intel i7 CPU, 16 Gb RAM, and a solid-state disk.
The problem is that Eclipse is very slow when it comes to:
Team synchronize and conflict resolution. Each time I fix a conflict
and "Mark as merged", I have to wait 10-30 seconds for Eclipse to
refresh workspace etc. Not to mention that the conflict-mode tree
view then clears entirely, so I have to select outgoing mode then
conflict mode again to re-populate. Twice - because it clears when I
save the resolved source file, and then again when I mark as merged.
Building. I use Maven launch configurations to do Maven builds. But
for some reason, doing the Maven build is not enough for Eclipse -
it then has to do its own build too of the entire workspace, which
takes at least as long, and much longer when Eclipse decides to hang
a few minutes on 'Clearing output folder of project xxx', for
several of the projects.
The result of all this is that often it takes over an hour to update from source control, resolve conflicts, build and be ready to run or continue development.
Me and my co-workers often feel that Eclipse is hampering development, when it should be enhancing it.
Is there anything that can be done to reduce the eternal waiting-for-Eclipse syndrome?
We have this same problem.... 72 projects in our workspace normally makes using m2e impossible. We're trying to not use m2e and start maven executable as an external tool with some configurations. And you can make maven generate your .project and .classpath to change the eclipse configurations:
mvn eclipse:eclipse
Not the best option, but is what we have now.
Some good tips I'd suggest are:
Before you start importing large projects, go to Projects and uncheck Build Automatically
Import projects in the smallest sub-components you can. If the project is Mavenized, this means selecting the lowest pom.xml in the tree to import each time, rather than importing a large parent POM and all it's sub-projects.
When you are not working on (as in, modifying) the source code of a specific project, close it by right-clicking and going to Close Project. This will release resources for Eclipse to use elsewhere. If you still need to see the source code of something without modifying it (for debugging), you can add source containers as you need
Use different workspaces for different projects. Eventually it's quicker to have 2 working, fast versions of Eclipse than one huge slow one
If you can't split a project, you can also try to give Eclipse more memory, and make sure you're using JVM 8, for example
-Dosgi.requiredJavaVersion=1.8 -Xms512m -Xmx2g
It sounds like you're just dealing with something that's too large for most any IDE, but that may not be the case. If all else fails, why not try IntelliJ to see if that works better for you?
Guys I had the same problem but instead of using IDE's for brute force build can you analyze the design of those applications properly? I'm telling that because there are applications which don't have to be entirely build, you can deploy it separately and analyze it in modules. Most of times, there aren't reasons for keeping together thousands and thousands of classes, build it and keeping it together... the IDE's like Eclipse wasn't designed for such tasks but for local developments and the servers do the integration build, not the workstations.
Probably your design or your philosophy of continuous build are somewhat misunderstood.
Cya.

Publish for tomcat:run on Maven

I am considering using Maven 3 for my Spring projects which I have been developing using Eclipse and Tomcat. Until now;
I have been disabling "Republish automatically" because sometimes I don't need publish, I only save .java files (classes) and keep development on debug mode.
I republish (by clicking Eclipse's "publish" button on Servers view) only when I changed js, jsp or htm-like files, not class files, so I can keep developing without restarting Tomcat.
Now I am going to use Maven for debug/run on development but whenever I changed my code, I don't know how to do this "publish" issues on Maven as it doesn't use Eclipse's Tomcat directly. I stop maven and start again. Do I have to do this for all changes on my code? How can I make this maven -tomcat:run- "publish/republish" for js/jsp/html files and "do nothing" for .java files?
Unfortunately, yes, you'll have to run maven for every change.
Maven't isn't really intended to be used this way - it assumes that you're going to use maven when you're ready to build (ie after development), but use something like Eclipse if you're trying to see your changes in real time. It has no mechanism for listening for changes.
For my webapps I use both maven and Eclipse, with the m2e and the 'Maven Integration for Eclipse WTP' plugins. With that setup I can see my changes in real time using an embedded Tomcat instance in Eclipse, and when I'm done, I use maven to build.
Give those two plugins a shot - I think it'll meet your needs.
I had the same problem. And it happens because I ran maven eclipse:eclipse. Then, Server stop publishing (click on publish and said it was synchronized). I downloaded again .classpath and .project and other innerit files of eclipse project structure from cvs and server starts publishing again. May be this could help.
Regards,
For the benefit of Googlers:
I was getting NoClassDefFound errors using tomcat:run to start the app.
It took some googling but the following post notes that using tomcat:run-war enables your dependencies to get picked up from the WAR:
http://www.hascode.com/2010/06/java-server-facesjsf-2-tutorial-step-1-project-setup-maven-and-the-first-facelet/
However, for debugging and hot code replace, it's simplest to run Tomcat from the Servers view in Eclipse.

Packaging JVM with Application While Maintaining an Automated and Repeatable Build Process

I'm currently working on a project that requires us to package a JRE with our application. I'm normally against this as it makes keeping the JRE patched quite difficult, but in this case it is necessary.
What are the best practices for packaging a JRE with an application
as part of an automated build process?
Where do you normally store the JRE files so that they can be picked
up by your build process? Shared file server? What about making it
an artifact in your maven repo?
Just trying to get a feel for what people do in this situation.
I currently do this for a desktop app I distribute. I just have the JRE on the build server (which is really just some custom perl scripts and a web server.), in a folder, ant copies it to be part of the build tree which comes out of subversion, and then everything gets consumed by Nullsoft and builds the installer. It's not great, but it works. I should also say, I at one time used to check it, and I'm happier with what I do now.
Most applications keep jre in the root installation folder and the startup scripts would then use relative paths to use that jre. For e.g. Jprofiler
You can use Maven repo for jre.

Why do I need Maven if I use Eclipse?

I have seen that if I right click on a project in Eclipse and choose to run it on a server, then I can see output which means the project is running.
If everything is working fine without Maven, what's the point of using it. How is it different than simply running it via eclipse?
Maven is a build tool (build manager, in fact), similar to ANT. The main job of any build tool is configure the project, compile using required projects and do the final packaging. A build script in your project gives a blue-print of project's deliverable structure. This frees you from any configurable dependencies on specific IDE like Eclipse. All you need to know is the standard command to perform the build and you can build your code almost anywhere.
Now, back to your question, why wouldn't do it in Eclipse?
For a simple project and small team Maven is an overkill. You can easily communicate the configuration, IDE to use, and instruct any special steps to be taken. In big projects, however, there exits lots of loosely coupled dependencies. To start with, there will be different settings for developer machine build, test build and production build. There are requirements to run automated test, integration tests, store the build package (artifact) to a commonly accessible repository, update versions of various modules.
Obviously, if all the steps mentioned above is done manually there are chances of missing a step. Moreover, the manual process is time consuming.
Ideally, you should prefer a tool which fits the best for you. If you think that you're able to achieve what you required without Maven, it makes sense to not to use Maven/build-tool just because everyone uses it.
It is suggested to study automated deployment, this will give you bigger picture on what all the stuffs that you can do with build tools. And if you do not feel that it adds any value to your current process, you probably don't need Maven or any other build tool right now.
Your question does not make much sense. Do you expect your users to access your application from eclipse? If so that is a very strange set up in my opinion.
Perhaps your question should be about how to build your project. Maven provides you a way to centralize dependency libraries across the enterprise. It lets you automate your build process (most likely in conjunction with a CI server like hudson, cruise control, etc). It lets you automate your unit testing. Maven makes the packaging of app very easy to do. A developer does not have to follow arcane set of steps to package an application. You add the right plugin and maven takes care of it as part of the build life cycle. All of this magic can happen because of the principle of convention over configuration. There are many more benefits, I just named a few.
Maven is not replacing how you run the app, rather how you package the app, automate that process, and manage the dependencies of your app.
Some links on why someone should use maven:-
Why maven ? What are the benefits?
why I use Maven
Why you should use Maven
Use Maven

Best way to automatically check out and compile Eclipse projects with Ant in Hudson or another CI tool?

We have several products which have a lot of shared code and which must be maintained several versions back.
To handle this we use a lot of Eclipse projects, some contain library jars, and some contain shared source code (in several projects to avoid getting a giant heap with numerous dependencies while being able to compile everything from scratch to ensure that source and binaries are consistent). We manage those with projectSet.psf's as these can directly pull all projects out from CVS and leave a fully prepared workspace. We do not do ant builds directly or use maven.
We now want to be able to put all these projects and their various versions in a Continous Integration tool - I like Hudson but this is just a matter of taste - which essentially means that we need to get an automatic way to check out the projects to a fresh workspace, and compile the source folders as described in the project-files in each project. Hudson does not provide such an approach to build a project, so I have been considering what the best way to approach this would be.
Ideas have been
Find or write an ant plugin/converter that understands projectSet.psf's and map to cvs-checkout and compile tags.
Create the build.xml files from within Eclipse and use those. I tried this, and found the result to be verbose and with absolute locations which is not good with automatic tools putting files where they want to.
Write a Hudson plugin which understands projectSet.psf's to derive a configuration and build it.
Just bite the bullet and manually create and update the CI configuration whenever stuff breaks - I don't like this :)
I'd really like to hear about other peoples experiences so I can decide how to approach this.
Edit: Another option might be using a CI which knows better about Eclipse projects and/or project sets. We are not religious - this is just a matter of getting stuff running without having to do everything ourselves. Would Cruise Control be a better option perhaps? Others?
Edit: Found that ant4eclipse has a "Team Project Set" facility. http://ant4eclipse.sourceforge.net/
Edit: Used the ant4eclipse and ant-contrib ant extensions to build a complete workspace as a sjgned runnable jar file similar to the Runnable Jar facility in Eclipse 3.5M6. I am still depending on Eclipse to create the initial empty workspace, and extract the ProjectSet, so that is the next hurdle.
Edit: Ended up with a dual configuration, namely that Hudson extracts the same set of modules as listed in the ProjectSet.pdf file from CVS (which needs to have the same tag) causing them to be located next to each other. Then ant4eclipse works well with the projectSet.psf file embedded in the main module. Caveat: Module list in Hudson must be manually updated, and it appears that a manual workspace cleanup is needed afterwards to let Hudson "discover" that there is more projects now than earlier. This has now worked well for us for a couple of months, but it was quite tedious to get everything working inside the ant file.
Edit: The "Use Team Projects" with ant4eclipse and a Ctrl-A, Ctrl-C in Project Panel with a Ctrl-V in the CVS projects in Hudson has turned out to work well enough for us to live with (for mature projects this is very rarely changed). I am awaiting the release of ant4eclipse 1.0 - http://www.ant4eclipse.org/, currently in milestone 2 - to see how much homegrown functionality can be replaced with ant4eclipse things.
Edit: ant4eclipse is as of 20100609 in M4 so the schedule at http://www.ant4eclipse.org/node?page=1 is slipping somewhat.
Edit: My conclusion after using our ant4eclipse approach for a longer period is that the build script get very gnarly and is hard to maintain. Also the Team ProjectSet facility (which ant4eclipse use to locate the projects) which works well for CVS based repositories, but not after we migrated to git (which is a big thing in itself). New projects will most likely be based on maven, as this has good support in Jenkins.
I'm not completely sure I understand the problem, but it sounds like the root issue is that you have many projects, some of which are dependent on others. Some of the projects that are closer to the "leaf" of the dependency tree need to be able to use "stable" (or previously "released") versions of the more "core" projects.
I solve exactly this problem using Hudson, ant, and ivy. I follow a pattern demonstrated by Clark in Pragmatic Project Automation (he doesn't demonstrate the dependency problems and solutions, and he uses CruiseControl rather than hudson.)
I have a hand-written ant build file (we call it "cc-build.xml", because of our CruiseControl roots.) This file is responsible for refreshing the working space for the project from the CM repository and labeling the contents for future reference. It then hands off control to another hand-written ant build file (build.xml) that is provided by each project's developers. This project is responsible for the traditional build steps (compile, packaging, etc.) It is required to spit out the installable artifacts, unit test reports, etc, to the Hudson artifacts directory. It is my experience that automatically generated build files (by Eclipse or other similar IDE's) will never get close to getting this sufficiently robust for use in a CI scenario.
Additionally, it uses ivy to resolve its own dependencies. Ivy supports precisely-specified dependency versions (e.g. "use version 1.1") and it supports "fuzzy versions" (e.g. "use version 1.1+" or "use the latest version in integration status.") Our projects typically start out specifying a very "fuzzy" version for internal projects under ongoing development, and as they get close to a release point, they "freeze" the dependency version so that stuff stops moving underneath them.
The non-leaf projects (projects that are dependents for other projects) also use ivy to publish their artifacts to our internal ivy repository. That repository keeps all past builds of the dependents, so that any project can always depend on any other previous version.
Lastly, each project in Hudson is configured to have a build trigger that causes a rebuild when any of its dependent projects successfully build. This causes them to get built again with the (possibly) new ivy dependent version.
It is worth noting that once you get this up and running, consistent automated "labeling" or "tagging" of an automated build's inputs is going to be critical for you - otherwise troubleshooting post-build problems is going to result in having to untangle a hornet's nest to find the original source.
Getting all of this setup for our environment took quite a bit of effort (primarily in setting up the ivy repository and ant build files,) but it has paid for itself many times over in saved headaches in manually managing the dependencies and decreased troubleshooting effort.
Write a Hudson plugin which
understands projectSet.psf's to derive
a configuration and build it.
That seems like the winning answer to me.
I work with CruiseControl rather than Hudson but in my experience if you can create a plugin that solves your problem it will quickly payoff. And it is generally pretty easy to write a plugin that is custom fit for your solution as opposed to one that needs to work for everyone in a similar situation.
I have tried both Cruise Control (CC) and Hudson for our CI solution. We (as a company) decided on Hudson. But for your question "Does CC support Eclipse project build" the answer is no as far as I know. CC supports many more different build tools and Source Control systems but it is a bit more difficult to configure and use. As for Hudson, it is more simple to configure and use it. We developed our custom plugins for both CC and Hudson for the parts of our build cycle that they do not provide as is. As for plugin development, if you know / use Maven, Hudson is simpler too. But if you are not familiar to Maven, first you need to learn the basic usage of maven to successfully develop a Hudson plugin. But once you understand the basic usage of maven, plugin development, test and even debug is simpler in Hudson.
For your specific problem, I can think of a solution that makes use of Eclipse plugins as well. You can develop your own Eclipse plugin that for instance gets the psf files from a (configurable) folder, and use Eclipse internals to process these psf's. I mean you can use existing Eclipse source codes that takes a psf file, check-outs it's project definitions and compile these projects. This Eclipse plugin of yours may have a preference page (which you can access by Eclipse -> Window -> Preferences) and configure which folder it will use to look for psf files. Your Eclipse plugin should also have a way to start psf processing without user interaction. For this, you can use ipc to trigger your process. I mean your Eclipse plugin can listen for a port, and you can write another java application that will connect to your plugin through this port and trigger its process. As for CI part, you can use either CC or Hudson and use their external process execution support. If you are using Windows, you can write a bat file (for Linux sh file) that first launchs Eclipse that has your plugin installed. Then it launches your java application that will communicate with your Eclipse plugin to trigger your process. From your CI tool you will need to run your bat / sh file to trigger your process.

Categories

Resources