netbeans builds Maven projects jar file without dependent jar files - java

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.

Related

Java Maven -> quickls add folder of jars as depencency

my Java Project uses a "/libs" folder containing ~100 .jar files. Almost all of them are not in an official maven repository.
1.) In the moment I manually added to whole folder to the classpath with my Eclipse IDE. That enables to compile and run the App using the Eclipse IDE. But if I want to maven to compile and create jar-with-dependencies, maven of course does not know about the "/libs" folder.
2.) I know that I can add a jar file to my local maven repo with mvn install:install-file but this would take a very long time because I would also have to open every jar and find the whole package name to insert as '-DgroupId' and the Name of the Main Class to add as '-DartifactId'
3.) My Questions:
3.1) Is there an easy way to let maven just include all jars in a folder like I did with my Eclipse IDE? I know that would break the principle of maven that every jar is identified with group and artifact id, but it would be a quick solution.
3.2) If it is not possible to add a folder with jars as a dependency in maven, is there a faster way to add a jar file into a local repo. It would be easier if there is a maven command where groupId and artifactId are automatically discovered by the jar that I do not have to open every jar file and find the Main Class and its classpath
Quick answer: No.
In the past, I have written a script for that because there is not support in Maven for this.

Export jar Eclipse Maven Project with just non-provided dependencies

I'm building a Java project using the Maven Project package in Eclipse Java EE IDE. I'm using different dependencies, some of them will be provided by the system where the java program will be run on, others will not. I added the provided scope tag into the pom file to the one I know are provided by the system and I now I would like to export a runnable .jar. Eclipse exports the .jar package with all the dependencies (provided and not) but there's a way to have the runnable .jar file with just the not provided dependencies packaged?
If you exporting a jar with eclipse, eclipse Jar-Packager will be used and the runnable jar will contain all dependencies. Eclipse jar-builder don't know about pom.xml.
In your case you should use mvn build, for example:
mvn clean package

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.

Create a single jar that will contain compiled classes and javadoc without Maven

When I build a jar with Eclipse, sometime I'd like to include source or JavaDoc with the .class files in a single .jar file. I found how to do that with Maven. Is it possible to do it just using Eclipse?
TIA,
You can import a maven project into Eclipse. That's one way. Just import your maven project and perform the maven steps you do to create you jar file.
You can also export a Java project, which includes the sources and all that you mentioned using Eclipse under the File -> Export option. You will see a wizard that allows you to choose the target (war, jar, ear, ...) and the resources that should be included.
Mode details: http://www.codejava.net/ides/eclipse/how-to-create-jar-file-in-eclipse

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