I've a simple program build in IntelliJ and using maven that uses the dependency io.netty. I've added to my POM file:
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.0.Beta1</version>
</dependency>
In order to compile and get a jar file I usually do:
Clean
Compile
Package
However I noticed that the dependency is not added to the jar, neither existing in the target folder (Or in any of it's sub folders) or added to the resources folder like usually happens.
In order to have the io.netty library to be added to the jar I have tried:
Setting the scope to provided and to compile.
Re-importing the pom file.
Deleting io.netty folder in the .m2/repository/ folder.
I have several other libraries linked including:
mysql-connector-java
slf4j-simple
trove4j
Thanks for reading.
For some odd reason I had changed my maven configuration a while ago. While I had not added any new libraries, the old ones still had their classes laying around therefor still being added to the jar.
I solved this issue by changing the build in my pom to:
<build>
<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.domain.Program</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Used as reference: http://mkyong.com/maven/create-a-fat-jar-file-maven-assembly-plugin/
Maven doesn't package all dependencies into a jar by default. You can use the assembly plugin to build a "jar with dependencies, as seen here:
How can I create an executable JAR with dependencies using Maven?
Related
I'm using maven to build a jar-with-dependencies jar. One of the vendor jar files in the project has similarly named classes with different case. For example aM.class and am.class.
When the build creates the "jar-with-dependencies" jar file, only aM.class is present. am.class appears to have been overwritten or skipped. The same thing happens for every similarly named classes files.
We have a couple work-arounds, but I'm curious why the maven build is behaving this way. Thanks
My pom:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.1</version>
<configuration>
<archive>
<manifest>
<mainClass>com.mytest.MyTest</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>
<dependencies>
<dependency>
<groupId>com.bloomberglp</groupId>
<artifactId>blpapi3</artifactId>
<version>3.10.1</version>
</dependency>
</dependencies>
Same class conflict issue will happen with maven-assembly-plugin, this plugin extracts all dependency jars into raw classes, and group it together.It works in project with less dependencies only, for large project with many dependencies, it will cause Java class name conflict issue, check here.
Better way to use maven-shade-plugin if you have many jars with potentially could conflict. You can also you relocating feature in shade plugin to explicitly avoid conflicts
I have a Maven project with a number of sub modules. Some of these sub modules are packaged as jar that are deployed to a Nexus Maven repository.
The problem I have is that the packaged jar references the parent pom which is not necessarily deployed.
Is there a way for Maven to deploy the effective pom instead of the pom.xml?
You need to be perfectly aware of the consequences of what you want to do: the effective POM will also contain your current settings (content of settings.xml), thereby possibly publicly exposing whatever passwords you have hard-coded in there. A better solution would be just to deploy the parent POM.
However, if you really want to go down that path, you can have the following configuration:
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-help-plugin</artifactId>
<version>2.1.1</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>effective-pom</goal>
</goals>
<configuration>
<output>${project.build.outputDirectory}/META-INF/maven/${project.groupId}/${project.artifactId}/pom.xml</output>
</configuration>
</execution>
</executions>
</plugin>
This tells the maven-jar-plugin not to add the Maven descriptor pom.xml and pom.properties to the jar. Instead, the pom.xml is generated by the maven-help-plugin and its effective-pom goal.
If you want the pom.properties file also, you will need to create it manually with the maven-antrun-plugin.
I'm trying to create automated solution for building with maven. My vision is to have a Maven build, which creates JAR file from my project and then just copies all the dependencies as JARs to some sub-directory in "target" folder.
I do not want to use Shade or Assembly (so I do not want to extract the content of other JARs and include it in one "super-JAR", because the project is more complicated and it breaks when I'm including all the JARs in one file).
How can I do such build POM?
I don't see here any problem. Just create maven pom.xml with <packaging>jar</packaging>
By default it should not pack into your jar all dependent libraries.
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
Related with your latest comment, use this plugin to add the main class in the manifest:
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.test.YourMainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
You can use the maven-assembly-plugin to create a .zip file from a selection of other files in your directory. I have used this method to create distribution zip files for a project. There are several examples available in the maven-assembly-plugin documentation.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Including dependencies in a jar with Maven
I am beginner in maven and would like to build a jar containing dependency jars in it using maven. But, I could not do so. Kindly help me about it.
If you're trying to create a single jar that contains your application and it's required libraries, there are two ways (that I know of) to do that. The first is One-Jar, which uses a special classloader to allow the nesting of jars. The second is UberJar, (or Shade), which explodes the included libraries and puts all the classes in the top-level jar.
I should also mention that UberJar and Shade are plugins for Maven1 and Maven2 respectively. As mentioned below, you can also use the assembly plugin (which in reality is much more powerful, but much harder to properly configure).
Use shade plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
You would to include the following in your pom.xml file.
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>MainClass with the packages</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
I have a project in NetBeans6.9.1.
It works fine from inside the IDE.
But when I try to run the jar, that NetBeans has automatically created under the dist directory, I get a NoClassDefFoundError
for classes inside my project. What am I doing wrong? Should I be using Ant or something (don't know Ant)
In eclipse I do a "create runnable jar", and the jar runs without issues. Is there something equivalent in NetBeans?
UPDATE: In the dist/myJar, I extracted the jar, and in the manifest, the current path and the root path of my project were missing. I added them manually, and re-created the jar from command line. And it works. But why doesn't NetBeans add these in the classpath of the jar's manifest.I do not understand
UPDATE 2 I found the problem. I think this is a serious NetBeans bug. I had done refactoring and changed the package names from myPackage.model to mypackage.model. But NetBeans did not do it correctly. It indeed changed the name of the package to mypackage as seen in the tree navigator, but the package name inside the file remained as myPackage.
The program executed fine inside the IDE and no errors were reported (although all the classes were declaring as belonging to myPackage and in the tree they were under mypackage), but when I tried to run the jar inside the dist directory I got a class not found exception. Today I noticed that the class was reported as myPackage/model instead of mypackage/model. I looked into the classes and the refactoring completely meshed up everything. I mannually changed the package name from inside my classes from myPackage to mypackage, and corrected all the imports (which were importing myPackage). Is this a known issue of NetBeans????
Thanks
With NetBeans your Java project will be either Maven or Ant based. What you want to do is to create a "fat" jar that has all it's dependencies included. For Maven you can add the following lines to your pom to accomlish that:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>package.and.MainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
So for Maven it's a two-step thing:
You must tell Maven to produce a "shaded" (including dependencies) jar,
and you have to specify the main class (the one containing the static main(..)method to run.
If you're using Ant you might want have a look a this blog post.
I've been having this same prolem, and all I can say is that it seems to be a glitch with NetBeans 6.9.1, as it worked in 6.8 and 6.9.
For convenience's sake, you can open it in WinZip or WinRar and just change the manifest file from there without having to jar it yourself. That is what I do.
I solved following this post in an external blog:
You have to add two plugins (maven-assembly-plugin and maven-jar-plugin) to the build section of your pom.xml file. You build section would be similar to this:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>(select version, i.e: 2.3)</version>
<executions>
<execution>
<goals>
<goal>attached</goal>
</goals>
<phase>package</phase>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>your.main.Class</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>(select version, i.e: 2.3.2)</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>your.main.Class</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
NetBeans creates a self sufficient JAR file for your project when you execute the "Clean and Build" command from Menu or Tool Button.
Ideally the classpath of the Jar file shall not contain root folder of the project, NetBeans adds library JARS if you have added them to the project in the Class-Path property in the manifest.mf file automatically.
If you have any custom requirement you can even modify the manifest.mf file which gets included in the built JAR. The manifest.mf file is available from the Files panel in the project root directory.
For making the JAR executable with the java -jar command you shall specify the Main class in the project configuration. (If the project was created through NetBeans project wizard then this class is already defined, otherwise we have to do that manually)
Specify the main class through Menu option as follows:
File > Project Properties > Run (Category) > Main Class (textbox)
Please give details of your project like
Is the project created from NetBeans New Project wizard?
Is the project Ant based or Maven based?
Is the project free form project created from existing source?
This additional information will help me answer the question in specific details. Hope this has helped.
with regards
Tushar