Rename to web.xml when packaging using Eclipse - java

I have a J2EE application which has two web.xml files. One is called web.live.xml another is web.dev.xml. I am building this application with maven using profiles. So maven knows which file to choose when packaging.
I was wondering is it possible to make Eclipse use web.dev.xml when packaging my project and deploying it to Tomcat. This would be very useful because web.dev.xml sets some options which decrease start up time of the application.

You can designate the dev xml to be default (and name it web.xml). You can also have maven move (from a different folder) rather than rename.

You can use the Maven resources plugin (http://maven.apache.org/plugins/maven-resources-plugin/copy-resources-mojo.html) and configure it with a property in the profile and use that property in the configuration of the filename.

Related

How can I define a different target folder for some dependencies in Maven

I run many instances of the same web application under Tomcat and to save memory I copy some of the libraries to Tomcat's lib (tomcat\lib) and shared (tomcat\shared) folders. Libraries that go under lib can be found by both Tomcat and web applications, libraries that go under the shared-folder can only be used by web applications. My web application also need some libraries on the web application level so those libraries goes under WEB-INF\lib.
I have defined in my pom.xml that most libraries have a scope of provided so that they're not copied to WEB-INF\lib, however is there any way to define that I would like some libraries to go under a custom-defined folder; e.g. WEB-INF\lib\tomcat\shared and WEB-INF\lib\tomcat\lib, from where I could mover them manually to Tomcat's corresponding folders on the server?
That is not really the recommended way to package dependencies, as usually the web application would be deployed without furhter modification of the war file.
I think you can archieve what you want using the dependency:copy-dependencies goal, but it would require some configuration. You would have to set includeScope to provided, set the correct outputDirectory and then define the artifacts with the includeGroupIds or includeArtifactIds options.
Make a separate maven project which contains the war file and define the dependencies which should be located in the share-folder as scope: provided. Furthermore create an other separate module which has these dependencies and create an appropriate archive from it via maven-assembly-plugin...

Difference between web projects with pom.xml and web.xml

What is the difference between Java projects having pom.xml and web.xml? Can projects have both these configurations at the same time?
They're completely compatible. As a matter of fact, they perform completely unrelated tasks.
pom.xml is the configuration file for Maven projects. One of its goals is to provide assistance in the compilation and building of a project when using Maven. You can think of it as an ant build.xml file or a makefile Make file if you're not familiar to Maven (actually, it can provide a lot more functionality)
web.xml is the Java EE web application deployment descriptor, where you specify for instance servlets, servlet mappings and other aspects of a webapp.
What is Maven from the Apache Maven site.
What is web.xml file and what all things can I do with it? question on SO.
The two files have nothing to do with each other.
pom.xml - Maven configuration file. Controls the build process for
the project
web.xml - Web application configuration file. Controls the deployment
and configuration of the web application
The POM file really shouldn't be deployed with the application, its just for the build process.
web.xml is an indicator that the project is running in some kind of servlet container (possibly even a full-fledged Java EE container).
pom.xml is an indicator that the project is built using the Maven build system.
Those two things are entirely orthogonal, so any given project can have none, one or both of them.
The Pom defines any dependancy libraries, it is part of Maven. This tells maven what jar files to download and store in the lib folder of your site.
Web xml is how your web project is configured.
They can both coexist as they do different things.
POM stands for "Project Object Model". It is an XML representation of a Maven project held in a file named pom.xml. http://maven.apache.org/pom.html
yes you can have both configurations at the same time.
The pom.xml is for configure your project with Maven.
The web.xml is use in all Java EE project under Tomcat for example.
You can use both, Maven is for compile and deploy your project, Tomcat is your server.

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.

What is the Maven idiom for accessing configuration files?

For example suppose I'm using the standard project structure and have
src/main/config/config.xml
To access this I presume
new File("src/main/config/config.xml");
would be incorrect
There is no "Maven Idiom" for accessing configuration files. Maven is a build platform, not an execution platform. So the conventions for accessing configuration files that apply are really just the conventions of the Java platform that you are using; e.g.
the plain J2SE way of doing it, or
the J2EE and/or webapp way of doing it, or
the J2ME way of doing it, or
...
Maven only comes into the picture because you (presumably) have resource files in your project / version control that need to be included in the JAR or WAR or whatever artifacts that you are building. To get this to work in Maven, you simply need to understand how Maven copies non-Java files into the artifacts.
In the simple (JAR) case, the default behavior is to copy anything in src/main/resources/ into the JAR, with the same relative name; e.g. src/main/resource/foo/bar.xml becomes /foo/bar.xml in the JAR file.
For WAR files, the default is to copy anything src/main/webapp to into the WAR file. So if you wanted a file to be accessible in the webapp as a classpath resource with the name /foo/bar.xml you would put it in src/main/webapp/WEB-INF/classes/foo/bar.xml. (I'm assuming that you know how webapp classpaths work ... or that this isn't your use-case.)
A config file is just a resource on your classpath like any other, so use:
URL resource = getClass().getResource("config.xml");
You'll need to do the usual Use as Source Folder on your src/main/config folder for this to work in Eclipse with m2e.
I think config files should be in src/main/resources by default.

Java classpath for dynamic web project

Hi all i have very rare problem which needs to be solved.
Problem/issue:
I have a dynamic web project which is already built and i have war file of that project.
I need to apply some customizations on top of the war file given to me.
Using maven or ant am able to compile the custom code written by me and able to add produced class files to the war file.
But the this is happening for final war file build.
when i want to test my code in eclipse. the war file build and deployed in jboss plugin contains only the class files produced out of java files written by me..........
Please help me how can i modify the .classpath file of my project so that a jboss publish can build a war file using the dependent war file which can run on eclipse-jboss to test my custom code....
Advance Thanks.....
Not a rare problem.
What you need to combine two web applications (wars) together. You have your customization war on which you need to overlay the existing web application.
It looks like you have already solved it from build perspective and looking for Eclipse support. To my knowledge, Eclipse lacks support for this. You probably need to manually do the necessary configuration to make this happen.
It looks strange to me to have two WAR files.
Perhaps you have to consider to package your customizations in a JAR and inserting that jar in the original WAR file.
Otherwise, another solution, and what I do often with open-source project to customize is to have three projects in your workspace.
PRJ-src (with your original sources/JAR/WAR)
PRJ-custom (which depends of the previous one); This project contains only the new classes or custom spring xml files (with injection of my own classes)
PRJ (the merge of the two previous projects)
I create an Ant task in the 3rd project which takes the 1st project (PRJ-src) and merge with the 2nd project (PRJ-custom). This is possible to do so with Maven as well.
Then this is the only project I deploy in my app server (tomcat / jboss).

Categories

Resources