Resolve classes of maven dependencies in eclipse - java

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>

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.

Stopping m2e to create non-existing source folders specified in build-helper-maven-plugin

In the parent pom of my projects I've the following build-helper-maven-plugin setup:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>add-test-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/it/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
so that when I do Maven -> Update project, m2e adds the src/it/java folder to the sources of the project.
That folder exists in most of the projects using that parent pom, but for the projects that don't have the folder, m2e will create it. I would like that folder to not be created. Is there a way to tell eclipse/m2/build-helper plugin to not create the source folder if does not exist?
I'm using eclipse Kepler and m2e 1.4 with build-helper connector 0.5.
When I do Maven -> Update project and untick the "Update project configuration from pom", then the folder is not created, but I would rather tell m2e not to create the folder instead of unticking the option for certain projects only.
This issue is not happening using an old version of the plugin (0.10.2) in eclipse Indigo.
why don't you update the required projects folder's path instead of using a plugin to do it ?
m2e will pick it up more easily, and it will be more standard from a maven point of view.
Here is a link to the entry to update: http://maven.apache.org/pom.html#Build_Element
If not using your current plugin is not an option you could add a profile which activates your current plugin, only if your folder exist in the current module.

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/

How to run a Maven project from Eclipse?

I am trying to run a simple Java project. I had created a project using the 'Maven Project' type. I have one main class called 'TestMain'. When I tried to run the project using right click -> run, there was no menu to run the application as 'Run As Java Application'. I am wondering where that option has gone.
Can anyone please help me to run the Java application?
Well, you need to incorporate exec-maven-plugin, this plug-in performs the same thing that you do on command prompt when you type in java -cp .;jarpaths TestMain. You can pass argument and define which phase (test, package, integration, verify, or deploy), you want this plug-in to call your main class.
You need to add this plug-in under <build> tag and specify parameters. For example
<project>
...
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1.1</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>my.company.name.packageName.TestMain</mainClass>
<arguments>
<argument>myArg1</argument>
<argument>myArg2</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
...
</project>
Now, if you right-click on on the project folder and do Run As > Maven Test, or Run As > Maven Package or Run As > Maven Install, the test phase will execute and so your Main class.
(Alt + Shift + X) , then M to Run Maven Build. You will need to specify the Maven goals you want on Run -> Run Configurations
Your Maven project doesn't seem to be configured as a Eclipse Java project, that is the Java nature is missing (the little 'J' in the project icon).
To enable this, the <packaging> element in your pom.xml should be jar (or similar).
Then, right-click the project and select Maven > Update Project Configuration
For this to work, you need to have m2eclipse installed. But since you had the _ New ... > New Maven Project_ wizard, I assume you have m2eclipse installed.

Categories

Resources