Maven Project Repository of Jar libraries - java

I'm trying to add Maven to already existing java project. It's been quite an adventure. I've read many questions here but I don't quite get it. The app I'm building is kinda modular so I'm loading many classes from URL so that the need of replacing the .exe (wrapped jar) file is at its minimum (Cuz once opened by any user I'cant update - replace the app (.exe)).
Since I'm using wide variety of libraries and the app Jar, hense the .exe, became too large - ~80MB, and slow to open from fileshare. Hense the need of Maven (or so I think). Since I installed Maven I had to add package statement at the top of every class, because before then there was not such thing, nor the need for it. Maven found the dependencies online and for the most part the process was not so time consuming, but the goal is to load the libraries from the project(app) folder /lib/. There I have all the libraries in jar files that I need. My question is: Is there a need of installing the libraries to local m2 directory (as I've read) since the app should always load them from the app folder? How do I configure the POM so the project/lib/ directory is recognized as a repo (or even is this the right approach), and load the libraries from the relative path of the project folder?
Thanks in advance to anyone able and willing to help!

Maven's core objective is to manage dependencies. You can add a dependency to your project, by configuring it in pom.xml, maven will download the dependency from the maven central repository & when building the project, maven can help you create a runnable JAR that have all your dependencies & code compiled.
Let's say in your current project if all of the dependencies are absolutely necessary, then even after migrating the project to maven, you're runnable JAR will remain the same size. There won't be any reduction in your application size. However, I would highly recommend migrating your project to any build tool like gradle or maven to manage project dependencies.

Related

how to get all dependencies of a library?

I want to use the Google Firebase Messaging library in my Android Project. But my laptop just works offline and has not any access to the internet. I want to download FirebsaeMessaging library with all it's dependencies then add those files as aar library files to my project. First I want to get list of all dependencies of that library. How can i do this without internet? Thanks.
There are two approaches in general:
You download the complete dependencies with your online system, copy them into a libs folder and then create a project where you dependent on every jar in that libs folder.
You create a maven based project, build the project so that every dependency is copied to your local repository and copy the .m2 (local repository) to your offline machine. In case you want a gradle based project, #lance-java already posted a solution to that.
The first approach has the advantage that you have an isolated project with no dependency to the internet. Everything is in your project and explicit. You would have to check in your dependencies into your source control system like in good old times.
The second approach is good, if you are working in a team on that project. The export of your dependencies hat nothing to do with the project setup itself. Every programmer would find a "normal" project setup and dependency management.
If guess, that you want to go for the 1. way. If so, I would suggest to use ivy to download the complete dependency tree. Put the ivy-X.Y.Z.jar into your project beside a script that lists all dependencies - some sort of poor mans dependency management.
Example:
$ java -jar ivy-2.5.0.jar -dependency com.google.firebase firebase-admin 6.8.1 -retrieve "libs/[artifact](-[classifier]).[ext]"
This command would download the complete dependency tree for com.google.firebase:firebase-admin:6.8.1 and store the jars without version into the libs folder.
You could use my dependency-export plugin to export dependencies to a directory using maven directory conventions. You can then use the directory as a local repository for building offline.
As of Gradle version 6.1 the dependency cache is portable meaning that you can copy the dependency cache from one machine to another and the second machine should be able build offline. Perhaps this is what you want?
See this issue

Include external jars in ant

I am new to writing build files and currently I am writing the ant for my project. The issue I am facing is to include the jar to in ant build file.
As per the standard 'ant build' the jars need to be kept in lib folder. But the issue is the jar is very huge, more than 100 GB of size and hence cannot be kept in GitHub.
I have put that in another repository and want to include that in my build file.
Could anyone please let me know how to include the jars in my 'pathelement location' from the url.
Apache ivy is a 3rd party dependency manager, a powerful feature built into more modern Java build tools like Maven and Gradle.
The following answer gives a detailed example, using ivy to manage classpaths and help in the creation of an executable jar.
Class not found with Ant, Ivy and JUnit - error in build.xml?
You mentioned the use of another repository. Presumably this is a Maven repository manager like Nexus or Artifactory. Ivy is capable of downloading from these. (which would be another question :-))

What is the difference between a project dependency and other dependencies

I'm working on another issue with getting my build.gradle to understand a workspace project dependency in my Android project. I've been using Java and Android for a while and I'm still easily confused about all the different ways to add a dependency. One way is to put a .jar file in the libs/ folder. Another way is to have a workspace project dependency. Another way (if working with Android) is to add a project dependency in the Android options menu? I feel like if I truly understood why there are these different dependencies and how they work maybe I wouldn't constantly have issues with managing my dependencies (even with Gradle). Why would one want a project dependency instead of a normal .jar in the libs/ folder? I'm not even sure how a project dependency itself works. By project dependency, I mean something like the Google Play Services project that I have to import into my Eclipse workspace. When importing into Eclipse it doesn't copy the actual directory into the workspace but it's more like a link to the directory. I know this is a big topic, maybe someone can show me a concise place to gain information.
Speaking in general terms, if you add a jar to lib folder, it will (usually) be packed along with the rest of your code. Project dependency adds a compile-time dependency, but (as you already said) it will not be packed into your build. However, that dependency will have to be satisfied at runtime (meaning, for example, Google Play Services will have to exist on the device that runs your code). Aside from that, gradle keeps "its own set of dependencies" to be able to build the application not depending on the IDE you are using. But most IDEs are well integrated with build tools, so they automatically synchronize dependencies between IDE project dependencies, and build tool's dependencies. I hope this makes sense :)

Two Maven project with public module

I have a project that devided into three pieces, PCommon, PWebapp and PAdminConsole. PWebapp and PAdminConsole are dynamic web project in eclipse, PCommon is java project, and both two web project will use the api in PCommon as a jar file in lib folder.
In the past,I added import project in build path, I used Ant to compile and build PWebapp and PAdminConsole and in both build.xml file there is
<ant antfile="${common}/../build.xml" inheritAll="false"></ant>
to make PCommon into a jar file.
Now I will change all my projects to Maven Project. But I don't know how to make two web projects contain one public module, and how to package my PCommon into a jar file automaticlly when I run package maven command to package one web project.
Now I always deploy my PCommon.jar on nexus server. and then add dependcy in pom.xml in web projects. But I think there is no sense to deploy the jar on the public server, so I think it isn't the right way to archieve this goal. Is there any way that is more convenient?
I know I can make a parent project with a parent pom.xml. But I have two web project need the module, the pom.xml in PCommon can only extends one parent, can't it?
The common way to resolve dependencies in Maven is using a repository. The first time a dependency is needed, it is downloaded from your repository and installed in the repository on your local machine. If a dependency is not available in the remote repository it has to be installed to your local one in some other way. There are a few other ways to resolve depenencies without using the repositories but I wouldn't suggest to use the.
To make this a little more convenient, you can use a proper IDE. I use Eclipse with the m2e plugin. It supports something called "workspace resolution", which should be enabled by default. It scans your workspace for other Maven projects before falling back to the repository lookup. This has the advantage that every change you make in your common project is immediatly available in the other projects. I think it also gets installed to your local repository in the background but I'm not sure. Anyway you don't have to worry about it yourself.
Something similar works with IntelliJ IDEA but I don't have that much experience with it. I'm sure Netbeans has some kind of Maven support too.

Applying Maven to a project

I've been asked to apply Maven to a project. After browsing a dozen sites it appears that it's quite vast and I'm not familiar as I'd like with similar tools like Ant. Why is it used/preferred and what does it offer over a standard Eclipse project? Also, how could it be added to an existing project?
Why is it used/preferred and what does
it offer over a standard Eclipse
project?
It is a build tool which can build your project without the need for an IDE like Eclipse. It can create a jar or war or other artifacts from the source, performing a bunch of steps like compilation, running unit tests, etc.
Where maven scores over ant is in managing third-party dependencies and in convention over configuration (which mean less lines of build script if you follow convention).
Also, how could it be added to an
existing project?
You start by creating a new maven project, following the step here.
Place it in the root folder of your project
If your source and resource files do not follow maven folder convention, update maven properties suitably referring to this documentation.
Run mvn package
It will fail if it needs any third party dependencies, which you can add as specified in the doc
With some trial and error, you should have your project running with maven, possibly, much quicker than if you were to set up the same with ant.
Others are already provided sufficient resources to read more about maven.
I suggest to start reading here:
http://www.sonatype.com/books/mvnref-book/reference/public-book.html
Maven is a great tool when you know how to use it. Maven (at core) is a dependency manager.
You include in your pom.xml (similar in function to the build.xml from Ant) all the librairies your project depends on (example : apache commons) along with their version and Maven get them directly from a repository (by default, the central maven repository)
Then you do not have to manually install any jar to make your project work. All is downloaded and cached on your local machine. You can even create an enterprise repository where you put all the jars needed by your company
Maven uses the concept of artifacts which are pre-built library projects with their own dependencies
To mavenize a project, you'll have to write a pom.xml describing your project (examples are numerous), get rid of your libs directory (or whatever classpath you described under Eclipse) and add all your dependencies to your pom.xml
You could also check Mavenizer for a first-start
But Maven is a lot more what i've just said. Read the docs, read poms from librairies and you'll get used to it quickly ;-)
If you use the M2Eclipse plugin from Sonatype, it's just a matter of right clicking the project in the package explorer and choosing Enable Dependency Management in the Maven menu. You are also advised to adjust the directories that contain the sources to the Maven standard directory layout but if you absolutely can't, you can configure that later.
Apart from that: Well, look for tutorials and documentation (for example there is the free book Better builds with Maven. Maven is very complex (yes, I don't think it is simple) and very powerful.

Categories

Resources