Add resource files to classpath using daemon(jsvc) - java

I am struggeling with this for a while.
I am using Spring3.1 in a standalone env. I have resources files which I need to add into the classpath (applicationXML). In eclipse it's a known and easy way. Now I am trying to deploy my application to a standalone env on linux using Daemon (commons-daemon-1.0.3.jar).
How can I add resources files there to the classpath?

One thing you can do is use the Maven Shade plugin. This is used to create a SuperJAR of everything in your build profile — dependent JARs from Apache and Spring, as well as your own code. The Shade Plugin can that add a Classpath entry of "." into the Manifest of the SuperJAR, this is precisely how you will be able to run the Main class of the SuperJAR, but have the classloader look in both a local directory as well as a JAR for all your components.
You'll have to adjust your build a bit so that things like log4j.properties and application-context.xml, and other files you wish to have sysadmins/customers modifer after build are kept out of the resources/path — otherwise they will get baked into the build.
You might look at my source code here http://sourceforge.net/projects/jee2pctest/. The client driver code provides an excellent example of how to use the Maven Shade plugin to create a directly executable JAR with external properties files.The magic is mainly in the pom.xml file. One Caveat, I am using the Maven build tooling, if you are still on ANT using using your IDE's built-in packager, then you might have some work cut out.

Related

Make an executable JAR from a maven project

I have a question about using Apache Maven: I built a very simple Maven-based project. This works fine so far.
Now I want to make an executable JAR file from my target file. The problem here seems to be that the dependencies (external libraries) are not packaged together with my app.
I've already googled and found the maven-assembly-plugin. That actually does exactly what I want, but seems to be somewhat inflexible, since the dependencies are not automatically resolved and I can not specify file filters, etc. (or only with much effort over assembly.xml).
What I really like is the solution of spring-boot-maven-plugin, so automatically determine all linked JAR files and put in a lib folder. Now my little project is not a Spring application, so Spring Boot might not be suitable for me, right?
So what would interest me: Is there a way to get a similarly structured and executable JAR archive as spring-boot-maven-plugin builds?
Just use Maven Shade Plugin. It packages your dependencies inside your jar and you can specify the Java packages to exclude

How is in the real world deployed Maven application?

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.

Adding plain Java project as a classpath to an eclipse plugin

I have a plain Java project (not a plugin project) which I want to add to a classpath of a eclipse plugin which I am developing. But in web projects I can add that project as a build path and it works fine. But I tried same thing in eclipse plugin, I am able to compile successfully, but at run time I am getting java.lang.ClassNotFoundException.
I know OSGi quite well and I know how to add OSGi into an classpath (using export-packages) but what I want is to add Standard, non-osgi project into an classpath, so that I wont' get runtime errors. Is there anyway I can achieve this?
I can export project as a jar file or make it as a plugin project and it would work fine. But that's not my option currently because, still that API is in pre-alpha stage, and there would be lot of changes going on. So I am trying to avoid pain of exporting it as jar file everytime. Is there any option for me other than this?
I have a similar situation: I want non-OSGi Maven dependencies integrated into the classpath of my plugin. I succeeded with a roundabout solution, which I think is the best I could get.
I have a build step outside of Eclipse where I copy the class files of the dependency into the plugin's lib folder. The lib folder is specified in MANIFEST.MF as an entry in Bundle-ClassPath and (here comes the hack) as a source folder in build.properties. That was the only way to make the plugin work both when launched from within Eclipse and when exported.

package all external classes in my jar, with Eclipse

I am working on a Hadoop project in Eclipse that depends on another one of my projects; I've included the other project in my build path, but when I export the dependent project, it only contains the classes from that same project.
Ordinarily, this would not be a problem, as I could just link the other project with the -cp flag, but Hadoop requires you to pass the jar as an argument, meaning that all of my dependencies must be inside that jar.
Is there a way, in Eclipse, to automatically build and include classes from projects that you depend on?
Thanks.
You coud use Ant to automatically build, test and export. It needs some time learning it, but its worth.
There are possible tasks (fileset, zipgroupfileset, copy) to include files, jars (unzipped) or anything into the final jar. By this way you definitly know whats inside your distribution jar and you don't need an eclipe installation running.
I suggest you take a look at maven as a build tool. You define the dependencies and build steps for each of your projects in files called pom files. The maven plugins for Eclipse (the m2e plugins) can take the configuration in the pom file and setup your Eclipse build paths and project description so that you can access the classes in your other project in Eclipse. Maven can also create a jar for you that has the classes from both projects (a "jar-with-dependencies").
In maven terms, your two projects are called "artifacts" with one having a dependency on the other.
The one downside to maven (and the cause for many negative comments about maven) is an initially steep learning curve that can be frustrating. What you're trying to do, however, is very straightforward and I expect you can find a number of examples showing you exactly what you want to do.
The first step, and that's what my answer is about, is to take a look at maven. It may seem overly complex, but it can scale to handle just about any build configuration you need as your hadoop apps get more and more complex.
You can export a project as a Runnable jar, which can be useful if you want a single jar, with dependencies included.
Select the Project. File > Export. Select the Java section. Select Runnable JAR file.
See related answer:
Eclipse: How to build an executable jar with external jar?

Deploying Java Files

What are the best practices (and enabling tools) to deploy Java standalone applications along with any required jar dependencies, config files, and launch scripts?
Are there any Maven plugins that easies publishing binary releases, so that users don't need to use maven for example?
Are there any Maven plugins that easies publishing binary releases, so that users don't need to use maven for example?
Use the Maven Assembly Plugin to create a binary distribution as zip/tar.gz/tar.bz2 of your project. This plugin is extremely flexible - at the price of some complexity - and you can do almost anything you want. Then deploy (in the maven sense) the produced artifact, upload it somewhere, etc.
As for dependency, I just use maven dependency copy plugin and copy all dependencies into a ./lib folder, and supply a launch script that uses the class path wildcard (that way you can change your dependencies as much as you want and don't have to change the launch script). As for configuration files, I put it in a ./config folder and again include it in my app's classpath in the launch script (The former admittedly only works for > java 1.6).
So in the end almost all my app has the following structure:
mystuff.jar launch.sh
./lib
./config
Then I'll just zip up the whole thing and give it to my users. The whole process is probably easy to automate using maven, but I confess that I do it by hand :p
If you prefer and the licenses permit, you could also just bundle all dependencies into a single jar (with expanded dependencies inside) using the assembly plugin. This tends to make the jar bulky and giving the users an updated app. more difficult. Also I had issues with it several time because of class files getting overwritten or something so I personally stick to the ./lib folder.
There's launch4j, which, if you can get it to work, will bundle up a Java app into an executable for your platform.
If your deployment target supports RPM files, I strongly suggest you investigate the rpm-maven-plugin. It allows you to easily map your project artifacts , including dependencies, to a RPM package.
I've been using it with great success to medium-scale application deployment.
You can use Oracle's ant or maven scripts:
http://docs.oracle.com/javafx/2/deployment/jfxpub-deployment.htm
The above will not only compile your code and create jar files, but it will also create binary executable (windows exe file or Mac app file). It can also create native installers. In addition it lets you include JVM with your distribution so the end use doesn't need to install Java.
Take a look at the Appassembler Maven Plugin. You may also want to combine it with the Assembly Maven Plugin.
Use the appassembler plugin to generate a set of "programs" by specifying executable names and main classes. You can also have it prepend and create an etc directory in which you can add configuration files.
If generating the directory with the start-up scripts and directory of binary files isn't enough, you can use the assembly plugin to copy over additional files (say your configuration files) into the appropriate directory and/or package your application into an archive.

Categories

Resources