Why do I need to look for a class in a jar file that is not included in the maven pom.xml file? - maven

I have imported a jar file in nexus, a local maven repository, and another jar file in the referenced lib via java build path.
The problem appears to refer to the class in the jar file referenced in the pom file of the Eclipse project at all, without using the classes in the referenced lib.
The same class exists in two jar files.
I do not know what caused the problem.
this is eclipse bug?
for example:
a.jar, b.jar -> both have ReferenceClass(same pacakages)
A project's pom.xml -> dependency a.jar
MyProject's referenced libraries -> dependency b.jar
my project:MyClass.java -> import ReferenceClass -> reference a.jar
build path
project structure
pom.xml

Do not use "Referenced Libraries" in a Maven project. This will confuse Eclipse.
Furthermore, try to avoid having the same class twice in your classpath.

Related

Include the bytecode of a JAR file in a gradle library vs. just adding it as a dependency

If I add a JAR file to a gradle project's depenencies, the code compiles just fine, but after publishing to maven (publishToMavenLocal), the classes from the JAR are not found.
Obviously, this is because the jar is added as a "dependency" and not part of the project itself. Is there a way to get the contents of the JAR file to merge into the library? Do I need to make a separate maven repo for each JAR?
You can always try to create a fat jar which includes dependencies. You can follow the instructions provided here https://www.baeldung.com/gradle-fat-jar

Eclipse indirectly referenced .class file

When i build the project i am getting this error
The type org.springframework.core.NestedRuntimeException cannot be resolved. It is indirectly referenced from required .class files
Vaelyr answers above is correct
but To be very specific, you need to add spring-core-4.0.5.RELEASE.jar into your eclipse build path.
Use the spring version that corresponds to your other spring jars.
The class you are trying to use is not apparently on your classpath. Add your Spring jar files to your sources classpath and it should work. If that's not the case do Project -> Clean as well.

Combining jars for multi modules maven project in a single jar

I have a parent maven project that includes some child projects and a build/assembly project. The structure looks like this
ParentProj
+ pom.xml
+ ChildProj1
++ pom.xml
+ ChildProj2
++ pom.xml
+ buildProj
++ pom.xml
I want to create an executable jar in the build project, or project that contains all child project jars. Basically the final result should be a jar that includes all child project jars in the classpath.
Your project buildProj should have the other child projects declared as dependencies, you could then configure the maven assembly plugin to use the predefined descriptor jar-with-dependencies which create a jar that contains the dependencies.
How can I create an executable JAR with dependencies using Maven?.
https://maven.apache.org/plugins/maven-assembly-plugin/descriptor-refs.html
EDIT
If you wish to add the dependencies jar in your jar, you could use something like onejar:
Creating a JAR file which contains other library files
http://one-jar.sourceforge.net/
There is a maven plugin for this tool: https://code.google.com/p/onejar-maven-plugin/

Local Maven Dependency project is referenced as a class folder instead of a jar

I'm using Eclipse Indigo with the m2e Plugin (Version 1.0.1). I have two separate workspace projects: A Maven Project that is basically a Vaadin widget and a second Maven project that is my main project which is referencing that widget with:
<dependency>
<groupId>com.mycompany.widget</groupId>
<artifactId>Calendar</artifactId>
<version>1.2</version>
<type>jar</type>
</dependency>
If I run mvn clean install on the widget project it is packaged properly as a jar and also available in my local Maven repository. I can also use the classes of the widget in my main project. However, in my main project's Maven dependencies, the widget project is shown as a class folder instead of a jar (though all other external widget dependencies are shown as jar files).
This causes some problems when I try to unpack (or even copy) the dependency to my main project with dependency:unpack-dependencies (resp. dependency:copy-dependencies). The maven build fails with:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-dependency-plugin:2.4:unpack-dependencies (default-cli) on project [main project]: Error unpacking file: /.../Calendar/target/classes to: /.../[main-project]/target/classes
[ERROR] org.codehaus.plexus.archiver.ArchiverException: The source must not be a directory.
My question is: How can I reference my widget project as a jar instead of a class folder in my main project?
They are in separate directories, right? Try this:
Right-click your project, select Maven -> Disable Workspace Resolution.
Also check the project properties and make sure that the project isn't referenced in Java Build Path -> Projects.

how to include dependency classes to war project's WEB-INF/classes folder in maven

I've a project B which has a dependency on project A. Both project's packaging is jar. Another project C packaging is war. Project C contains no java classes. It contains only webapps folder. Project C has dependency on project B. I want the war to be packaged in just a way that all the java classes from Project A and B should be copied to WEB-INF/classes folder. How can we achieve this ?
You can use the unpack goal of maven dependency plugin in your pom to unpack the desired dependencies to a folder and ensure maven war plugin includes this folder.

Resources