Java project and maven project [duplicate] - java

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>

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

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.

Maven: add external jar folder to classpath [duplicate]

This question already has answers here:
Can I add jars to Maven 2 build classpath without installing them?
(24 answers)
Closed 6 years ago.
I've simple Eclipse Web Project based on Adobe framework.
Now I have to upgrade my project to a new version of framework and I wish to use Maven to manage dependencies, packaging, ecc.
The problem is that in the Adobe documentation is write that I MUST use thier own jars that are a lot (69). I see Maven System_Dependencies, but i'm looking for something smarter that add all 69 entries like that.
Usually, I would create an Eclipse UserLibrary and would add to Eclipse BuildPath, but in this case I don't know how to add them to maven classpath to compile and package the project properly
To include external jars you can add the dependency with System scope.
<dependency>
..
<scope>system<scope>
<systemPath>your jar path</systemPath>
</dependency>
Also you can define your compiler plugin to include the directory in your classpath.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<includes>
<include>directory path/*.jar</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
NOTE: This will make your build success. But not sure how to make those classes available for access inside IDE.

Maven + Tycho, adding Maven dependencies

We have an Eclipse Plugin which we build using Maven and Tycho. Currently
however, we still provide all project dependencies through a bunch of manually
added JAR files and not by Maven. This is due to the following reasons: (1) The
dependencies are not available through a standard Eclipse update site (at least
not in a current version), (2) the dependencies are not available as bundles.
The biggest part of these dependencies are the Selenium libraries (API, Remote,
browser-specific libs and their transitive dependencies, such as Guava, etc.)
I've wasted hours, trying to pull those dependencies during our Maven build.
Following this SO question, I tried the p2-maven-plugin, created an update
site with our dependencies which I added to my Eclipse target platform. However,
during runtime, classes, which are referenced across different JARs could not be
loaded (I assume, from my very limited OSGi knowledge, because some
necessary information was missing in the MANIFEST.MF files). Here's an example
of the issue, when trying to create a RemoteWebDriver, which uses the
DesiredCapabilities class (both classes in different bundles):
Exception in thread "Thread-8" java.lang.NoClassDefFoundError: org/openqa/selenium/remote/DesiredCapabilities
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:243)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:126)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:153)
…
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.remote.DesiredCapabilities cannot be found by org.seleniumhq.selenium.remote-driver_2.45.0
at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:439)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:352)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:344)
at org.eclipse.osgi.internal.loader.ModuleClassLoader.loadClass(ModuleClassLoader.java:160)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
Is there anything I still need to take care of, when using the p2-maven-plugin? The relevant parts of the pom.xml looked like this:
<plugin>
<groupId>org.reficio</groupId>
<artifactId>p2-maven-plugin</artifactId>
<version>1.1.1-SNAPSHOT</version>
<executions>
<execution>
<id>default-cli</id>
<configuration>
<artifacts>
<artifact>
<id>org.seleniumhq.selenium:selenium-remote-driver:2.45.0</id>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
Couldn't get it to work, so we're now using the maven-dependency-plugin with the copy-dependencies, which we execute during the Maven initialize phase to pull all necessary dependencies (contrary to my initial feeling, this can be combined with the pom.xml using the eclipse-plugin packaging and the "manifest first" approach). The relevant part looks like this:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>initialize</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeScope>runtime</includeScope>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
The Maven dependencies are then copied to target/dependency.
Only little issue: The Bundle-ClassPath in the MANIFEST.MF needs to be manually updated in case the name of a JAR file changes when updating Maven dependencies (e.g. commons-io-2.4.jar becomes commons-io-2.5.jar).
[edit] Revisiting this answer in regards to the last sentence above: The version numbers can be conveniently stripped through the following option: <stripVersion>true</stripVersion>. This means, the above library will be renamed to commons-io.jar and thus no paths need to be updated when a version number changes.
Another possibility:
Some jar files may be broken (if you're using Eclipse, it's commonplace hibernate-commons-annotations-4.0.1.Final.jar; invalid LOC header (bad signature)? ). To check this possibility, try manually opening the jar to see if it's okay.
I also build an Eclipse plugin with Maven and Tycho. I have the same problem: the bundle org.eclipse.team.svn.core and org.eclipse.team.svn.ui are not available through a standard Eclipse update site.
Maybe you can try this to solve this kind of problem:
In Dependencies, find the box Automated Management of
Dependencies.
Add the wanted plugin using Add...
Choose Analyze code and add dependencies to the MANIFEST.MF via: Import-Package
Click on Add Dependencies so that you find required packages in the box Imported Packages nearby.
Then you can run the Maven build.

maven create jar with pom

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/

Categories

Resources