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
Related
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'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.
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.
I intend to extract several classes and packages from one Java project and place them into another (which will have a distributable jar). That much isn't too difficult, but of course with such a large refactoring there are consequences. Namely there are many classes in the original project that are subclasses of the classes I want to extract. What's the best method for approaching this sort of refactoring?
You can create separate projects and the main project will have dependencies for all these projects. So in your IDE you can navigate through source code easily.
When building your application, each dependency could be built into a jar and the main application will be bundled with all the dependents jars in its classpath.
Let take as example a web app using plugins, and plugins using common classes, utilities and so on stored in a project named common-plugins.
project/webapp: having dependency on plugin1, plugin2 and common-plugin
project/plugin1: having dependency on common-plugins
project/plugin2: having dependency on common-plugins
project/common-plugins: having no dependencies
When building your project, you could build the plugins and the common-plugins into jars, bundled with your web app
project/webapp.war/WEB-INF/lib/plugin1.jar
project/webapp.war/WEB-INF/lib/plugin2.jar
project/webapp.war/WEB-INF/lib/common-plugins.jar
This way in your IDE, I will take eclipse for instance, you will have a workspace having 4 projects with dependencies as described above. At build using maven, ant, ivy, or what you want, you will build the 3 projects that the webapp project depends on, then bundle the whole stuff.
So basically this is what I did:
Create a new project
Copy over the appropriate classes from the old project to a new package in the new project, reconfigure until everything builds
Test that project separately and build it in to a jar
add jar as a dependency
Delete the classes from the original project
Manually change all the imports from the old packages to the new packages
What I was really looking for was some way to automate or streamline step 6 to make sure I didn't break anything, but I'm not sure it exists beyond mass find/replace.
I have developed an Java application with RMI,JPA and H2DB, look at the screenshot is my project :
All I need is to have two JARs (client and server) with imports to the libraries described in the screenshot.
the problem is that netbeans does not generate two JARs for the same project :
this box allows you to create a single jar for the project...
is there a solution for this problem?
thans in advance.
If you want to keep your project structure, I would suggest using an ant script to build your jars. That way you can specify which source files and jar dependencies to include in your client and server jars.
Break up your project into three modules. One for shared code, one for server and one for client. Having it all together like you have will eventually result in unmaintainable code (and sooner rather than later..).