where to put jar files inside repository - java

I've got a Java project hosted on github. My project needs few custom .jar files to be imported. Since I was to be able to run the project anywhere, I want to include jar files inside the git repository. Is there a recommended, conventional place where jar files should be kept, e.g. lib dir of the root project directory?
PS
At the moment I'm not using Maven and I'm not considering it.

The usual case is actually a lib folder. Or webapp/WEB-INF/lib it is a web application.
But ths usual case is bad. I would not put jars in my source management system. If you need to add references to another project, you might consider having a look a git submodules (though you are using Github).

Related

Eclipse: how to properly integrate a Maven/Ant project into a larger one?

I have a Java project that depends on a 3-rd party component. This component is available both as a jar and as a Maven/Ant project. One option for me is to simply add the jar as a library. However, I prefer to add the source code into my project since I may need to slightly modify their source code to better suit my needs.
What's the right way to do it in Eclipse?
My main project has a simple structure: src/ and lib/. The external component also has a standard structure: src/, test/, build/, target/, pom.xml, build.xml. So do I need to copy piece by piece (like contents of one src/ into the other src/), in which case what goes where? Or do I somehow copy it all at once? Or smth else?
The best way would be if you use maven on your projet for dependency management. This way, if you have the other projects open in eclipse, your project will resolve them as local projects, but if you don't, maven will try to fetch the jars from the configured nexus repository.
This way, you avoid having to manually configure your Eclipse projects. Maven will be able to configure your project anywhere you want to build it, not having to manually configure dependency resolution.
Import both the projects into eclipse. Add the reference of 3pp jar project to your project as a reference by clicking on Add on build path option. While delivering it as output there will be a dependency to the 3pp jar project. So either deliver it as separate jar and add it to classpath while executing your project else you have to copy the entire source files into your project and deliver it test complete jar.
Making a jar will be handled by eclipse itself.

Can files outside of the Maven folder structure get compiled?

We have a Java project that was modified about 2 years ago based on the dates.
The project uses a the Play Framework which as I recalled built and ran just fine back in 2012.
The developer apparently had tried to change the project to be a Maven project but the folder structure is all over the place and not within the Maven src folder structure.
Attempting to run the Play commands does not work on building the project any longer and using the Maven commands does not compile the code.
What occurs is just the packaging of all of the folders and source code into a .jar file.
So the question is 'Can files outside of the Maven folder structure get compiled?' if so how, OR do I need to restructure all the code to be placed into the proper Maven folder structure to try and get this to work again?
Thanks for your time.
Rough view of the folder tree below: Unable to post the POM as it is on another system
Project Name
src
main/java/
trunk
Project Name
... play framework folder structure in here eclipse, modules, precompiled, tmp
conf
lib
Web Content
META-INF
WEB-INF
Yes you can compile files in a non-standard Maven folder structure. Maven natively supports multiple source directories for the purposes of generated sources.
Read the Maven use guide When You Can't Use the Conventions
Using Multiple Source Directories This occurs when you are producing a
single JAR (or other artifact), and have several source directories
with classes you want to include.
This answer shows how to edit the directory structure in Maven by specifying the appropriate properties to override from the superpom.
The Maven pom docs show the build element set mentioned in the link above.
As a side note this answer covers a non standard directory layout for building war.

How to construct maven project from source code

I have compiled sources of java web application. I know that project uses maven, because there is pom.xml files inside .war file.
I want to know is it possible to re create maven project using .war file. I use Java Decompiler to get sources, but i do not know how to combine all folders and .java files as it was in original project.
Is there any tool or howto to do it automaticaly?
Here is sources directory tree structure
For information: I do not want to stole some project or code, it's just my work. There is some web app in our production that was done by other developers in 2007. Now we are supporting this projects, and i don't know why customers do not have sources.
Create a new project from scratch as explained in Maven - Guide to Webapps. Then find the src/main/java directory and create a directory structure that reflects packages of classes in the war. Move the resources to resources folder. Use the command mvn package to recreate the war.
You can follow following steps. If your pom.xml is proper
Create a folder say, project
Copy your source code files with package structure intact to project folder( Note: if subfolder should src /project/src .
Copy pom.xml
In eclipse File->Import->select Maven-> Existing maven projects-> select the folder(project)-> Follow the instruction

Setting up a maven project for already made jars

I have some jar files that I need to include in my build - I'd rather not specify them as system dependencies - that creates a nightmare for setup. I have been uploading them to artifactory and then they can be pulled down directly, but I won't always have access to artifactory while building.
What I was thinking of doing is creating a project that has these jar files in them. It could be one per or all of them (open to suggestion). I was wondering if there is a graceful way to handle this?
What I have done (which is clearly a hack) have a project that takes the jar and during the compile phase it unpacks the jar into the target/classes directory. It then packs those class files back during the package phase. it essentially creates the same jar file again...massively hackey. Could I add the jar into the resource area or is there a different project type I could use? I am open to any ideas.
You may try to use install:install-file. I would go about it in the following way.
Create project that contains all your jars in some location
Configure install:install-file in pom of this project to install jars in repository in some early phase.
Make sure that this pom is executed before anything else that depend on it. List it as first module.

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.

Categories

Resources