what happens when java builds the jar (war) - java

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

Related

'Correct the classpath of your application' in a Springboot Configuration in IntelliJ

I have such an application with an IntelliJ-Idea IDE that has a Springboot configuration that errors out and gives the following recommendation:
The following method did not exist:
org.springframework.context.ConfigurableApplicationContext.setApplicationStartup(Lorg/springframework/core/metrics/ApplicationStartup;)V
The method's class, org.springframework.context.ConfigurableApplicationContext, is available from the following locations:
jar:file:/C:/Users/tlmitch/.m2/repository/org/springframework/spring-context/5.2.8.RELEASE/spring-context-5.2.8.RELEASE.jar!/org/springframework/context/ConfigurableApplicationContext.class
Correct the classpath of your application so that it contains a single, compatible version of org.springframework.context.ConfigurableApplicationContext
I'm relatively new to IntelliJ-Idea and Springboot. If this were say Eclipse - I would probably just go to the Java Build Path and add the jar file that contains the class.
I'm less familiar with IntelliJ. So far if something is missing, I've just been running 'npm install' and that usually takes care of it.
Any suggestions?
Thanks much
If you've added Spring dependencies in your pom.xml file, they might be conflicting with the Spring dependencies in the lib folder in your project directory. Deleting the lib folder will fix this. (just make sure your project does not depend on other libraries in the lib folder before deleting).
As error suggests:
Correct the classpath of your application so that it contains a single, compatible version of org.springframework.context.ConfigurableApplicationContext
you need to make sure your classpath contains only one such class. You can use Navigate | Class action to see if there are multiple versions of this class exists in project and exclude the duplicated library dependency in Maven pom.xml file.

How to retrieve automatically dependencies of an arbitrary jar?

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.

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.

ant to maven migration maven - jar building

I am migrating an Ant project to Maven. There are a lot of Java projects in "Ant solution" which are only compiled to a specific directory (compiled to *.class files and copied to build folder). I.e. some project A uses dependencies from project B, which are preceded by copying *.class files from output folder B to resource folder project A.
Are there any possibilities to create a Maven jar-Module which uses another jar-Module only for compilation? Without using <dependency>?
Are there any possibilities to create a Maven jar-Module which uses another jar-Module only for compilation?
Yes, see below
Without using <dependency>?
If you are using another module in your project, then you have a dependency on that project and should be declared as such. If you have a scope of provided, then the project which you depend on will only be used when compiling your project but will not be included in the final jar as it expects the classes it depends on to be provided at runtime.
Not entirely sure if I understood your question correctly as I do not see why you would want to not use the <dependency> section when you state in your question that you actually have dependencies.
http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

maven classpath generation for deployment and test

I have a module that is used by creating a custom class loader. The class loader should therefore be created with the path to the module and also all dependencies.
I'm looking for a way to make it productive to work with this mechanism in both dev and production environments.
I thought the build can generate two files listing all dependency paths. A 'dev' file and 'production' file (then in the code I'd choose the right file based on some system property):
dev: dependencies should be paths to thirdparty jars in the local maven repository or 'target\classes' for other modules (which will allow to compile with the IDE and not need a full build for each change)
production: all jars should be copied to a 'lib' folder and the dependencies should list them (without full path). Alternatively, create an uber jar, and then the (?)
So I'm looking for pointers to plugins & pom snippets to help me with this.
Some hints:
For the dev part, the Maven Dependency Plugin and its dependency:build-classpath can help (and supports writing the output to a file). Another option would be to use the Maven AntRun plugin, it has access to the Maven runtime classpath, it should be easy to write the property to a file.
For the prod part, I assume you'll use the Maven Assembly Plugin or the Shade Plugin or another equivalent and I wonder if you couldn't generate a classpath entry in the MANIFEST.MF and rely on it.

Categories

Resources