I have a project in javaFX and I'm using ControllerFX Library , the problem here in the .jar file it's necessery to keep .jar file of the library with my .jar project.
is there a way to combine .jar of library with .jar of project using intellij idea ?
does Maven help in this situation? (I don't know exactly what maven can do)
thanks for answers
The jars that you use with your project are called as the dependent jars. You can bundle them while creating a jar of your own project.
Intellij Approach
File
|
Project Structure
|
Artifacts
|
create new artifact choose --> jar --> From modules with dependencies.
Next goto
Build
|
Build artifacts --> choose your artifact
Maven Approach
Create a Maven project and keep the packaging as jar.
Add the executable jar plugin and add your Main class details and your dependent jars
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
Related
i am using netbeans 8.2 to create a simple java project. i've chosen Maven-Java Application. I've created a simple hello world java class. I've selected the java class as the main class from Properties -> Run and if i run the project from Netbeans, it print the hello world. After that, i do "Clean" and "Build with dependencies" the editor create a target folder with the jar file. When i go to that folder and execute "java -jar XXX.jar", i get the no main manifest attribute error. Do I have to manually update the POM.xml file? Am I missing a step?
I am answering my own question.
Apparently you have to manually update the pom.xml file. I've added the dependency for maven-jar-plugin and added below section to the pom.xml file.
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.mypackage.XXX</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
I'm coding a maven project with eclipse 2018.09 under java 11 and I've a problem with the maven jar creation. When I clean package the project, it delivers me a jar but no dependencies are added into and i sometimes have warning in eclipse like:
classpath entry junit(for example) will not be exported it may result a ClassNotFoundException.
Which is in fact what's happening when i launch my jar project.
Thanks.
it delivers me a jar but no dependencies are added into [it]
it is totally normal. By default, when Maven builds a jar, it does not add any dependencies in it, but only the .class and resources of your current project.
When you run your programm you want it to find your dependencies otherwise you will face ClassNotFoundException. So you must configure your classpath to reference the dependencies.
1- if you want to run you programm from your local computer with Maven, use the exec Maven plugin with the <java> goal defined in your pom like explained here: https://www.mojohaus.org/exec-maven-plugin/usage.html#Java_goal
alternatively you can run it from a launcher in your IDE. The IDE will build the classpath for you and the classpath will corectly contain your dependencies.
2- if you want to run from the command line on any computer, you have to copy all of you dependencies in one directory (using Maven's dependency plugin mvn dependency:copy) and run you jar like this:
java -cp myProgram.jar:dependencyDirectory/* com.blabla.MainClass
(beware the use of ';' or ':' and '/' or '\' depending on Linux/Windows)
3- as an alternative you can run your jar with java -jar myprogram.jar but only if it contains a correct MANIFEST.MF where the location of all the dependencies are hardcoded.
My advice is to target solution 1 or 2 first.
PS: you can also create "fat jars" or "uber jars" containing your dependencies but I would advise you do not target this solution at first.
You can simply add this to your pom.xml (under the < plugins > tag):
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>App</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
Remember to change the mainclass to your entrypoint (which class the static void main(string[args]) is).
Now when you run the command mvn clean install there will be a jar in the targets folder with name yourproject-version-SNAPSHOT-jar-with-dependencies.jar
I followed the example in Building a fat jar using maven and now I can run the following to build/test and install my jars.
mvn clean compile install
However, install now takes a lot longer because we are now building a fat jar. Is it possible to have two versions of install where one just builds jars without dependencies and the other does that and in addition builds the fat jar, like:
mvn clean compile install
mvn clean compile install-fatjar
I know install-fatjar is not a valid phase but just want to give an idea of what I'm trying to accomplish, i.e. a conditional install where the fat jar is built only when an option is provided.
Create a profile for the fat jar and configure the maven assembly plugin to create the fat jar in this profile.
For example use this profile:
<profiles>
<profile>
<id>fatjar</id>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Then you can build it by activating the profile
mvn -P fatjar clean install
where fatjar is the profile id.
Multiple profiles can also be activated
mvn -P fatjar,release install
Normally we don't use the fat-jar (or, uber-jar) as the main artifact.
You can make use of Assembly or Shade plugin to create a variation of the artifact (with a different classifier) which is a uber-jar.
It is strongly recommended that you still keep your "normal" artifact as is. If you want the uber-jar only occasionally, put the use of assembly/shade plugin for "uber-jar" creation in a profile, and activate the profile whenever you want. However, this should still generate you an "extra" uber-jar instead of making your main artifact a uber-jar.
I am developing a Java maven project with Eclipse and want to export a jar that includes all referenced libraries. These referenced libraries fall into one of these two categories:
They are explicit (or implicit) dependencies in the pom.xml
I have some libraries not available as maven artifacts and have put them in /lib (and added them to the build path in Eclipse)
There's the maven-assembly-plugin, which works fine for 1). However, I'm unable to find a maven plugin that also includes non-maven-dependencies, e.g. "all jars in /lib".
Then there's the Eclipse FatJar plugin, which sort of works, but hasn't been updated since 2009, so it seems unmaintained. Also I prefer an export method that I can directly specify in the pom.xml.
Can anyone point me to a maven plugin or the like to export all referenced libraries, including those from case 2) ? That only needs to involve putting them in the jar and referencing them in the manifest's classpath.
Thanks!
I think the best way to handle this is to include your custom libs into a local maven repository. Now you can inlcude your libraries as maven dependencies and you can export all your dependencies specified in your pom with the maven-assembly-plugin.
Here is a tutorial, how to put your libs into a local repository in maven to use it in your pom. http://www.mkyong.com/maven/how-to-include-library-manully-into-maven-local-repository/
And in your pom.xml:
<!-- setup jar manifest to executable with dependencies -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>your.main.class</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
This looks like a task for Tycho. It is a set of maven plugins that allows to create eclipse plugins with maven. Tycho considers manifest entries as build dependencies.
However I'm not aware of how to package all those dependencies in a single jar. This may also be conflicting with the osgi spec. But if you wish to ignore osgi you could just try the jar-with-dependencies assembly.
I am using netbeans to work on a project. I am using svn, so that I can commit to newer versions and revert to an older version in case I mess up. Now I want to create a jar file of this project but the build tab in the project properties does not display the packaging option, it only displays one option and that is of 'compile'.
If I create a new project (Java Application) it does show the packaging option and then I can easily create a jar file, but this maven project that I am working on does not work like this.
Please suggest me a way to create jar file out of my maven project.
Thanks,
Based on #yatskevich answer, you could go to your NetBeans Project Properties > Actions and add package to the Execute Goals of the following Actions:
Build project
Clean and Build project
Build with dependencies
Plus any other you feel should also package
I did this on NetBeans 7.2.1
PS: this will create the JAR on every change you make, so choose wisely where to add package. I don't mind it building a JAR for small projects on every build.
Open cmd (if you are on Windows) or any shell (if you are on Linux)
Navigate to your project directory (use cd command)
Run mvn clean package there.
Your jar will be in <project dir>/target.
Include maven-assembly-plugin plugin in your .pom file. It will instruct Maven to assemble your application with all it's dependencies.
Later when you will build your Netbeans project you will see your newly builded jar with all it's dependencies.
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.my.class</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Check out to learn more about creating Jar file with Maven: https://javatutorial.net/create-java-jar-file-with-maven