I have a Java console application, till now it was developed in Netbeans IDE. When Netbeans builds application, it creates dist directory and builds an app into this directory as a jar archive and into dist/lib copies all dependencies. This this directory could be copied into final destination and run.
Now I'm trying to transfer this project into Maven. Everything goes ok, I can compile and package my app and a jar is created into target directory. I use maven-jar-plugin to set main class in manifest and maven-shade-plugin to package all sources into one jar file.
I would like to ask you how is such Maven project deployed in the real world? Should I use all target directory, copy it ad the final destination and run as I have been used to do with Netbeans? What are consequences when I don't use maven-shade-plugin - where are all libraries defined as dependencies located? I am asking, because in my testing project these libraries don't exist in target directory.
My question - I have a Java console application "A" packaged via Maven (without maven-shade-plugin) and Linux server "S" where this application should run. Can I copy all target directory manually to server "S" or is there some better / more automatic way how is this solved in the real world?
Simply copying over the target directory will not solve your problem. I have packaged many standalone applications using Maven and I have used Maven Assembly Plugin for it. You can create a distribution archive (zip, tar.gz) using the assembly plugin which your customer can unzip and start running.
It depends on you, how you want your target application directory structure (release). I usually end up with something like
bin/
conf/
lib/
log/
The bin directory contains a shell / batch script to run your program by calling your main class, setting appropriate classpath, providing relevant memory settings etc. I prefer using classworlds (which is used by Maven) to bootstrap my application and simplify writing of start scripts.
conf directory contains configuration files for your application as well as logging configuration files like log4j etc. This directory I add on classpath to make it easier to access configuration resources at runtime.
lib directory contains all the dependency jars a well as jar file for your code.
log is where your logging configuration will point to output log files.
Note that this structure is good for standalone server like applications. Also having a bin directory and run scripts allows you to add this directory to PATH on Windows / Linux to ensure you can run the application from anywhere.
If you are packaging a command line utility, simple shaded jar may work for you. Personally, I am not the biggest fan of java -jar application.jar
The question is too broad to be answered comprehensively, but I would like to provide an example of real-world maven deployment.
There are maven plugins for all major application servers. They have defined targets for local and remote deployment. One such plugin is the jboss-as-maven plugin. You can define the deployment properties (IP, port etc.) in your .pom or directly from command line, e.g.
mvn jboss-as:deploy -Dpassword=mypassword
There is also the cargo plugin that specializes in application deployment.
Related
I have two web applications Web-main and Web-sub, and a simple java project Maven-app.
Web-sub is having dependency of Maven-app.While, Web-main is having dependency of Web-sub.
Building Maven-app results in a jar file.Then, I build Web-sub which includes Maven-app's jar file and packaged into a war file.Finally, building Web-main results in a war file which contains war file of Web-sub.
Now, if I do any changes in my java project, I need to do all these installations again so that target war of my main Web-application(i.e. Web-main) contains the updated jar.
This becomes very tedious process when I have very minor changes(e.g. typo mistakes).
One solution could be to script on shell which will make this jar directly copied to war of Web-main, but I am not able to do it.
How to do it ? Or
Can I do it in other way which will speed up my build process?
I am trying to configure JRebel
Dev environment description:
Eclipse Juno IDE
JBoss 6.0.1 - Running externally from eclipse (same machine, not remotely)
Debugging: Remote, all hotswapping functionalities off
Ant script that: compiles, deploys and lifts target WAR to the JBoss
I have not used JRebel before, the configuration guide seemed pretty easy, but i am having troubles with my particular case.
All source (incl. jsps and images) is being compiled in project's bin folder.
Ant script creates WAR which holds separately JSPs, style, property files and compiled application classes are put inside dedicated jar in /web-inf/lib/ together with libraries. The script lifts the generated WAR to the JBoss and runs the server.
Where should i put my rebel.xml (i suppose in my project's bin folder?!) and what should i write into it in order to get jrebel-hotswapped the code changes inside the jar inside the war that is deployed in JBoss server.
I provided JBoss run script with necessary jrebel VM arguments.
Many thanks in advance!
rebel.xml should end up in WEB-INF/classes of your application. If you have dependencies in WEB-INF/lib folder, then every archive should have its own dedicated rebel.xml configuration file.
See the doc
If you deploy using Eclipse WTP, then it is fine to put it into source folder. But if you are using Ant to build the WAR, then you have to make sure that your Ant script will detect rebel.xml and copy it to the correct location.
The paths in the rebel.xml should point to the folder where the compiled classes are, i.e. the location where your IDE compiles these classes to.
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.
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.
I have an eclipse Tomcat project that has several dependencies on other eclipse projects.
For example imagine the main project is called server and has several dependencies:
server
(depends on):
data-lib
server-utils
messaging-utils
Currently every time I change data-lib,server-utils,messaging-utils I have to recreate the .jar file and copy it into WEB-INF/lib directory of server. At that point I export the server as a .war and deploy to my server.
I want to make this process work from the cmd line using ant (note I know maven is out there but I know ant pretty well from past experience so would prefer sticking to it for now). Its easy enough to create the build file for server -- it will end up creating a .war file. But I want it to automatically build the dependent libs. I want to do this while preserving my development workspace in Eclipse.
What is the easiest and cleanest way to do this? Currently my thought is each individual project will have its own build.xml (i.e. data-lib/build.xml , server-utils/build.xml , etc). I will have the server/build.xml do an antcall to these individual build files and then copy the jars to the server/WEB-INF/lib directory.
Is there an easier/better way?
if you want an Ant based script, I would go with Apache Ivy. The basic idea is that each of your submodule has its own build.xml file and publishes (via Ivy) their "publications" (like a Jar file) to a repository on the file system. The other modules then import these "publications" to build the final product.
I am not sure if it can help you, but in your WAR project, right-click on it in Package Explorer, and click on Properties.
There is a "J2EE Module Dependencies". In this option, select all of the dependencies (i.e. "data-lib", "server-utils" and "messaging-utils").
Now, when you modify a Java class in one of the dependencies, Eclipse will recreate the JAR file and deploy it directly in the WEB-INF/lib of your web application.