I have a module that is used by creating a custom class loader. The class loader should therefore be created with the path to the module and also all dependencies.
I'm looking for a way to make it productive to work with this mechanism in both dev and production environments.
I thought the build can generate two files listing all dependency paths. A 'dev' file and 'production' file (then in the code I'd choose the right file based on some system property):
dev: dependencies should be paths to thirdparty jars in the local maven repository or 'target\classes' for other modules (which will allow to compile with the IDE and not need a full build for each change)
production: all jars should be copied to a 'lib' folder and the dependencies should list them (without full path). Alternatively, create an uber jar, and then the (?)
So I'm looking for pointers to plugins & pom snippets to help me with this.
Some hints:
For the dev part, the Maven Dependency Plugin and its dependency:build-classpath can help (and supports writing the output to a file). Another option would be to use the Maven AntRun plugin, it has access to the Maven runtime classpath, it should be easy to write the property to a file.
For the prod part, I assume you'll use the Maven Assembly Plugin or the Shade Plugin or another equivalent and I wonder if you couldn't generate a classpath entry in the MANIFEST.MF and rely on it.
Related
I am trying my luck in eclipse to achieve something that I am able to successfully achieve using Visual Studio(.net project). I have a core-framework maven project with additional files and folders apart from the normal folders that maven provides. I exported it as jar file and added it as an external library to another maven project. Is there a way that when I add it as a library to the second project, the second project gets all the folders, files from the first maven project overwriting the pom file in the second maven project too? Inshort I want to make sure whoever takes the framework jar as reference follows the same folder structure as framework with required files such as config file, pom.xml file to avoid errors on missing path/files. I read about dependency management but even for that I have to define all the dependencies in child pom file which I want to avoid. Any help would be appreciated, I didn't find much info around the query.
Copying JAR files to lib folders is old-school. Such you also set aside Maven's sophisticated dependency (and transitive dependency) resolution. The clean Maven way is to put your core-framework JAR to a Maven repo (remote or local at the users machine) and let the users of it declare it as dependency in their projects.
What do you exactly mean by "additional files and folders apart from the normal folders that maven provides"? What is there more than code and resources?
To make your users' life easier concerning dependencies you could a) use a BOM (Bill Of Material) dependency or b) use inheritance (i.e. <parent> ← child relationship).
Adding a dependency will not change your project in any way. You cannot add folders from a dependency to the project.
You can write a Maven archetype that is a kind of project template. Then people can use it to create new projects where the files are at the right places.
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.
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?
As we do in Ant build, we can specify the Jars that we need to copy in build.xml in case of Ant(We just need to specify the folder name from which we need to pick the jar files). Is there any facility of same kind is available in Maven as well.
If yes, Then do we need to add the dependency tags equal to the number of jars in folder or one dependency tag is sufficient. I hope you get my point.
I think your missing the point of dependency management. All the JAR's required by your project should be defined as dependencies in your POM. If you have any custom JAR files (not available in a public repo) then you will want to install those in a local repository, and access them that way.
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.