Export jar Eclipse Maven Project with just non-provided dependencies - java

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

Related

how does a Java program find its Maven packages?

I have a Java program in IntelliJ which has a pom.xml and uses Maven. The packages were downloaded and currently they are found by IntelliJ.
I'm a little confused though because the Maven repository is not part of the CLASSPATH as far as I can tell. So does IntelliJ just do a bit of magic where it looks into its Maven repository to find the packages? (I think that IntelliJ has its own Maven repo. I separately have Maven 3 installed, but I think it isn't using it.)
But more generally: If you build a JAR using Maven then I guess it will put the dependencies in the JAR where the Java program can find them, so there won't be a problem. But if you just run a Java program directly, do you need to add the Maven repository to your classpath or does something else happen?
Thanks for any information you can provide to lessen my confusion :)
When you start the program from IntelliJ using a runtime configuration for your main() method IntelliJ constructs the classpath from all the project dependencies. You can see this in the Run window, the first log line is the java command used to start the main(). It's a long line but it usually looks similar to:
java -javaagent:/opt/idea/idea-IC-173.3727.127/lib/idea_rt.jar=40165:/opt/infra/idea/idea-IC-173.3727.127/bin -Dfile.encoding=UTF-8 -classpath /home/ [...]
IntelliJ constructs the -classpath argument adding both the module target directory and the Maven dependencies referenced from the local Maven repository.
When you package the project using Maven mvn clean package usually it becomes a standalone JAR with just your code (unless you changed the defaults). Now you have a few choices how to provide dependencies needed to start your main():
Provide them using -classpath parameter just like IntelliJ.
Add maven-shade-plugin and use shade goal to the build a runnable Uber JAR. This creates a fat JAR which doesn't require -classpath.
Use some other Maven plugin to perform point 2 e.g. Spring Boot spring-boot:repackage goal.
All the required dependencies, defined in the pom.xml file(s), are downloaded from Maven Central (or others if configured) to the local Maven repository. That repository is located at <user home>/.m2/repository.
Maven generates/calculates a dependency tree to know all the required dependencies for the project. (you can also dump that tree with the command mvn dependency:tree. I always pipe the result to a file, because the tree can be large mvn dependency:tree > deptree.txt). Maven put them all on the classpath when executing a maven command like mvn compile
IntelliJ also use/calculate the dependency tree and add all the jar files to the projects classpath (point to the files in the <user home>/.m2/repository folder). You can see them all in the list with External Libraries, and they will be used / on the classpath for compilation and running the application.
When building a JAR file the dependencies are NOT added to the JAR. Only the bytecode (java classes) and resources from your own project are packaged into the JAR file. (Source files can also be packaged if you configure that)
By adding a Maven plugin (maven-shade-plugin) you can configure your project to also pack dependencies into the JAR. SpringBoot projects also will do that.

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.

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

How to add a jar package to project in eclipse?

I have added to my pom.xml a section that specifies the mainClass and allows it to essentially create an executable jar. I have included a bunch of dependencies that maven manages as well. It executes fine, but fails to run when it gets to a section of code that needs to know the location of a jar package that was made inhouse by somebody (i.e., not from Maven). In the project in eclipse I had put the jar in src/lib and my code is in src/main/java. I had to select properties and Java Build Path and specify there the src/lib location for the jar to get it to even compile. However, trying to run java -jar name.jar has it fail and complain because it fails to import the classes from the src/lib jar. Since it is not a maven thing, how to I make sure this is a dependency for this project and that it is seen on the project's classpath?
The thing with maven is that maven has to control all of the dependencies and that includes this jar you want to reference. That doesn't mean that you have to build that other jar using maven, you could mvn install it in your local repository or use a tool like Artifactory to put it in a private remote repository. I know that installing regular jars via Artifactory creates a pom file for the jar and from then on you can treat the jar like any other maven dependency.

How to create a jar file for the eclipse plugin project that should be able to download and run without eclipse software?

I m doing an eclipse plugin project to create an IDE. I need to create a jar file for the plugin project in which i have four plugin packages which was created by me. Now I need these to be created as a single jar file and the user should be able to download the jar file and run my plugin project without the eclipse software.
EDIT-
You cannot run Eclipse plugin outside of Eclipse, because you need the Equinox runtime container. you could run a plugin using the eclipse executable, and as an application, see:
http://wiki.eclipse.org/FAQ_How_do_I_create_an_application%3F
You're effectively creating an an org.eclipse.core.runtime.applications extension point.
You could also publish a plugin as part of an Eclipse application and then export it as an executable so that it can be run aside from Eclipse. This still bundles the Equinox runtime and plugin together though.
Also, check out "Running it outside of Eclipse" section here.
-END of EDIT
Generally, all you need to run an executable jar file is the jvm (java) and your code with all the classpath dependencies. You can use "Runnable Jar Export Wizard" available in Eclipse IDE when you right-click your project.
You can put all the dependencies inside your jar (for example you can create a lib directory in your project and put all your dependency jars inside). Also you will need to specify the dependecy location in the MANIFEST file that will be generated for your executable jar (if you use the wizard the MANIFEST file will contain your dependencies).
To run your executable jar you will need to execute:
java -jar jar-file
Good Luck!

Categories