How to create single jar with dependencies in maven - java

I am trying to create single fat jar using maven assembly plugin but somehow after running maven clean install it give 2 jars, one is client-1.0-SNAPSHOT.jar and other one is client-1.0-SNAPSHOT-jar-with-dependencies.jar.
I am only interested to create a jar with dependencies so dont know why the other jar is also creating by this assemble plugin.
Can someone tell me how to eliminate this jar Or should i use some other maven command rather than clean install ?
<build>
<finalName>${project.artifactId}-${project.version}</finalName>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<excludes>
<exclude>*.properties</exclude>
</excludes>
</resource>
</resources>
<plugins>
<!-- download source code in Eclipse, best practice -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>false</downloadJavadocs>
</configuration>
</plugin>
<!-- Set a compiler level -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
<!-- Maven Assembly Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<!-- get all project dependencies -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
-->
<!-- MainClass in mainfest make a executable jar -->
<archive>
<manifest>
<mainClass>com.app.MainApp</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- bind to the packaging phase -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<!-- overwrite! -->
<overwrite>true</overwrite>
<outputDirectory>${project.basedir}/target</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/</directory>
<includes>
<include>*.properties</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Related

Corrupt JARS in zip using maven

I have maven project that i want to package in a zip file including the jar binaries(in a folder /jars). After the packaging, I've unzipped and extracted the jar file but get the below error when i invoke this:
PS C:\Users\kenochrome\Downloads> jar xf service-0.1.jar
java.util.zip.ZipException: invalid literal/length code
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:139)
at java.util.zip.ZipInputStream.read(ZipInputStream.java:142)
at sun.tools.jar.Main.extractFile(Main.java:715)
at sun.tools.jar.Main.extract(Main.java:678)
at sun.tools.jar.Main.run(Main.java:191)
at sun.tools.jar.Main.main(Main.java:904)
Below is a snippet of parent and child poms.
Parent POM
<build>
<finalName>release_1.0</finalName>
<resources>
<resource>
<directory>${basedir}/hello/world/00006/jars</directory>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.7</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptor>assembly/bin.xml</descriptor>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Child POM
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>jar</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>scala-compile</id>
<phase>process-resources</phase>
<goals>
<goal>add-source</goal>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>scala-test-compile</id>
<phase>process-test-resources</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<finalName>service-${project.version}</finalName>
<outputDirectory>../../jars</outputDirectory>
<resources>
<resource>
<directory>jars</directory>
<filtering>false</filtering>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
</dependencies>
</project>
I'm sorry if the information is not adequate or if this question is not correctly formatted.
Many thanks for going through this post and much appreciated in advance.
Was looking through another forum and found out that the issue that I was having was using the <lineEnding>unix</lineEnding> element in my descriptor assembly xml.

java.util.MissingResourceException: Can't find bundle for base name messages, locale en_US Floreant POS

I have downloaded the FLoreant POS open source code, using mvn clean install it got build successfully. But when I am trying to run the application its not able to read the messages.properties file even though everything is in classpath below is the structure of project and classpath snapshot. not getting any clue why input Stream is coming as null in code
private static class ResourceControl extends ResourceBundle.Control {
#Override
public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload) throws IllegalAccessException,
InstantiationException, IOException {
String bundlename = toBundleName(baseName, locale);
String resName = toResourceName(bundlename, "properties"); //$NON-NLS-1$
InputStream stream = loader.getResourceAsStream(resName);
return new PropertyResourceBundle(new InputStreamReader(stream, "UTF-8")); //$NON-NLS-1$
}
}
maven build plugin code
<scm>
<connection>scm:svn:http://svn.code.sf.net/p/floreantpos/code/trunk</connection>
</scm>
<build>
<sourceDirectory>src</sourceDirectory>
<finalName>floreantpos</finalName>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<directory>resources</directory>
</resource>
<resource>
<directory>filters</directory>
<filtering>true</filtering>
<includes>
<include>*.properties</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
<configuration>
<doCheck>false</doCheck>
<doUpdate>false</doUpdate>
<providerImplementations>
<svn>javasvn</svn>
</providerImplementations>
</configuration>
<dependencies>
<dependency>
<groupId>com.google.code.maven-scm-provider-svnjava</groupId>
<artifactId>maven-scm-provider-svnjava</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.tmatesoft.svnkit</groupId>
<artifactId>svnkit</artifactId>
<version>1.8.5</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.floreantpos.main.Main</mainClass>
</manifest>
<manifestEntries>
<Class-Path>config/ i18n/</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptor>resources/META-INF/mvn-assembly.xml</descriptor>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>prepare</id>
<phase>validate</phase>
<configuration>
<target>
<echo message="prepare phase" />
<unzip src="database/derby-server/posdb.zip" dest="${project.build.directory}" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<phase>package</phase>
<configuration>
<target>
<zip destfile="${project.build.directory}/${project.build.finalName}-1.4-build${buildNumber}.zip" basedir="${project.build.directory}/${project.build.finalName}-bin/${project.build.finalName}"></zip>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself. -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-antrun-plugin
</artifactId>
<versionRange>
[1.6,)
</versionRange>
<goals>
<goal>run</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
Just copy "i18n" directory from src to target/floreantpos-bin directory.
Run your application from target/floreantpos-bin directory using
java -jar floreantpos.jar

java.lang.NoClassDefFoundError in uber jar

I am creating a jar with dependencies. Here is the relevant section in my POM
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>${project.basedir}</directory>
<includes>
<include>lib/*.jar</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>path.to.main.Main</mainClass>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
i can build the jar just fine with mvn clean install. however when i run it
java -jar myProject-0.0.1-SNAPSHOT-jar-with-dependencies.jar, i get a java.lang.NoClassDefFoundError on a class which comes from the jar within the lib folder.
Note that i had included this jar by
<resource>
<directory>${project.basedir}</directory>
<includes>
<include>lib/*.jar</include>
</includes>
</resource>
so why am i getting the error? what am i doing wrong? Also, when i unzip the jar, i see the lib folder and the jar within. So why cant the myProject-0.0.1-SNAPSHOT-jar-with-dependencies.jar find it?
Update
i added
<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>
and on mvn clean install i see this on the console
[INFO] Copying my3rdParty.jar to path/to/project/target/lib/biomedical-my3rdParty-0.0.1.jar
Check your inclusions in the built JAR. Typically you'll run into this if you have a duplicate of this class included on your classpath (in your fat JAR). See if the class in question was double-included. Either that, or what the above user said (it's not structured correctly in the built JAR).

Maven manifest file issue

I have been struggling with this for some time. I have a Maven project that builds and packages just fine with one problem. I have a jar I am using that is NOT in my companies version of NEXUS. I added the jar to my .m2 repository through this command line command:
mvn install:install-file -Dfile=c:/u/lib/terajdbc4-15.00.00.20.jar -DgroupId=com.teradata -DartifactId=terajdbc4 -Dversion=15.00.00.20 -Dpackaging=jar
This jar will NOT append to the manifest file when the jar is built. So, when I need this jar, the code dies because it can't find the resource it's looking for (the teradata driver)
Is there any way to add this single jar to the manifest file IN ADDITION TO the other jars I'm already adding to the manifest?
Here is the build section from the parent pom file:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/Crunchify</outputDirectory>
<resources>
<resource>
<directory>resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/Crunchify/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>ap.invalert.handler.InvAlertHandler</mainClass>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
<finalName>Crunchify/Crunchify</finalName>
</configuration>
</plugin>
</plugins>
</build>

How to bundle the war with different configuration in maven

i need to change multiple xml file while build a war using maven like below example
I am able to do the required change in the target folder. But It is not copied in to the war file
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.3</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
<id>dse xml replacer</id>
<configuration>
<file>
${project.artifactId}/target/${project.artifactId}-${version}/WEB-INF/example.xml
</file>
<replacements>
<replacement>
<token>reloadingEnabled=".*"</token>
<value>reloadingEnabled="false"</value>
</replacement>
</replacements>
</configuration>
</execution>
</plugin>
Try the maven-war-plugin, like:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<webResources>
<resource>
<!-- this is relative to the pom.xml directory -->
<directory>resource2</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
More info https://maven.apache.org/plugins/maven-war-plugin/examples/adding-filtering-webresources.html

Categories

Resources