Use Maven Library without Drinking Maven Koolaid - java

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).

Related

What is the right way to export a jar using maven for a library project?

I have a library project which is built in maven. It has its dependencies. I need to export this project as a jar (Not a runnable jar). Should I include the dependencies along with my jar or should I not? Because when I exported the dependencies with my jar, there were conflicts when the same jars of different versions were used for projects that added this project as a dependency. But if I don't export with the dependencies, at run time this library project throws NoClassDefFound errors for its dependencies. So what is the right way to do this? If I don't export the dependencies with my jar, is there a way that the project using this library project could download those jars for the library project? If that is how I would do it, then wouldn't it mean that the project using this project must be using maven too? It won't be a good practice as whoever uses this library should be able to use whatever the build tools they want. I am pretty new to maven. Please advice.
It is largely to taste, some open source projects do both, providing with for and without so that it is easier to use in larger projects.
If you are using Maven for your other projects you can, when you declare a dependency on this library tell maven to exclude some of its component jars.

netbeans builds Maven projects jar file without dependent jar files

Could anyone would be so kind and explain how to make a single jar file with all maven dependencies in Netbeans Maven Java Application? In Eclipse user can Export to Runnable jar file and select Package required libraries into generated JAR, so all dependencies within project comes in created jar file. In Netbeans there is no such option.
I have checked other answers, but the only thing i understand is that I have to add code to Build.xml file which is not even in the project.
The last time I had to do this I used the Maven Shade Plugin. It allows you to create a single JAR file and also handles dependency clashes.
A simpler solution (which doesn't handle dependency clashes) is to use the Maven Assembly Plugin.
Note that these are pure Maven solutions which should work in any IDE.

Add maven project and its dependencies to my project in Eclipse, best workflow

I am new to Maven, and this is a general workflow question. I have Eclipse Mars, and have added subclipse and m2e to it. I checked out a Java maven project (for instance MyLib), and can run its classes inside Eclipse.
Now I want to create my own project (for instance MyProject) that will use MyLib's classes. I right-clicked on MyProject/Properties/Projects and added all the subfolders from MyLib. However, this does not add MyLib's Maven dependencies to with it. I could manually look for all the jars in the .m2 folder and add them, but this sounds like the wrong workflow to me.
I have taken a look at http://maven.apache.org/guides/getting-started/, but the tool and Eclipse with its plugins and possibilities add quite to the learning-curve.
What would be the best workflow to be able to access MyLib (with its dependencies) in MyProject in Eclipse?
Do I have to convert MyProject to a maven project (Configure/Convert to Maven Project) and then (somehow) add a MyLib reference to pom.xml ? If so, how would the pom.xml have to be modified?
If this would be the right way, won't that download MyLib all over again?
EDIT: What I tried is to click on the pom.xml of MyLib, and create a new Module element in it, which then I called MyLib-MyProject. But this still does not add the depenendies of MyLib, and of course this would only be a temporary solution, because the same problem would appear again if I need another library via Maven, for example MyLib2, that also should be referenced from MyProject.
You could just add MyLib to the build path of MyProject.
Right Click Project->Properties->Java Build Path->Projects Tab->Add...
If you have trouble with dependencies, then you could compile MyLib into a jar, and add it as a library to the other project.
Right Click Project->Properties->Java Build Path->Libraries->Add JARs...
(or external jars if it's not in the workspace)
Here is a crunchify tutorial on creating a jar from a maven project.
The best way to achieve that is to use myLib as it was a normal maven project, on the mvn repository.
Of course I don't ask you to upload it on the global public repository, you should take a look at some nice proxy that allows you to have private repositories, like Nexus, Archiva, ...
I recommend you to read about Archiva, it's the easy one.
https://archiva.apache.org/index.cgi

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.

Maven/Eclipse plugin: easiest way to have new Maven project have dependency on legacy non-Maven project?

I created a new Maven project in Eclipse. This was working fine until I needed to add a dependency to another Eclipse project, a legacy utility project, which does not have a pom.xml, and does not have the directory structure of a typical Maven project. (It has the directory structure of a typical eclipse Java project). This other project is in the same Eclipse workspace as the Maven project.
In looking at other posts on this, it seems that usually the solution is to build the jar for the other project and install it in Maven. However I am actively modifying code in the utility project while writing code in the Maven project, so I can't just install a jar once to satisfy the dependency.
What is the easiest way to handle this so that I can code simultaneously in both projects, and also get maven to build cleanly? (Of course Eclipse can build just fine with just a project dependency.)
UPDATE
Using the Build Helper plugin to add the utility projects source folder to my pom was a viable path to the solution, but then I needed to update all the dependencies of the utility project into my new Mavne project, which started to make the whole process too time consuming (and also not really the chain of dependencies I wanted). I think that if I would have added all the dependencies, then Build Helper suggestion would have worked.
For now, I built the utility project jar and installed it into maven. Which turned out to be the the quickest solution. I will try to Mavenize the utility project, without modifying its structure (as suggested by FrVaBe below), and see if I can link the poms afterward.
I am going to keep this question open until I have a full solution which can be reported back, since I assume this is a problem others will have (trying to integrate legacy projects with new maven projects).
For the development time you can add the dependency as a System Dependency. It will be resolved by the file path (which can be the path to your utility.jar file under development) in this case.
It is added as describe in the link above, e.g.:
<dependencies>
<dependency>
<groupId>my-utility</groupId>
<artifactId>my-utility</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${somewhere}/lib/my-utility.jar</systemPath>
</dependency>
</dependencies>
The maven handling of System dependencies is sometimes special. E.g. they will not be included in war-packages! Therefore, when you are finished I would strongly recommend to install your utility library to the maven respository or to deploy it to a repository manager (Nexus/Artifactory).
You can add utility project's src folder to your working project in eclipse. For your development purpose.
right click on Working project
go to properties and choose java build path
go to source tab
Add your utility project src folder to that.
Later you can install your jar as maven dependency.

Categories