maven create jar with pom - java

I'm working on an Android project and have some core code(that has some dependencies) that i'd like to version and make into a library/artifact(?) that I can pull into other projects. I'm using Maven to build and my editor is IntelliJ. I've never created a .jar but I think that's what I want to do.
In IntelliJ, i've gone to File->Project Structure->Artifacts->+ but I'm lost. I don't know how to define which source directories and files to include in the jar and I'm unsure if I need to include the actual jars of its dependencies in the jar? or define those dependencies in a pom.xml file and include the pom.xml file in the jar?
Any clarification would be much appreciated.

Maven project's dependencies are defined in its pom.xml file (basically, changing the dependencies from here is what are you visually doing using Intellij, by File->Project Structure->Artifacts). The source files that Maven takes are the ones which are into the src/main directory of your project. The build is done by default excluding the dependencies, so if you want to include them in your final jar, you have to specify this in your pom file (plugins):
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>attached</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
After that you have to execute mvn install command to have your jar created. You can do it running maven externally or from your IDE (if you have some Maven plugin installed) and the jar will be created in project's target folder.

The settings in IntelliJ IDEA will only create the JAR when you build using the IntelliJ IDEA compiler. To have maven create the JAR and deploy it to the maven repository, you need to use the maven assembly plugin. There is a predefined configuration for creating a jar with dependencies.
The Maven Shade Plugin can also create JARs and handle more advanced use cases. But you'll probably want to start with the assembly plugin/

Related

IntelliJ - Maven adding external jar file but java.lang.NoClassDefFoundError

I am using IntelliJ version 11.0.7 (2020.1.3) created a simple maven project and added my jar to it by
File -> Project Structure -> New Project Library -> Java -> Selected my jar -> Ok -> Ok
in that jar file, all the dependencies present which requires to run the application.
There are no compile-time errors but when I run my maven project then it is throwing this exception:
Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonProcessingException
After adding this jar it is throwing an exception about the next missing jar, likewise when I added all the dependencies which are used inside that jar then everything works fine.
Is there any way to auto-generate all the dependencies and add to External Libraries from the jar when I added to it?
in that jar file, there are all the dependencies present which requires to run the application.
Are you sure that all dependencies present? Your next statement saying After adding this jar it is throwing exception about the next missing jar, likewise when I added all the dependencies which are used inside that jar then everything works fine.
Is your jar file hosted in maven repository? If yes, simply declare it in maven pom.xml file, it will manage all the transitive dependencies. If it is not in maven repository, you need to run mvn install command to install that into your local maven repository, later on refer it in your pom.xml file. It will auto resolve your transitive dependencies as well, if you package your jar file properly which include correct pom.xml inside.
Finally, I added this in my pom.xml where I generated the Jar file
To get Maven to build a Fat JAR from your project you must include a Fat JAR build configuration in your project's POM file. You configure Maven to build a Fat JAR from your project by including the maven-assembly-plugin in your POM file's plugin section. Maven refers to an output product that it builds as an assembly. Hence the name maven-assembly-plugin.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
Now it is working as expected.
Reference: http://tutorials.jenkov.com/maven/maven-build-fat-jar.html

Resolve classes of maven dependencies in eclipse

Consider the following dependency hierarchy:
Now I have maven-jibx-plugin in project D which generates the compiled classes in target/classes folder. But when I run my spring-boot project A the generated classes from project D could not be resolved.
Resolve dependencies from workspace is also checked from maven preferences of project A
To me it looks like Eclipse and Maven do not recognize project D as a related project.
There are two possible solutions:
In Eclipse you can add project D as a dependent project to project A's build path. Go to the project's Properties dialog. Select Java Build Path and then switch to the Projects tab. There you should add project D.
Or alternatively you rely on Maven's dependency management. Therefore you have to add a dependency to project A's POM file. First add a <dependency> ( if not already done ) to the <dependencies> section. Now comes the important part! Maven can only resolve this dependency if you have installed the compiled maven artifact ( the jar file ) in your local maven repository. On the shell switch into project D's directory and then run mvn install
I hope that did the trick.
Have you tried adding generated sources folder as a source folder in eclipse? You can do this from eclipse (right click the generated sources or any folder in it > Build Path > Use as Source Folder) or you can use maven build helper plugin by adding something like below to your pom.xml
<!-- MAVEN ADD GENERATED-SOURCES TO CLASSPATH -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>${maven.plugin.build-helper.version}</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>target/generated-sources/annotations</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>

maven - referenced libraries not found in packaged jar [duplicate]

This question already has answers here:
Add external library .jar to Spring boot .jar internal /lib
(8 answers)
Closed 4 years ago.
When I mvn clean package the project and run it with java -jar target/project.jar it throws an error that it cannot find some class from an external jar.
Steps taken
All external jars are in a folder of my project: /jars or /jars/morejars/
Adding the jars to the build path: In Eclipse I right click on project, go to build path and select add external archives. I can see that eclipse creates a library folder "referenced libraries" below the "Maven dependencies" folder. When I check project -> Properties -> Java Build Path -> Libraries I can see all the imported jars.
Some of those external jars are described as <dependency> (with <systemPath) in pom.xml, so they will not be seen in Referenced Libraries but in Maven dependencies (interestingly, the class that cannot be found when running my packaged project is in an external jar that doesn't reside in Referenced Libraries but Maven dependencies)
e.g.
<dependency>
<groupId>external.jar.groupid</groupId>
<artifactId>external.jar.artifactid</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/jars/external-jar.jar</systemPath>
</dependency>
Use the maven assembly plugin (as described here), this is my full <build> config:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
Run mvn clean package -> BUILD SUCCESS
java -jar target/project.jar -> Cannot find some.class from external.jar
Thanks for the help
The problem is clearly with dependencies. While your application starts loading it looking for required classes to run properly. While it loading a class from a jar library and that library class also refer to some other class from a another jar library file but that library (jar file) is not available in your class path, it will give you the above error. So check for maven dependency and get all the jars required by your application. Also based on error message also you can add the jar reference in your pom.xml file. For example if you get an error like Failed to load com.apache.some.Example.class just google the Example.class jar file and get it from maven repository.
Hope this will help you.

Java project and maven project [duplicate]

This question already has answers here:
How can I create an executable/runnable JAR with dependencies using Maven?
(33 answers)
Closed 6 years ago.
I have several java non-maven projects (with one main project) and several maven projects (with one main project). Now I need to use the maven-projects functionalities in the non-maven projects.
I have to say, I know very little about maven. And I'm working in NetBeans IDE.
There are several options I've come up with:
Make non-maven projects maven projects and add dependencies.
I cannot do that because others use the non-maven projects their way and I cannot just make changes like this.
Make maven projects non-maven projects and add them as Libraries
I cannot do that because there would be a lot of libraries to add. The dependencies might be large.
To non-maven projects add jars as libraries of the maven projects. This is same as the (2) option. I tried it and added all the maven project jars as libraries to my non-maven main project, but at a run-time there was a lot of NoClassDefFoundError exceptions because of missing jars (3rd party jars that the maven projects depends on).
?
Any ideas?
Thank you in advance.
Solved
I used the pom adjustments from chresse.
The whole pom is here: https://codeshare.io/5ZeYN2
I used the maven command from the question Tunaki marked this was duplicate of (how-can-i-create-an-executable-jar-with-dependencies-using-maven)
mvn clean compile assembly:single
Thank you all.
you can generate a jar from your maven project including all dependencies.
add the following plugin to your pom of your main maven project. the mvn assembly:single will create a additional jar ('projekt'-jar-with-dependencies.jar) with all dependencies, which can be included in your non-maven project:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

How do you include a local jar when using mvn assembly:single to build a jar-with-dependencies?

Specifically, I am trying to build a Datomic (http://www.datomic.com) jar with dependencies so that I can experiment with it in the Jython REPL.
Datomic is not distributed on Maven, but you can download and install Datomic into your local Maven repo like this:
mvn install:install-file
-DgroupId=com.datomic
-DartifactId=datomic
-Dfile=datomic.jar
-DpomFile=pom.xml
But I need it as single jar with its dependencies so I can put it in my classpath and use it from the Jython REPL.
mvn assembly:single builds a jar with dependencies, but it doesn't include the Datomic jar, presumably because it's a local file.
How do you include a local jar when doing mvn assembly:single?
I think you will be better off with the Maven Shade Plugin:
http://maven.apache.org/plugins/maven-shade-plugin/
This will allow you to create a single über-jar, which will contain both your own classes as well as dependencies.
It doesn't matter if the jar is installed locally, as long as you add the jar file to your project's pom.xml file (dependencies section):
<dependencies>
... ...
<dependency>
<groupId>com.datomic</groupId>
<artifactId>datomic</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
Check out Maven Assembly Plugin docum jar-with-dependency:
Use jar-with-dependencies as the descriptorRef of your assembly-plugin configuration in order to create a JAR which contains the binary output of your project, along its the unpacked dependencies. This built-in descriptor produces an assembly with the classifier jar-with-dependencies using the JAR archive format.
Note that jar-with-dependencies provides only basic support for uber-jars. For more control, use the Maven Shade Plugin.
Sample pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<configuration>
<descriptorRefs><descriptorRef>jar-with-dependencies</descriptorRef></descriptorRefs>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals><goal>single</goal></goals>
</execution>
</executions>
</plugin>

Categories

Resources