Make maven dependencies visible in classpath - java

I have a project that can be executed as is or I can pass in its classpath another project so that they run together.
However, in the main project, I am getting an error because it can't instantiate a bean from the second project (since it is an maven dependency inside it).
I saw that I can use maven assembly to export those dependencies as a jar an point in the classpath from first project.
However, this way I will need to rebuild everytime, is there a way to export those dependencies in compile time? To make further development easier?
Thanks in advance.

Related

Unable to create an object of the class from Maven type project

My pom.xml has only 1 dependency -
<dependency>
<groupId>com.shubham.TestNexus</groupId>
<artifactId>TestNexus</artifactId>
<version>1.0</version>
</dependency>
This is a test jar that I have created and uploaded to my local nexus repository. I could see that when I am building the project, maven is downloading the jar and placing it in the Maven Dependencies directory. I could see the same jar added to the classpath as well.
This jar has nothing but a simple helloWorld printing method.
Now when I am using this jar in my project, it is not allowing me to create the object of the class inside this jar.
And, when I tried making a non-maven java project and added the jar manually to the classpath I am able to create the object of the class. Can anyone please help here.
I found the solution to the problem, seems like it doesn't allow the jars that have classes, present in the default package. I created another jar and put it inside a package and it worked. Thanks a lot everyone.
I work with maven aswell. When i have problems with dependencies to try all this steps:
Use the maven clean and install commands.
Use the maven update project to force it.
Refresh all the project.
If it doesn't work the problem could be on m2 repo.

Difference between intellij Project make and Maven Compile?

When working in maven module, what's the difference between doing in intellij build -> Make Project and Maven Projects -> Root pom -> Compile phase.
Does intellij calls maven?
Does both of them compile the sources to the same place?
Do they both copy resources files?
Why do we need both options?
Does intellij download the dependencies automatically and we can just call project make, without using maven compile?
They are actually quite similar in terms of the task they perform which is to compile the source and test paths of the project with javax.tools.JavaCompiler by default.
Does intellij call maven?
No it doesn't by default. Remember that this is just IntelliJ's built in Java compiler and some Java projects don't use maven.
Does both of them compile the sources to the same place?
Yes, they use the same source files to compile if that's what you are asking here.
Do they both copy resources files?
Yes they both copy the resource files to different locations though.
Why do we need both options?
We don't need both options. Maven compile command checks the source code against any syntactical errors which effectively covers the work done by IntelliJ's make option.
Does intellij download the dependencies automatically and we can just call project make, without using maven compile?
Intellij's compiler can be considered as syntax compiler which is why you need maven to handle your project's dependencies.
No, IntelliJ doesn't call maven, but get all information from the pom.xml e.g. output directories libraries etc.
So don't worry, you can call make from IntelliJ for compiling and running your application.
If you have some special goals in your maven build file you can run it from the maven window.
All maven dependencies will be downloaded from IntelliJ an stored in the maven local repository.
Let's think we have a Project A which is use Maven Project B.
If you had changed B project you have to update this project on A. Then you have to compile B in A.
But if you had changed only A you dont have to compile Maven.

Maven dependencies in p2 project

Let's consider the following projects:
Project1: uses Eclipse P2 Target Definition for dependencies
Project2: uses Maven repositories for dependencies
The problem: project1 requires a dependency which is present in project2 which comes from a Maven repository (and is not available in p2).
My workaround up to now is that I am exporting project2 as a jar with dependencies. Then, I add this jar to project1 and can access the dependency from there.
How could I do this in a better way?
What do you mean with a better way?, What do you think you are doing wrong?
The only way to use libraries or classes from external projects is by importing the package that contains them. In this case, i guess you made this package using (on your second project) mvn install, mvn package or similar, which is perfectly fine...
If you import to your first project the jar that you just created it is ok too!, there is no better way to do this unless you combine both projects and make them a single one.
You could use Package Drone, an open source tool I am currently working on. If your maven dependencies are OSGi bundles, you can drop them into Package Drone and let it create a P2 repository from it. So you can re-use these Maven dependencies using P2.

Maven: Using listed dependency in build path in Eclipse?

I am new to Maven, and as I added a dependancy in my pom.xml, the dependency got resolved and gets packaged in target/dependencies.
However I do not know how to use them in Eclipse.
It is not in the project's java build path in Eclipse, and even though I can circumvent this in various ways (e.g. manually add the dependencies from the local repo to build path in eclipse), there should be a proper way to manage your dependencies in maven and for Eclipse to be able to "see" the dependencies. I want to add the dependencies on one place (pom.xml) and both maven and eclipse to able to understand that and add them to build path/classpath.
How is this done? Via plugin? Which plugin should I use?
I assume you are using the command line Maven option instead of the eclipse plugin.
if that is the case, then once you have added dependency and did a "mvn install", you need to do "mvn eclipse:eclipse".
Every time you add a dependency and install, you will have to do a eclipse:eclipse else it will not put the new jars in classpath.
Alternatively, switch to m2eclipse

Link maven dependencies on non maven project

I have two projects in Eclipse, the first project depends on maven, the second project which dependent on the first one does NOT depend on maven.
The first project downloads external libraries like jar files and natives to the .m2 maven folder. However the second project gives a ClassNotFoundException since it cant find the jar files and the native files from the first project.
Is it possible to link these downloaded jars+dlls with the second project without having to reference in the build path->libraries in the second project properties?
I would appreciate any help.
In your Maven project, use Assembly plugin to create an Uber-jar that contains the project build artifact and all its dependencies (mvn assembly:assembly -DdescriptorId=jar-with-dependencies). Then, reference that from project #2, either with a relative path or by using an ant build task to copy it into your other project's lib directory (assuming you have such a directory). Also, although it's frowned upon, you could configure the assembly plugin so that your Uber jar artifact always has the same finalName.
Is it possible to link these downloaded jars+dlls with the second project without having to reference in the build path->libraries in the second project properties?
I don't think so.
But maybe you could create a 3rd project (which is a Maven project) that depends on the first one, and on the JAR (or whatever) file created by the 2nd one as a non-repository dependency.
Having said that, anything you do is going to be a bit of a hack. You'd be better of either turning the 2nd project into a proper Maven project, or creating a custom build script that manually pulls the 2nd project's dependencies from somewhere. (I think that Ivy could help you with that ... assuming you use Ant in the 2nd project.)

Categories