I'm using the Eclipse "Export... Runnable jar file" feature to package up my Clojure+Java application for deployment.
This works great, magically including various resources and Clojure source files etc.
The one issue I have is that various libraries I have get included multiple times from the "lib" directory dependant projects, e.g. I get four versions of the Clojure jar file due to other projects on the build path that also use Clojure.
This issue is needlessly tripling the size of my .jar file!
Is there any way to easily eliminate these duplicates other than manually deleting from the generated jar?
If there is a natural dependency graph to your projects, I would change your eclipse project settings such that only one project has the jar on the build path and it exports (by export I mean from the "Order and Export" tab in the Configure Build Path dialog) that jar for other projects to see. The other projects then have that "core" project on the build path. I believe this should naturally take care of your problem.
Edit
One comment I have is that having a jar within a jar is rarely a good idea. I would either reconsider packaging it all into a single jar (unless the point of the main file of the jar is to extract its own contents into a folder) or maybe explore the possibility of using the "Extract required libraries into generated JAR" option.
Related
I have an old project. I am trying to add maven build to the project.
All the jar files in the project are present currently in the WEB-INF/lib folder.
Is there a way to add these jar files to the classpath during source compilation and then have it in the war file lib
This is one of the places where switching to Maven hurts. There are a lot of suggestions where most short-circuit something you will need later, and hurt you there. I would suggest that you
Move your jar files out of the lib folder into another project folder not meaning anything special to Maven.
For all jars that you easily recognize, make the proper Maven dependency in your pom file. This will allow Maven to download sources and javadoc if present.
For the remaining jars, you can tell Maven to install them as part of your normal build as custom dependencies. I asked the same question years back and got a very useful response at Multiple install:install-file in a single pom.xml. This will allow you to get up and running quickly.
When you have the time, locate proper replacements for your custom dependencies.
Take your time doing this. It is tedious work but it pays off quickly.
I currently have a basic Java project, that I want to deliver as an executable JAR. The program within it is based on several resource files, which must be editable by the user, or by a third-party program, which means that those files must not be embedded into the JAR archive.
I am using Eclipse to develop my project. The question is :
How to make the exportation of those files automatic, to end up with the JAR, and right next to it, a folder containing the resources for exemple (if that is possible of course) ?
Every thing I've tried or found on the net concerns resources delivered within the JAR, which avoids any modification of those resources. The ideal solution would copy the files right next to the JAR when it is exported.
Eclipse's "export executable JAR" functionality can't do this directly, it's limited to the contents of the JAR. I recommend you investigate doing this with a build tool like Gradle, Maven, or Ant, and then invoking that from Eclipse or via command-line.
I am working on a multi module Maven project. We have created a project which builds an uber jar. The unpacked jar is about 60mb which is a problem for our client.
Are there any tools we can use to remove unused .class files within the dependencies when the build is completed?
Another option would be if we could analyse what .class files are loaded when running and produce a list of these and thus remove others from the build manually.
cheers,
I'm not sure if it could remove classes as such, but proguard has some awesome results in terms of minifying class sizes. You can see from the link it reduces the Ant module by 90%
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?
I have a Java application and created a JAR file and deployed it.
The App uses external JARs such as the Log4J JAR. When creating my JAR file, how do I include all external dependent JARs into my archive?
In order to get my App working, I'm having to copy the Log4J JAR into the same directory as my own JAR which kinda defeats the purpose of the jar. Wouldn't it be more elegant to have 1 single JAR file to deploy?
If you use Eclipse, You can extract all included files into one runnable jar like this:
Right click on your project name from Package Explorer and select Export.
In Export screen, select Java -> Runnable JAR file and Next.
Fill in the Runnable JAR File Spec screen and Finish.
You can choose whether to package dependency jars as individual jar files or extract them into the generated JAR.
You could use something like One-JAR to package your Java application together with its dependency into a single executable Jar file (One-JAR uses a custom classloader to make JARs nesting possible).
You have to expand the library jars into the same place where your compiled classes go, then make a jar from that. Depending on how your build process is set up, there may be multiple ways to achieve this. It's not rocket science - a jar is just a zip archive with a META-INF directory at the root level.
Keeping JAR separate is better as it is easy to upgrade only the specific JARs to its new versions without touching any other configuration. As of your issue of having to copy each file to same location as of your JAR, you can always use Java CLASSPATH and include any JAR to your application's class path.
A JAR is not itself capable of nesting other JARs, as you discovered.
Traditionally, one would distribute a ZIP archive or other installer that would unwind the application JAR (yours) as well as any support JARs in the appropriate location for classpath access. Frequently, then, the application was invoked through a script that invoked the primary JAR and created a classpath that listed the support JARs.
As other posters have noted, you have some options to create a super-JAR if that's what you want.
You can use Maven + assembly plugin (http://maven.apache.org/plugins/maven-assembly-plugin/)
BTW, probably that's not the easiest way, if you did not work with maven.