How to create a jar for specific package? - java

i have a maven desktop project, and it consists of several packages, and i was wondering if it's possible to extract one of the packages as a separate jar, so i can reuse it, instead of making a new project that contains only this package ?

You could use the includes parameter in the maven jar plugin to achieve this. In fact the usage page has an example on how to do this.
You could do this within a profile so that you can continue to build the normal project with all classes.

Related

Does using Checkstyle Plugin for use within large multimodule projects require a separate project?

I went through https://maven.apache.org/plugins/maven-checkstyle-plugin/examples/multi-module-config.html to configure checkstyle plugin for multi-module projects. This requires creating a separate project just to house the resources which could be hardly 4-5 files(at least in my case). Can't I just keep those files inside a resources folder in parent-project? Will those files be available on the classpath of every inheriting child?
In simple terms, I don't want to create a separate project just to hold a few files (if this doesn't make any difference)
No you do not need to create a separate project to house the checkstyle resources. Those resources house the checkstyle rules that need to be available to the plugin when it runs. Putting the checkstyle rules into its own project, and then adding them as a dependency is just the most convenient way of accomplishing this. Of course you can have every project have its own copy of the checkstyle runs.
If you put it in the parent project then that could work. If the project is packaged as a jar.

Using Custom Packages in Multiple Projects

I've seen different packages in source code such as com.website.package These packages are used across multiple applications, and I've been trying to accomplish something similar.
The only way I know of to achieve using the same packages in different projects is to copy each source file into the new project which would create the new packages. this probably isn't the preferred method, as it could possibly drag thousands of files into the project (see Java's library).
How would this be achieved?
TL;DR: How can I use a custom written package in multiple projects without copying many files? (aka Java's libraries).
Take the class files and create a jar. Put the jar on the classpath of all the applications that need those classes.
How do you do that?
Inside an IDE, you can create multiple projects and put the shared java code in one project. Make the other projects depend on that project and you can share things within the IDE. That means you run inside the IDE and it will use the shared code.
For example, in Eclipse, you choose the project that will use the shared code/project. Right click on it in the package view or navigator view and choose "properties". Select the option to set the build path and there is a tab for selecting projects that this project depends on. Select the shared project and then it's code is callable from that project.
In this case, any change you make to the shared project will be immediately available, inside the IDE, to those projects that depend on it. Run them right then and the change will be in effect.
For running outside the IDE ...
If you are using Eclipse (or some other IDE) it will have an option for creating a jar. In Eclipse you create a java project and move the classes you want to be in the library/jar into this project. Then, once you have all the right classes, including those that others depend on, you will do some sort of build to create the jar.
Eclipse has an 'export' option on the 'file' menu. Use that and select to export as a "java" > "jar" and then select the project you just made.
You can also create a Maven project of type "jar" into which you put all those classes (as java files) and then building that project with Maven will create the jar. The "install" goal for Maven will deploy it to your local repository.
Any time your shared code is shared by way of a jar, you will have to rebuild the jar and copy it into the location(s) from which it is shared by other projects before changes inside the jar take effect.
Maybe export the package to some place on the drive and create linked folders to it in the projects?

taking care of dependencies while creating a jar file

I have created a package that is to be used by other programmers by importing in their code.
my programs use other jar files for XML parsing and I don't want others to worry about the dependencies
what is the best way to make sure that my jar files always gets its dependencies?
Should i include the dependencies in my original jar?
Is there any alternative way?
I would say cleanest solution is to use bulid scripts like using Ant or Maven. In Maven you could create a local repository with the name of mayank. Now, all your team members just need to include dependency mayank; all other dependencies will automatically be downloaded. They dont have to worry about anything else.
If you want to release your source as a zip archive, I would keep the dependencies outside the project jar. For example in a folder name lib.
I would use a build tool like Maven (http://maven.apache.org) to manage my dependencies. It's pretty easy to set up a repository like Nexus (http://www.sonatype.org/nexus) where your team members can get your jar and all the required dependencies.
Use jarjar, seems doing exactly that you want, does not force your potential users to use exactly Maven (some may use old Ant scripts or IDE features to add .jar file directly).

A jar inside another jar in Android (Java)

I have a library project that uses a jar ("httpmime-4.1.3.jar"). I'd like to take the jar generated by that project and use it in other projects but when I use it in other projects and one class of the "httpmime-4.1.3.jar" is required the app crashes with a NoClassDefFoundError.
How can I solve that without having to add the "httpmime-4.1.3.jar" to all the projects reusing the library? Thanks!
You might consider using something like maven for dependency management. With maven, you specify the libraries that your library project depends on (httpmime). Then, any project that depends on your library will automatically recognize that it needs to download and put the httpmime jar in its classpath as well, and you don't have to worry about manually copying files around.
Edit: I just saw that you're specifically looking at android development. Here is a plugin with a nice getting started guide for using maven with android.
You have to add it to the classpath. You can't include it in another jar.

Eclipse: Is there a way to create dependencies between source folders?

I have a Web Project with two source folders in Eclipse. Folder A, depends on Folder B being compiled. Is there a way to create this dependency in Eclipse, without having to create separate projects for each folder? I know projects can be set up to depend on one another, but I am not looking to do that.
The eclipse compiler automatically manages dependencies at the source level within all source folders of a project, you don't have to do anything special.
Edit: You answer "you would not be able to have Folder B reference classes in Folder A" to a comment - that's kinda the opposite of a dependency. And no, I'm pretty sure you cannot enforce that within a single eclipse project.
You can create a ant task to do that for you
You can have two programs in the same Eclipse project, with two separate main methods. You can then make two separate Run Configurations.
Before running you can then build the project. Building the project will automatically build both programs in the project, so you wont have to remember to manually build your second program.

Categories

Resources