How to copy a file in Maven only when it has changed - java

I'm developing a web application in Java, using Tomcat, Maven and Eclipse.
In order to manually test the application while I develop, I'm using Maven to copy some files to the right place in Tomcat. These files are not dependencies, just Tomcat configuration files I have in a local directory.
I'm copying them using the copy-rename-maven-plugin in the process-resources phase, so the files are in place for launching Tomcat manually, after compilation.
The problem I'm facing is that touching some files (context.xml and server.xml), even without modifying them makes tomcat restart.
The ideal solution would be to configure copy-rename-maven-plugin to only overwrite a file if it has changed (or if it's newer) but it seems that it's not possible. So far I have been able to set overwrite to no, but then if there's a change in the original file is not going to be copied because the destination file already exists.

Related

Is there a way to deploy a Maven project with "jar" packaging into Tomcat7?

My project generates a Jar as the output package and uses an external War file, available on our Artifactory, as the Web Application to be deployed on Tomcat (currently using version 7). This War file contains all libs and modules required for the application to run.
I have already packaged and ran those projects outside eclipse on a "vanilla" Tomcat installation. In this scenario, the Jar my project generates is loaded on the context.xml file this way:
<Loader className="org.apache.catalina.loader.VirtualWebappLoader" virtualClasspath="/home/igor/workspace/myapp/myapp-2.4.3.jar"/>
Is there a way I can deploy this project on Tomcat using Eclipse and still be able to debug it? Can I use the Jar generated for this purpose or do I have to deploy the workspace project?
As for the War file, do (or can) I have to add it as an dependency?
Thanks in advance!
EDIT
We actually provide an Web Framework, which is packaged as a war. Other applications that use that framework are exported as jars and loaded into the framework through the context file as cited above.
Your question is confusing probably because of your custom plugin/classloader and deployment which is sort of orthogonal to debugging.
What I recommend is you keep whatever system you have to build/package/deploy and use JVM remote debugging. That is do not use the Eclipse WTP since you seem to have custom steps for deployment but rather build your code deploy & run a separate Tomcat instance and then run the remote debugger in Eclipse.
You will get some hotcode swapping with this method but not as much as something like JRebel.. (which you could use also) it will certainly be better than constantly redeploying.

Modifying Java Paths when deploying WAR

I have a webserver that I developed in Eclipse with a local TomCat server. When developing, I often had to run scripts from the command line from my Java code. I just looked at the working directory that Java inside eclipse was working from (was in the Eclipse subdirectory somewhere) and put a ton of files and scripts in here.
Now when creating a WAR those paths are wrong. I'm just not sure how to integrate this all into my project correctly. Should I copy everything into the Eclipse project directory and make the Java 'home' path here - how can I set this up so that the WAR would work when deployed on a webserver?
Should I copy everything into the Eclipse project directory and make the Java 'home' path here - how can I set this up so that the WAR would work when deployed on a webserver?
That is a bad idea:
Since you can't change the "current directory" in a Java program, the only way to do that would be to "cd" to the Eclipse project directory and start Tomcat from there. Yuck!!
And that's conceptually wrong:
Suppose you had two WAR files created in two distinct Eclipse projects. An application cannot have two different "current" directories. It makes no sense.
What if you want to deploy to a Tomcat on a system that doesn't have the Eclipse projector available to it ...
The correct way to solve this is to put all of the requisite files into the WAR file, and then access them from your webapp codebase using either webapp relative file paths ... or via the classpath.
Yes, it will entail changing the code of your webapp, but it is the right approach.

Deploying to Glassfish via Eclipse

I have set up a project inside Eclipse which I can debug on a Glassfish (3.1) server using the Eclipse Glassfish plugin. So when I click 'Debug on server', it uploads fine and I am able to step through the code correctly etc.
The problem is that I don't know if the program is being compiled/build (to a new .war) each time I press debug. I have got an Ant script in the project (as I previously built the project via terminal) but I'm not sure if it is actually being used in Eclipse.
Is there any way to check if my ant script is being run?
Also, how does Glassfish know what resources to upload? Does it just look for any .war files in the project?
Not sure about this particular jar plugin but as far as I know here is how Eclipse handles web applications:
Eclipse automatically compiles all of the sources in the class path
Then it creates a configuration file which tells Application Server to look for webapp on your project folder and does some mapping based
on your project setup. This will not create a WAR file. Eclipse will
just map WEB-INF/classes to {projectDir}/bin, your classpath jars to
WEB-INF/lib and so on.
When launching the Application Server, eclipse will feed it the config file made above.
Actually answering your question: Eclipse will not use the Ant script you created, nor will it create a WAR of any kind. It will just use project configuration to properly map project folders to web application structure.
Again, this is how eclipse handles things by default, the plugin you're using might do something different. This is based on my experience and is not based on some kind of documentation.

Compiling newly changed files only in Maven Deploy

We use Maven to for our web application and we used to use Ant.
When deploying in ANT, you can set it to not use war files and to only copy newly files into the deploy folder. This is a tremendous time saver when working on your local machine.
In Maven, I'd like it to work the same way. Whether there is a change in the main project or one of its dependencies, I'd like it to just recompile and copy the newly changed files. Is there a way to do this?
If you "deploy" by copying files from your project directory to, say, Tomcat webapps directory - do the same in Maven by means of maven-antrun-plugin.
Otherwise, I would recommend to use either cargo-maven2-plugin or tomcat7-maven-plugin.

Eclipse(STS)/Maven integration issues

I am working on a fairly big project that uses maven for dependency management. As part of this we are using Maven profiles to build and replace certain properties files that differ between test/dev/production environments.
To perform a build I would execute a Maven:build using the correct profile and mavens reactor would then build the projects in the correct order and store the jars in the .m2 folder, eg the domain jar first, then the service jar (with the domain jar included in its jar as a dependency) etc. This leads to a war file eventually with all the correct libs required by the war to run.
When eclipse performs its default build that it performs everytime you save a file the jars are not built with any profile, just a regular build.
When I then push the final war file to the server and it is exploded when the server starts up (started and deployed through eclipse) I get in the lib folder all the jars that maven had packaged into the war file but also all the jars that eclipse had built.
eg
lib/
domain.jar (built by eclipse)
domain.SNAPSHOT.1.0.jar (built by maven)
etc
Is there any way to prevent this from happening? This has the end consequence of there being two of every property file and only the order in which they are loaded determines which is used. A real hassle as different properties are used in different environments.
I found a slightly hacky solution to this problem.
In the web projects properties -> Deployment Assembly I modified the path for the offending jar files eg domain.jar from
WEB-INF/lib/domain.jar
to
WEB-INF/autogen/domain.jar
This leads to the eclipse generated jar files (with the wrong properties files) to be deployed to a folder that won't be loaded when tomcat starts. Not a perfect solution but it allows all the nice things of eclipse auto-building like code completion and error messages in the web project if the interface of the domain changes etc while also providing the correct profile when deployed.
Leaving this here for anyone else in this situation.

Categories

Resources