I'm working on a project in Netbeans 8.0. Currently, the build process compiles all of the classes and assembles them into a JAR, as expected. That part works perfectly well and gives me the expected results.
What I'd like to add to the project is the ability to also produce a second JAR meant to be used as a library, containing classes that are useful for making extensions to the primary project. Since all of the classes designated to be in the library are also in the primary JAR, the problem is simplified to essentially building a second JAR that contains a subset of the first one.
The behavior I am trying to achieve is that after editing one or more source files, building the project will build both the primary (executable) JAR, as well as the secondary (library) JAR. I would prefer to avoid using a separate project.
I'm pretty sure that this involves configuring the Ant build process, but I'm having difficulty finding documentation on how to do so. If there is a good tutorial or guide on how the Ant process works and how to configure it, a link would be great! Otherwise, is there a conventional method for configuring this sort of behavior?
Thanks!
Netbeans creates Ant buildfiles for your project that contains many targets for initializing, compiling, packaging, etc. Adapting this automatically generated buildfile (which may be importing other buildfiles) in order to generate your library Jar may not be straightforward because you would have to follow the build procedure and take into account Ant properties, target dependencies, etc.
The basic idea is that in your buildfile, there should be a target that creates the normal Jar of your project. You can place the following task inside that target to create the library Jar. Assuming the subset of your Java classes that needs to be packaged as a library is under package common in the bin directory, the task would go like:
<jar destfile="${myProject}/mylib.jar" basedir="${myProject}/bin" includes="common/**"/>
Check https://ant.apache.org/manual/Tasks/jar.html for how to use jar.
You can also do this in a separate build.xml outside of Netbeans' scope but then of course you would have to manually launch this build.xml whenever you want to build the library Jar.
Related
I am not a big fan of creating fat executable jars for java programs as it involves a massive overhead when I have multiple executable programs from the same project.
I want to be able to create a single library-like jar and create corresponding .sh scripts which pretty much have the structure of:
java -cp classpath_libs main_class program_args...
or any other executable where I can customize it to my needs but in a similar fashion(ex: hadoop jar project_jar main_class classpath_libs program_args). Is this achievable in gradle? if so, how? Note, I need to create multiple scripts using different main classes from a single project.
Key requirement here is to be able to use final fully resolved classpath string.
UPDATE: I have seen examples of using the application plugin but it creates an executable jar with dependent libraries packaged into it. This is NOT what I am looking for.
So far as I have found, it appears there's no plugin that handles this directly. So, I used the java-library plugin, used the configurations.runtime classpath value and created the necessary scripts and the copy task to copy the libraries necessary into the necessary directories. For anyone interested, you may also try the distZip option in application plugin where gradle does create scripts for execution and package the necessary libraries in a distribution. You can take a look at the output of the script to see how the classpath is structured and create something similar.
I have a project with a library JAR and a set of applications. Right now the applications (all 5 of them) are in a single project. These tools each have one or two classes, they run from the command line with one exception. Even the GUI tool has only about 8 classes. I am struggling with how best to structure this in Eclipse.
I have seen that I can use maven-shade to create multiple executable JARs from one project. I would need to convert the project to Maven, but I assume that is not a big problem.
I can create a separate project for each tool. This seems like overkill to have several projects with one or two classes in each.
Additionally, both of these solutions will leave me with either a bare class file or a JAR plus the dependent library JAR. Not ideal for deployment.
Before I pick one of these, I thought I would ask here if there is a different approach that I am missing for packaging these tools.
In eclipse, Export --> Archive file, You can select/deselect what classes you want to export into your Jar File
Yes, but You could/need to create using Maven, and for each build building jar you need to set the main Class.
Checkout the documentation of Maven.
I think You could to the same with Gradle, but not sure.
https://maven.apache.org/plugins/maven-jar-plugin/examples/attached-jar.html
I am writing a Bukkit plugin in Eclipse in which I separate different functions into different packages and export each package as its own jar file.
However, I would still like to keep these packages in the same project, rather than separating them into different Eclipse projects. These plugins each have files which must be in the root of the jar file, such as plugin.yml. I have moved each jar's files into their respective packages, but these files are put into plugin.jar\com\Preston159\plugin rather than in the root of the jar file (plugin.jar\), causing the plugin not to work.
Does Eclipse have any function to make these files automatically compress into the root of the jar file even though they are contained within the package in the source, or, is this something that I could solve by using Maven? My current solution to this problem is to move the files manually after exporting the jar, but this is becoming increasingly annoying.
EDIT:
The project builder XML I ended up using to complete this task can be found here
You would need to use a Build Tool. There are several supported by Eclipse. Ant and Maven are now built-in, but there are several build tools that run directly within Eclipse, but Eclipse can also be configured to run an external build tool as well.
Do a quick search on build.xml for examples of ANT build jobs.
Unless you're specifically required to use MAVEN for continuous integration, etc. then what you want to accomplish would be easily done with ANT.
I am having trouble adding dependancies that are located in my working directory, to my .jar. When I try to run the .jar from command line I get errors saying that files I am attempting to access do not exist. Why does netbeans not include the working directory in the build dependancies, seems like a pretty easy and obvious thing to do....
I am simply trying to read a file, located at res/settings/settings.txt. I have made my working directory the res folder so that in netbeans I access that file successfully through 'settings/settings.txt'. I attempted to make the settings folder a library so it would be added to the build, however it refused to copy it because it was a directory.
How can I add this file to the build so i can run it from command line?
Why should it? The working directory is simply a "test" location for your application. The build process is not a "packaging" process.
Netbeans provides you with a build.xml file which can you can modify to perform custom actions, but consider this, each time you build your application, these custom build actions/targets could be executed. That could add considerable time to your build process. If you're just making small changes, this might not be desirable.
Two solutions then come to mind.
You could create custom build targets within the build.xml file which you would need to trigger by right-clicking the build.xml file (in the files view) and selecting Run Target and choosing the required target from the cascading menu...from experience this is not as convenient as it sounds. You could also run the build.xml file from the command line, specifying the targets you want...
Make another ant script which packaged your application separately. This is similar to the first comment except that you completely divorce the build and package processes.
I work on a very large application, which was taking upwards of 15 mins to build (completely). We wrote a utility which went through all the Netbeans project properties and built it's own dependency map, which then generated an Ant build script. We then included this in our own packaging script, so we could build the application and package it within a single pass when we wanted to create a release. This reduced the build down to something like 3 minutes (don't ask why, it just did), it also meant that we could remove the immediate dependency on Netbeans from our build process.
So. The basic answer is. Netbeans is a builder, not a packager. In order to supply this support, you're going to have to write something yourself. The easiest thing to do would be to simply write some Ant script to perform this aciton. Where you do this is up to you.
When I clean and build, Netbeans generates a .jar file for my Java project, and it uses a "lib" folder which has all of my external Jar files that I use. Can I somehow tell Netbeans to package these Jar files into the .jar it makes? So that I can run my project as a stand-alone .jar file?
I am using Netbeans 7.1.1 on Mac OSX
The short answer is no, the long answer is complicated.
Firstly, Java does not support embedded Jars (ie, you cann't simply add the depended Jars into the main Jar).
Secondly, uncompressing all the Jars and merging them into one will overwrite any resources/classes that share the same path/name. This may not be an issue in small projects but is a major issue in large ones (we have this problem with the project I'm working on at work).
One solution is to use something like One-Jar, which basically uses it's own boot class loader to all you to reference Jars inbedded within a single Jar.
I've had issues with this when using our own custom classloader, but otherwise it seems to work well.
It includes Ant support, so you can include it in your projects Ant build script, if you not using Maven, otherwise, you'll need to devise your own build process
There is no options in netbeans to do that.
There are some other options that a quick search would help, but requires manual intervention.