Maven dependency for self placed jars in project level - java

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.

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.

How to add custom jar files into pom.xml

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.

How to include a jar file that has dependent jars with it as a dependency itself in an another project?

I just want to know how to include a jar file that has all dependent jars with it, as a dependency itself of an another project. I have tried export as runnable jar option and though it does work when I run the project as standalone, however I get noclassdeffound errors when I include the jar itself as a dependency for another project. To summarize suppose I have project A which depends upon some external jars a.dep1 and a.dep2 I include them in the jar by exporting the project A as a runnable jar file. Now I wish to use project A itself as a dependency in project B and for that purpose I include the jar of project A in my project B. But when trying to run I get the noclassdeffound errors. I don't want to use maven plugins. Is this possible?
for such cases you should be using maven
then you need to create a fat jar.
a fat jar will include all the dependencies it needs inside .
such a jar can be created using the assembly plugin you can see an example here:
assembly plugin
in general if you are using maven you do not have to do this as maven will bring all the dependencies your jar needs based on the pom file of your first jar.
A jar is just a collection of files; you have free rein on what you want to include in it via the command line. https://docs.oracle.com/javase/tutorial/deployment/jar/basicsindex.html should teach you what you need to know

Add library into jar of project

My problem is with a project where I use the POI library. It's from apache and allows you to work with excel,word, ... .
I had to add some jar files to my library and it runs perfect.
The problem is when I put my JAR file somewhere else.
It gives me the errors it can't find the librarys.
Is there a way I can put my librarys into the 1 jar?
I already tried to add my lib folder as source package but that didn't help.
there is a Maven plugin for Eclipse called M2Eclipse, which will read a POM and construct a classpath out of jars it finds in the local repository and any remote repositories you've configured. It behaves largely like Maven does in terms of finding the latest version for a given jar (if you've specified a version range in your POM).
You can also have a look on this
http://fredpuls.com/site/softwaredevelopment/java/deploy/five_strategies_for_managing_j.htm
You want to make a 'shaded' or 'uber' jar, which has all of its dependencies included
There is a maven plugin for building a shaded jar.
https://maven.apache.org/plugins/maven-shade-plugin/
For me the fastest/ easiest solution was to just open my project in eclips and export is a runable JAR then it adds the librarys to the JAR.
NetBeans builds your application into the project's dist folder. There is also a readme file in it, that tells you, you should distribute the contents of that folder...
Edit
Remove the lib folder, that is shown on the second image, from your sources.

Javadoc for a New java maven project in eclipse

I created a new Maven project and generated Javadocs through eclipse. It created a lot of html files in the project directory inside a folder called "doc".
Now I want to distribute this project as a dependency to my colleague and he does not have the source of the project I created. He is only adding my project as a dependency. He says he cannot see the javadoc when he hovers on a method that I created in my project.
What am I missing here?
Add the maven-javadoc-plugin to the project (plugins section of your pom.xml), instead of generating the Javadocs through Eclipse. Please have a look at the jar goal. If done correctly, the Maven build will package a javadoc jar beside the normal jar file. The build will result in following jars:
${artifactId}-${version}.jar
${artifactId}-${version}-javadoc.jar
If you want to provide the sources additionally, the maven-source-plugin (goal jar) will do the job for you.
The installation of the jar file, the javadoc file and in case the source file into your colleague's local Maven repository can be done with the maven-install-plugin.

Categories

Resources