This question already has answers here:
Generate test-jar along with jar file in test package
(2 answers)
Closed 8 years ago.
I have maven project with main and test subfolders under folder src.
However, when I build maven jar, I dont see test files in jar file (I do see files that were there in src/main; I dont see test files from src/test)
[xx#localhost target]$ jar tvf cbm.jar | grep *Test*
[xx#localhost target]$
Here is my pom.xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<!-- https://maven.apache.org/plugins/maven-assembly-plugin/examples/index.html -->
<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>Main</mainClass>
</manifest>
</archive>
<finalName>afloat</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
EDIT
#azurefrog: I am having issues in production and need to run the test cases on production machine.
It is normal behavior. Maven crate jar from src (only production code).
If you want add test .class to jar you have to create your own maven plugin.
Check this
Guide for maven plugin development
Maven does not package test classes. What you would have to do is copy the maven project to the production machine and run mvn test from that machine.
The reason Maven behaves like this is that everything under src/test is not supposed to be used in production. It's test code, resources, etc. One common use of this is to put configuration files under src/test/resources such as Spring context files. If these files made it into the official jar file, it would be very bad in many, many cases.
That said, what you can do if you want to run test code is follow this process:
Build a maven project that uses this one as a dependency.
Write a some test code, either as JUnit with a standalone JUnit runner, or a simple class with a main method.
Use the Maven assembly plugin to generate a jar that has all dependencies in it. This GitHub project of mine uses the assembly plugin to build an uberjar for a groovy script.
Related
I have Java Maven project in Eclipse. Sometimes I need to copy binary with all libraries and configuration files to live system. Is it right place to ask Maven about it?
Should I use package goal for this reason. Should I use any plugin? Where I should define remote system sftp logins and paths?
For building a .jar with all dependencies and a main-class attribute, you can use the maven plugin maven-assembly-plugin
You can use it like this in your pom-file:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>package.mainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<goals>
<goal>single</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
</plugin>
Inside the <build> and plugins of course :)
See How to Create an Executable JAR with Maven with pros & cons for the different options:
Manual Configuration
Apache Maven Assembly Plugin
Apache Maven Shade Plugin
One Jar Maven Plugin
Spring Boot Maven Plugin
Web Application with Executable Tomcat
Use the Wagon Maven Plugin to copy/upload the so created "executable" JAR wherever you want.
I am having an executable jar ,when I try to run this jar ,it is giving me an error saying one of the bean is not available.But however, if i unzip this jar file
and replace any of the class file and zip it back, the application starts without any issue.No matter which class is replaced, after zipping it back, the jar starts without any issue. Can anyone please tell what could be the issue here?
Please note that When the jar is created in local, it doesnt have any issue. But when I download the jar that had been uploaded to jenkins and try running it, It is giving this issue. As mentioned earlier if I replace any class file inside this non working jar and zip it back, even this jar starts working
Your char in local environment cold be placed in such manner that the executable jar can "see" the necessary dependencies.
You need to package all necessary dependencies into your executable jar in order to make it run when you move your jar to other location. You can utilize maven-assembly-plugin to solve this, just add the following in your maven's pom.xml in your build setup:
<!-- BUILD SETUP -->
<build>
<plugins>
....
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>path.to.your.MainClass</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>
</plugins>
</build>
Hope it helps!
NOTE: Please, before marking this question as a duplicate make sure you know the difference between executable JAR and fully executable SpringBoot JAR.
The official Spring Boot documentation describes how to build fully executable JAR. Then generated JAR file can be linked from /etc/init.d/ and started/stopped/restarted/statused as a normal unix service without additional scripts or tools like JSVC.
But the generated JAR contains all libraries and can be big enough in size (in my case 70Mb+).
I want to generate such fully executable JAR without libraries, but then to be able to run it as SystemV service on Linux and link external libraries (JARs) somehow.
UPDATE
I want to reduce the artifact size in order to speed up deploy->test->fix cycle. Sometimes I'm working via mobile network and big file size can decrease my job speed dramatically.
In case there is no a simple configuration property or a profile or a command line option I would use a kind of hack.
At the beginning, I can generate a build containing all dependencies.
Then I can unzip it and move all libraries to a special folder.
Then I need to pack it again as fully executable somehow and run with pointing to the folder with libraries.
I don't think this can be done with jar utility because file utility recognizes fully executable jar as data
$ file fully-executable.jar
file fully-executable: data
unlike the usual jar
$ file usual.jar
usual.jar: Java Jar file data (zip)
You may want to consider using Spring Boot Thin Launcher. It creates a jar file with your application code but none of its dependencies. It adds a special thin launcher that knows how to resolve your application's dependences from a remote Maven repository or from a local cache when the jar is executed. Judging by the description of what you want to do, you'd utilise the local cache option.
The configuration of Spring Boot's Maven plugin to produce a fully executable jar that uses the thin launcher looks like this:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot.experimental</groupId>
<artifactId>spring-boot-thin-layout</artifactId>
<version>1.0.3.RELEASE</version>
</dependency>
</dependencies>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>
I can use this setting to create spring boot jar without dependency jars.
Copy dependency jars to dist/lib
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeScope>runtime</includeScope>
<outputDirectory>${project.build.directory}/dist/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
Specify 'lib' as classpath prefix, so MAINFEST.MF will be created like this:
Class-Path:lib/httpcore-nio-4.4.14.jar lib/guava-24.1.1-jre.jar...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.xxx.MyMainClass</mainClass>
</manifest>
</archive>
<outputDirectory>${project.build.directory}/dist</outputDirectory>
</configuration>
</plugin>
Let spring boot plugin includes dependency which is not existing, will cause spring boot jar exclude all dependencies.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<layout>ZIP</layout>
<includes>
<include>
<groupId>not-exist-in-my-project</groupId>
<artifactId>not-exist-in-my-project</artifactId>
</include>
</includes>
<outputDirectory>${project.build.directory}/dist</outputDirectory>
</configuration>
</plugin>
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?
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