How to retrieve automatically dependencies of an arbitrary jar? - java

I'm analyzing a jar statically. In this jar there are references to classes not defined neither into the jar itself nor in the pom.xml file or any other config file. How to retrieve automatically the jar containing the missing class without using an IDE?
Example: in the jython2.7.1b3 jar (https://mvnrepository.com/artifact/org.python/jython/2.7.1b3) there is a reference to "javax.servlet.HTTPServlet" (dependency found with JDeps) but not defined anywhere.

You can use the dependency tree of maven as done in this answer:
https://stackoverflow.com/a/3342951/7758117
Create an empty project with only a pom.xml and a dependency to your external dependency. As this jar has a pom.xml, you should then be able to run mvn dependency:tree on your project and then get the dependencies of your jar and which library uses this class.

Related

How to add config files to a dependency jar to create target executable jar using maven

I need to create a "wrapper" project with a pom that pulls in a dependency jar file and adds some local config files to it (form src/main/resources) to create the target jar.
Can this be done using spring-boot-maven-plugin, maven-dependency-plugin, a combination of the two, or some other plugin entirely? (I have to use maven)
An example pom would be much appreciated.
Note: the dependency jar is built by a 3rd party as an executable jar (with transitive dependencies included) using spring-boot-maven-plugin. Likewise the jar I build must have the same main class and include all dependencies contained within the 3rd party jar.
Thanks in advance!

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

Import external JAR Eclipse

I have a Maven project where I've imported an external JAR (via Build Path) and it's under Referenced libraries folder. How can I import it within my .java class file? I mean literally the code "import ??". The class is in com.example.demo package. Do I need to add a dependency somewhere (pom.xml)?
If that's a maven project the best way would be to add that jar as a dependency in your pom.xml dependencies section. Then when you compile with maven it will use that dependency. In order to build properly in eclipse you need to call mvn eclipse:eclipse and it will sync the dependencies in the pom.xml with the build paths of the eclipse project.
If the jar is not a common one and you cannot add find it in the public repo (for example some jar that you have created) then you need to first install it in your maven repository with the required group and artefact name. Otherwise if it is some library which is publicly available in the central maven repository maven will download the required jar and install it in your local repository for you

How are dependency binaries included in my final built and installed .JAR in Maven?

1) I included a Spring Context dependency in my pom.xml project in Eclipse with Maven.
2) I ran the 'Install' phase on the project and it built properly, and the project was installed to my local .m2 repository
3) When I unzipped my .JAR, I only saw my single class that I created custom.
This brings up two questions:
1) Are external, dependency classes only included in your final built jars if a class from it is physically instantiated within your class?
and
2) How come, when I imported the SpringContextAnnotationConfig class into my class, and instantiated an instance of it, and installed my project, I STILL only saw my custom class when I unzipped my .JAR. Is this unusual? Or is the SpringContextAnnotationConfig now just written into the .class binary so that when I deploy my .jar to another JVM, it hass all its dependencies within my custom built .class binary?
Thanks
SOLUTION:
The problem was that I was expecting maven to do the same for a JAR output as it would for a WAR. When using the webapp archetype to generate a WAR file, Maven automatically packaged the required dependency jars into the WEB-INF directory.
I was basically trying to understand how a container on a remote, brand new server would run my classes without having the dependency binaries. I was attempting to run a maven built to produce a JAR file, which did not end up including my dependencies. However, when I ran maven install to build a WAR file, my dependencies were all there ready for deployment.
No, they are never included (unless you use a special plugin which does that).
See 1.
If you add this artifact as a dependency to some other project, its dependencies (and their dependencies, etc.) will be automatically added (this is controllable, so you can e.g. exclude them or change the version). But they are taken from pom.xml, not from the .jar itself. This also allows not to download same libraries a huge number of times.
For building fat jars, see e.g. How can I create an executable JAR with dependencies using Maven?. Specifically for Spring you may want Spring Boot.

what happens when java builds the jar (war)

I'am new to maven and because of the scope properties I began to think about how java builds the jars (or wars). Let's say I write the source code for my project which will be compiled as myProject.jar. For my project I need one other jar the externalProject.jar. So I add externalProject.jar to my build path.
Question: When I compile my Project to get myProject.jar, will externalProject.jar included in myProject.jar, so that I can deliver only myProject.jar and it will run? Or is myProject.jar just calling code of externalProject.jar and exernalProject.jar must be in the same folder as myProject.jar and must be delivered too?
Helmsen
That will depend on how you configure the dependencies in Maven.
Using a dependency scope "Runtime" for externalProject while configuring dependencies for myProject in its pom.xml file, indicates to the dependency management framework within Maven that it is not required for compilation, but is for execution.
Similarly using "compile" which is also the default scope, indicates that project be included in the classpath. So most likely you are using the "compile" default scope for the externalProjects dependency if you do not specify explicitly specify scope. In this scenario, your dependency will be included as a JAR for your project.
I would recommend reading the Maven-Introuduction-To-Dependency-Mechanism

Categories

Resources