How to add custom jar files into pom.xml - java

I am creating a maven project, in which I've two jar's for say x and y for now,which contains some helper classes for my project. I want to added these x and y jars to my project's pom.xml as dependency. As these two jar files are not available in maven repository. So I try to use these jar in my pom.xml with in repository tag.How to achieve this. I've searched in google and found one project , which is similar to my project.
when I build this it able to build application, I saw the jar file it created.But I couldn't create the same with new project. If I copy the entire pom.xml I'm able to build.What is dependency-reduced-pox.xml and how it will create. and in moven-local folder how it creates another pom.xml, which command is used to create these auto generated xml files Can any one help me to do this. Here are the screen shots of my maven project I got it .
here are other screen shot.

There are 3 ways:
A) Install your JARs to your local Maven repository and then use them in your project with provided groupId, artifactId and version: How to add local jar files to a Maven project? (this is quick & easy & pretty clean until you remove your local repository and delete your JARs accidentally).
B) Install Nexus or Artifactory (will will be then your remote Maven repository), set it up in your settings.xml, add those JARs to remove Maven repository and download them from there (this is much less error-prone, but in longer run it's worth it).
C) not recommended: Other response (btw. currently with most upvotes) from previously suggested resource: How to add local jar files to a Maven project? which contains systemPath tag. You shouldn't use it, because it will cause lots of headaches in the future (for example if you want to package your application to WAR), it's not the correct way, but it's possible.

Related

How to get folders, resources and pom file from a maven project to another maven project?

I am trying my luck in eclipse to achieve something that I am able to successfully achieve using Visual Studio(.net project). I have a core-framework maven project with additional files and folders apart from the normal folders that maven provides. I exported it as jar file and added it as an external library to another maven project. Is there a way that when I add it as a library to the second project, the second project gets all the folders, files from the first maven project overwriting the pom file in the second maven project too? Inshort I want to make sure whoever takes the framework jar as reference follows the same folder structure as framework with required files such as config file, pom.xml file to avoid errors on missing path/files. I read about dependency management but even for that I have to define all the dependencies in child pom file which I want to avoid. Any help would be appreciated, I didn't find much info around the query.
Copying JAR files to lib folders is old-school. Such you also set aside Maven's sophisticated dependency (and transitive dependency) resolution. The clean Maven way is to put your core-framework JAR to a Maven repo (remote or local at the users machine) and let the users of it declare it as dependency in their projects.
What do you exactly mean by "additional files and folders apart from the normal folders that maven provides"? What is there more than code and resources?
To make your users' life easier concerning dependencies you could a) use a BOM (Bill Of Material) dependency or b) use inheritance (i.e. <parent> ← child relationship).
Adding a dependency will not change your project in any way. You cannot add folders from a dependency to the project.
You can write a Maven archetype that is a kind of project template. Then people can use it to create new projects where the files are at the right places.

For an external jar, is it necessary to add a dependency in pom.xml file with a system path if jar already present/copied to lib folder

I have downloaded ojdbc14 jar from the internet and copied it to the lib folder of my maven project. Is it necessary to add dependency in pom.xml as well. Currently working without adding.
You can use tricks to manually upload a jar into the lib folder, but it does not make sense. And it would work cause the build process will just look for that jar into the lib and if found everything will compile nicely. BUT....
Maven is a useful tool that helps you handle dependencies, internal, external, third parties, any kind, it's one of his benefits, don't need anymore to look around for jars, and put them manually into the lib dir, but why? You would override one of the basic behaviour of Maven.
Maven set lots of rules to give you the ability to manage them the way you want, you have options about how to handle every single dependency of your project, you can point to a local jar within a dependency, you can set the scope of the dependency, the type you can exclude some of the inherited transitives, and so on...
But this is the standard approach for standard situation
You should simply define the dependency, maven will downloaded from the configured repo or the default one, maven central, and retrieved from your local repo if there are no updates on that artifacts all the other time you will build that artifact.
If you have issues with licenses for ojdbc14 then the solution is configure the oracle repo where you can easily download it.

Maven dependency for self placed jars in project level

I created the java class and converted into jar files. So, I want to use those jar files which I have placed in project level in some folder like "External Jar".
So I need to write a dependency in maven that when someone imports my project they should be able to run the program.
Basically you created your own jar and you want to publish this jar, so that when somebody else clone/use your project, this jar comes with (assuming that you have a maven project and dependency of your jar is included in pom.xml).
To achieve this, you need to publish your jar to maven , you can follow many of the online docs like http://kirang89.github.io/blog/2013/01/20/uploading-your-jar-to-maven-central/ on how to publish jar to maven central.
Edit:- As suggested by khmarbaise, please use official reference http://central.sonatype.org/ for central repository.

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.

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.

Categories

Resources