Export Java project as JAR with ressources out of it - java

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.

Related

Android: compile APK with ANT, common libraries from outside the project folder

I'm running a web service that allows users to create simple android apps. I'm using ANT to automate the building of the APKs:
Create the project folder (eclipse-style)
Copy libs (ad sdks) to the libs folder
Run ant
The problem is that even if the libs are always the same for all apps, they are pre-dexed for every single app and the build process takes much longer than needed. I understand this is the normal behavior for the setup I have now.
What I'd like to do is keep all the libs in a common folder on the server and tell ant to use those, preventing the pre-dexing process to happen for each app.
I'm not sure what settings to use in ant.properties or build.xml to make this happen.
Thanks!
You can use the lib switch to specify a library
-lib specifies a path to search for jars and classes. Or put the libs in the standard directories.
Any jars or classes in the directories of the path will be added to Ant's classloader. The order in which jars are added to the classpath is as follows:
*.-lib jars in the order specified by the -lib elements on the command line
*.jars from ${user.home}/.ant/lib (unless -nouserlib is set)
*.jars from ANT_HOME/lib
First of all, a tiny improvement; put your shared libs where you want and than, for each Android project that requires one or more of these libs, just create a symlink in the project libs/ directory which point to the desidered lib. You can save space and time, specially if the webserver is used by many users.
To avoid the multiple-dexing stuff, you may try to build a standalone .dex file of your libs and use it dinamically inside your apps using a ClassLoader.
You can start from this: http://android-developers.blogspot.in/2011/07/custom-class-loading-in-dalvik.html

.classpath file in the eclipse question

In the eclipse directory, there is .classpath file. What's the purpose of this file?
I have ant build.xml available, why Eclipse still need its own?
Eclipse has its own mechanism for building your project. The .classpath file contains information that the IDE uses to create the classpath used at build-time, runtime etc. You can directly edit this file if you want but it is created by the IDE based on the settings that you provide via the project properties dialog.
There is Ant integration within Eclipse insofar as it provides you a specific editor for build files, but it can't use any of the information in the build file for its own builders. Ant files are custom, so there is no way Eclipse could know what info to use.
The reason for this is that it doesn't matter if you have an Ant file or not. The reason for the presence of this file is that this is a Java Project, and the corresponding Project nature always generate such a file. Create a normal Project (New->Project->General->Project) and you'll see that there is no .classpath file.
In general I would recommend to split those functionalities in separate projects, that means one Java Project for developing, one non-Java-Project for executing your Ant scripts.
HTH Tom

How do I include external JARs in my own Project 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.

Avoiding duplicate library .jars when exporting a single .jar in Eclipse

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.

Automating build process for a web application created in Eclipse

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.

Categories

Resources