How to reference a java project in an eclipse maven project? - java

I have two java projects in eclipse. One is maven enabled, the another one is just a normal java project. I want my maven project to reference the java project on build path.
My question is, Any simple way to reference the java project without changing it to a maven project ?
Firstly, i know that i can export the project as a jar and then maven install it. Then i can reference it in the dependency section of pom.xml. This is not dynamic enough to suit my needs.
Is there a maven plugin that can help me. Thanks in advance.

You can use the answer here (https://stackoverflow.com/a/22300875) to "import" the JARs, and the link (Maven compile with multiple src directories) for the source. That way the project will remain pure Maven. Let me know if Eclipse has some issue with this solution.

Related

Use Maven Library without Drinking Maven Koolaid

I want to use the diffplug/Durian library, but do not want to use Maven. Is there a way to use a Maven library without using Maven itself in a project?
This has a link to the .jar and lists it's dependencies.
http://mvnrepository.com/artifact/com.diffplug.durian/durian/3.4.0
Download the jar - then all the jar's dependencies and their dependencies and add them to the class path.
I found that IntelliJ IDEA allows for exporting of a Maven module to a JAR, that can be used in non-Maven projects.
https://www.jetbrains.com/help/idea/2016.1/downloading-libraries-from-maven-repositories.html
And needed to know what a "Maven Coordinate" was. I found it for the library in question at https://mvnrepository.com/artifact/com.diffplug.durian/durian/3.4.0, and it is the string "com.diffplug.durian:durian:3.4.0".
Basically, follow Project Structure->Project Settings->Libraries->"+"->New Project Library->From Maven. The resultant dialog takes a Maven coordinate, and has a "Download To" option, that will make a nice JAR at the specified location, from the Maven library you import. Can add source and javadocs as well. After doing the download, you navigate to the system folder containing the new JAR, and stick it in your real non-Maven project (an Eclipse project, in my case).

I just started learning Maven. How does Eclipse build without it?

I've been coding Java in Eclipse for awhile without needing to specify dependencies. Now that I learned how Maven does it, I'm wondering: how did Eclipse build projects on its own? How did Eclipse figure out which versions of imports, and which dependencies of dependencies, are needed to make everything work?
Finally, what are the advantages and disadvantages of building a project in Eclipse by starting with New->Other->Maven Project instead of New->Java Project?
To the first question: Eclipse doesn't add any dependency in a standard Java project. You have to manually add to the Build Path all needed JARs, otherwise you'll have compilation errors.
To the second question: if you create a standard Java project Maven is not used, even if you create a pom.xml file in the root of the project. You can always convert a standard Java project in a Maven project (see Convert Existing Eclipse Project to Maven Project).
Your dependencies has always to be in the classpath
When you start a project with Maven, Eclipse will automatically add the Maven repository to the classpath.
When you start a Java project you have to link your library manually in Eclipse and the version of the library is the one you've downloaded.
You can see the difference in your project's Properties > Java Build Path > Libraries

Is there a way by which i can automatically add dependencies based on the jar files present in my build path using maven?

I have a j2ee project which is having many jar files attached to it, now i want to convert it to a maven project, is it possible to get all the dependencies of the attached jar files in the pom.xml automatically without adding them one by one manually?
Sorry if this is a noobish question !
AFAIK, you cannot.
But your IDE could help you. Netbeans or Eclipse both help you to find the names of the group and the artifact for almost any library.
On Netbeans it is Add Dependency.
On my opinion: yes, you can. Will you try such scenario: import your project into Eclipse, then convert project into Maven project throw context menu on project. It's not easy way, but this solution is worked. May be another IDE can convert more effective.

How can I use a src folder library from GitHub

I am trying to use this library from github.
They only provide a src folder and there is no compiled JAR that I can use.
My question is, How can I use the library in my Java project in the Eclipse IDE?
I've tried researching but I can't find a single website that doesn't assume I know how to use these types of libraries. It must be something obvious that I'm missing. It would be great if someone could point me in the right direction.
Clone the project with git, then make a build yourself, there is an included pom.xml file for maven
UPDATE
link from maven central
It's a Maven project. You will have to build it using Maven and then import the build jar from ~/.m2/repository or target directory in your Eclipse project classpath.

How to force Eclipse to see java project as java project?

I have downloaded a 3rd party project, which consists of multiple files, including Java ones.
General structure is as follows:
<topfolder>
pom.xml
<subfolder1>
pom.xml
src
main
java
<normalclasspath>
resources
site
apt
index.apt
test
java
<normalclasspath>
Eclipse imports this project normally, but is unable to index its Java content. For example, I can't browse from a variable to its definition, and so on.
Also I can't set my own Build Path since it says No action available.
Of course, I can refactor folder structure myself to suite eclipse needs, but are there any automation means for this?
UPDATE
Yes, this is a Maven project and Eclipse already knows that it has Maven nature. The only option now is to disable it
But this is not a question. The question is how to add JAVA NATURE, so that Eclipse knows consistence of classpath and be able to navigate to class definitions and so on.
UPDATE 2
#75inchpianist's answer about facets helped partially. It was not available to select Java facet immediately, but required turning on facets at all first. Then Java facet was already there.
Now I see, that Eclipse interprets Java, but Maven interpretation is not full. Namely, no Maven dependencies interpreted (no Maven Dependencies node in Package Explorer).
The attempt to add it fails:
Right click on the project and select "Properties"
Within "Project Facets" make sure that Java is selected!
As this is a maven project (because of the pom.xml) you need to install a maven plugin for eclipse.
Next you have to right click on the project and choose "Configure->Convert to Maven Project".
Now you should have your normal java structure.
Assuming that Eclipse recognizes the project as Java project yu can do the following:
If you don't want to install a maven plugin into Eclipse you can make Eclipse recognize the source correctly by adding every src/main/java folder as Source-Folder to your Build-Path.
This is better than modifying the folder structure as you can update the sources without problem later.
Add following to your .project file and refresh the project.
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>

Categories