I am trying to create a jar file from my project in Eclipse IDE. I was trying to run mvn package on my pom.xml that looks like this:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.javacodegeeks.snippets.enterprise</groupId>
<artifactId>springexample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.javacodegeeks.snippets.enterprise.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version> 3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>com.rapplogic</groupId>
<artifactId>xbee-api</artifactId>
<version> 0.9</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>jfreechart</groupId>
<artifactId>jfreechart</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>jcommon</groupId>
<artifactId>jcommon</artifactId>
<version>0.9.5</version>
</dependency>
<dependency>
<groupId>org.graphstream</groupId>
<artifactId>gs-core</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.graphstream</groupId>
<artifactId>gs-ui</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.graphstream</groupId>
<artifactId>gs-algo</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.9.Final</version>
</dependency>
<dependency>
<groupId>org.rxtx</groupId>
<artifactId>rxtx</artifactId>
<version>2.1.7</version>
</dependency>
<dependency>
<groupId>org.bidib.jbidib.org.qbang.rxtx</groupId>
<artifactId>rxtxcomm</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
</project>
Afterwards, I have gotten a file MyProject-0.0.1-SNAPSHOT.jar in the target directory of the project. Double clicking on that jar in Eclipse gave me a pop-up Windows window saying "Java Virtual Machine Launcher - A java Exception has occured!". To find more about what a possible problem could be, I have copied that jar on the desktop and run java -jar MyProject-0.0.1-SNAPSHOT.jar from that. After that, I got a following error:
Assuming from the error log, I tried to search if I have included org.hibernate.cfg.NamingStrategy in my Maven repository, and yes, I do have it there. After that I got stuck. Any ideas? Thanks.
You need to create an executable fat jar: one that contains your jar dependencies as well. See: Building a fat jar using maven
You have to include all the dependencies on the classpath that are required by your code.
You can naively do this with a shell script that manually adds everything to java -cp parameter, but this is brittle and a complete fail when something changes.
The simplest way to do this is to use the shade plugin to make an uber-jar that includes all the dependencies.
Note that this won't work correctly if any of the dependencies are cryptographically signed, unfortunately the signing stuff is in the MANIFEST file in such a way that it applies globally to the entire .jar and not individual classes or packages. You would have to manually remove the signatures from the uber jar.
Related
I have a project which works perfectly on eclipse, but when I try to run it on terminal using jar file then I get an error. Basically, the aim is to create a fat jar with all dependencies included.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.scryAnalytics</groupId>
<artifactId>NLPAnnotationController</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>NLPAnnotationController</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hadoop.version>2.6.0-cdh5.5.1</hadoop.version>
<jdk.version>1.7</jdk.version>
<hbase.version>1.0.0-cdh5.5.1</hbase.version>
</properties>
<repositories>
<repository>
<id>cloudera</id>
<url>https://repository.cloudera.com/cloudera/cloudera-repos</url>
</repository>
<repository>
<id>maven</id>
<url>https://repository.apache.org/content/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.6.0-cdh5.5.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-auth</artifactId>
<version>2.6.0-cdh5.5.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.6.0-cdh5.5.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-yarn-api</artifactId>
<version>2.6.0-cdh5.5.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-app</artifactId>
<version>2.6.0-cdh5.5.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-common</artifactId>
<version>2.6.0-cdh5.5.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-core</artifactId>
<version>2.6.0-cdh5.5.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-yarn-client</artifactId>
<version>2.6.0-cdh5.5.1</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.0.0-cdh5.5.1</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-common</artifactId>
<version>1.0.0-cdh5.5.1</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.0.0-cdh5.5.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>uk.ac.gate</groupId>
<artifactId>gate-core</artifactId>
<version>8.1</version>
</dependency>
<dependency>
<groupId>uk.ac.gate</groupId>
<artifactId>gate-compiler-jdt</artifactId>
<version>4.3.2-P20140317-1600</version>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.8</version>
</dependency>
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<version>${jdk.version}</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.8.3</version>
</dependency>
<dependency>
<groupId>com.scryAnalytics</groupId>
<artifactId>NLPGeneric</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>NER</groupId>
<artifactId>NER</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<finalName>NLPAnnotationController</finalName>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.scryAnalytics.NLPAnnotationController.GateNLPJob</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>conf</directory>
</resource>
</resources>
</build>
and when I run it I get the error shown below.
java -jar target/NLPAnnotationController-jar-with-dependencies.jar -inputTable posts -outputTable posts -batchId 1
Error: Invalid or corrupt jarfile target/NLPAnnotationController-jar-with-dependencies.jar
As suggested in a comment, double-check to see if the JAR file is really corrupt using jar tv <filename.jar>.
There are a couple of common causes for corrupt JAR files:
The file was corrupted by copying / transferring it the wrong way; e.g. using FTP in ASCII rather than BINARY mode.
The file system filled up while you were writing the file.
This is the ZIP64 problem. The original ZIP file format did not support more than 4GB in size. This was addressed by an extension to the format, but versions of Java prior to Java 7 did not support that.
Reference: https://blogs.oracle.com/xuemingshen/entry/zip64_support_for_4g_zipfile
There is a hard limit of 65535 entries in a ZIP file. Unfortunately, some Maven plugins does not entirely understand this limit. When you attempt to generate a JAR with too many entries, the result is a corrupt JAR file.
References:
Invalid or corrupt JAR File built by Maven shade plugin
Create a large zip assembly with maven-assembly-plugin
The first three explanations are unlikely in your case, for various reasons. That leaves a Maven plugin problem. One of the Q&As that I found said that this was fixed in version 2.5 of the maven-assembly-plugin. But you appear to be using a later version. So maybe this is not the problem after all.
Anyhow:
Check that you are using a Java 7 or later JRE or JDK on your build and execution platforms.
Try updating the version of the maven assembly plugin in your POM to the latest available.
I'm trying to create .jar file in my server(CentOS) using Maven. It is successfully compiling and generates the .jar file. But when I execute it, it shows the following error. This questions may look like "previously asked" but, I tried all the suggestions from here. What I believe is in my POM.xml have some issue, but I'm not able to find. If someone can verify the POM.xml it would be helpful.
Command:
$ java -jar target/ListDescCrawler-1.1-SNAPSHOT.jar
Error:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
My POM.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>edu.uci.ics.crawler4j</groupId>
<artifactId>ListDescCrawler</artifactId>
<packaging>jar</packaging>
<version>1.1-SNAPSHOT</version>
<name>SanListDescCrawlerHandu</name>
<url>http://maven.apache.org</url>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>edu.uci.ics.crawler4j.example.MainController</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.james</groupId>
<artifactId>apache-mime4j-core</artifactId>
<version>0.7</version>
</dependency>
<dependency>
<groupId>org.apache.james</groupId>
<artifactId>apache-mime4j-dom</artifactId>
<version>0.7</version>
</dependency>
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>de.l3s.boilerpipe</groupId>
<artifactId>boilerpipe</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>fluent-hc</artifactId>
<version>4.3.6</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-stax-api_1.0_spec</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.6</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.3.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.3.6</version>
</dependency>
<dependency>
<groupId>com.sleepycat</groupId>
<artifactId>je</artifactId>
<version>4.0.92</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.8.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>com.drewnoakes</groupId>
<artifactId>metadata-extractor</artifactId>
<version>2.4.0-beta-1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
<dependency>
<groupId>org.ccil.cowan.tagsoup</groupId>
<artifactId>tagsoup</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-app</artifactId>
<version>1.6</version>
</dependency>
</dependencies>
</project>
The MANIFEST.MF file in the generated .jar file:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: root
Build-Jdk: 1.7.0_91
Main-Class: edu.uci.ics.crawler4j.example.MainController
Class-Path: apache-mime4j-core-0.7.jar apache-mime4j-dom-0.7.jar asm-3
.1.jar boilerpipe-1.1.0.jar commons-codec-1.6.jar commons-compress-1.
3.jar commons-logging-1.1.3.jar fluent-hc-4.3.6.jar geronimo-stax-api
_1.0_spec-1.0.1.jar httpclient-4.3.6.jar httpcore-4.3.3.jar httpmime-
4.3.6.jar je-4.0.92.jar jsoup-1.8.1.jar log4j-1.2.17.jar metadata-ext
ractor-2.4.0-beta-1.jar tagsoup-1.2.1.jar tika-app-1.6.jar tika-parse
rs-1.6.jar tika-core-1.6.jar vorbis-java-tika-0.6.jar netcdf-4.2.20.j
ar unidataCommon-4.2.20.jar jcip-annotations-1.0.jar commons-httpclie
nt-3.1.jar jmatio-1.0.jar xz-1.5.jar pdfbox-1.8.6.jar fontbox-1.8.6.j
ar jempbox-1.8.6.jar bcmail-jdk15-1.45.jar bcprov-jdk15-1.45.jar poi-
3.11-beta2.jar poi-scratchpad-3.11-beta2.jar poi-ooxml-3.11-beta2.jar
poi-ooxml-schemas-3.11-beta2.jar xmlbeans-2.6.0.jar asm-debug-all-4.
1.jar isoparser-1.0.2.jar aspectjrt-1.8.0.jar rome-1.0.jar jdom-1.0.j
ar vorbis-java-core-0.6.jar juniversalchardet-1.0.3.jar jhighlight-1.
0.jar java-libpst-0.8.1.jar tika-serialization-1.6.jar tika-xmp-1.6.j
ar xmpcore-5.1.2.jar slf4j-log4j12-1.5.6.jar slf4j-api-1.5.6.jar gson
-1.7.1.jar
Commands executed:
mvn clean compile
mvn clean package
java -jar target/ListDescCrawler-1.1-SNAPSHOT.jar
There are many library files are added to the project including mysql-connector-java-5.1.34.jar. But still the error is showing. I checked in SOF for a solution and tried many suggestions from here, but nothing fixed my issue.
I checked MANIFEST.MF in the generated .jar file and it looks fine.
Other points to note is, its working fine from Eclipse(without Maven)
is there any mistake in my POM.xml?
The suggestions I tried:
dependency/scope I tried all the possible values
Copied mysql-connector-java-5.1.34.jar in the lib folder and changed the POM.xml accordingly
and may other suggestions but nothing helped.
At last the things started to work with the following POM.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>edu.uci.ics.crawler4j</groupId>
<artifactId>ListDescCrawler</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>SanListDescCrawlerHandu</name>
<url>http://maven.apache.org</url>
<properties>
<jdk.version>1.7</jdk.version>
<junit.version>4.11</junit.version>
<apache-mime4j-core.version>0.7</apache-mime4j-core.version>
<apache-mime4j-dom.version>0.7</apache-mime4j-dom.version>
<asm.version>3.1</asm.version>
<boilerpipe.version>1.1.0</boilerpipe.version>
<commons-codec.version>1.6</commons-codec.version>
<commons-compress.version>1.3</commons-compress.version>
<commons-logging.version>1.1.3</commons-logging.version>
<fluent-hc.version>4.3.6</fluent-hc.version>
<geronimo-stax-api_1.0_spec.version>1.0.1</geronimo-stax-api_1.0_spec.version>
<httpclient.version>4.3.6</httpclient.version>
<httpcore.version>4.3.3</httpcore.version>
<httpmime.version>4.3.6</httpmime.version>
<je.version>4.0.92</je.version>
<jsoup.version>1.8.1</jsoup.version>
<log4j.version>1.2.17</log4j.version>
<metadata-extractor.version>2.4.0-beta-1</metadata-extractor.version>
<mysql-connector-java.version>5.1.34</mysql-connector-java.version>
<tagsoup.version>1.2.1</tagsoup.version>
<tika-app.version>1.6</tika-app.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.james</groupId>
<artifactId>apache-mime4j-core</artifactId>
<version>${apache-mime4j-core.version}</version>
</dependency>
<dependency>
<groupId>org.apache.james</groupId>
<artifactId>apache-mime4j-dom</artifactId>
<version>${apache-mime4j-dom.version}</version>
</dependency>
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>${asm.version}</version>
</dependency>
<dependency>
<groupId>de.l3s.boilerpipe</groupId>
<artifactId>boilerpipe</artifactId>
<version>${boilerpipe.version}</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${commons-codec.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>${commons-compress.version}</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>${commons-logging.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>fluent-hc</artifactId>
<version>${fluent-hc.version}</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-stax-api_1.0_spec</artifactId>
<version>${geronimo-stax-api_1.0_spec.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpclient.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>${httpcore.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>${httpmime.version}</version>
</dependency>
<dependency>
<groupId>com.sleepycat</groupId>
<artifactId>je</artifactId>
<version>${je.version}</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>${jsoup.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>com.drewnoakes</groupId>
<artifactId>metadata-extractor</artifactId>
<version>${metadata-extractor.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-java.version}</version>
</dependency>
<dependency>
<groupId>org.ccil.cowan.tagsoup</groupId>
<artifactId>tagsoup</artifactId>
<version>${tagsoup.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-app</artifactId>
<version>${tika-app.version}</version>
</dependency>
</dependencies>
<build>
<finalName>ListDescCrawler</finalName>
<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>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>edu.uci.ics.crawler4j.example.MainController</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Eventhough your class path in MANIFEST.MF contains jar that you are dependent it does not mean that they actually on your class path when you run your project. You have two option here either put all the dependent jar in the same directory of your runnable jar, or create a shaded jar.
A shaded (fat) jar contains all of your dependencies. You can create shaded jar using maven. Here is an example.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>${project.build.finalName}-with-dependencies</finalName>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass> edu.uci.ics.crawler4j.example.MainController </mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
I am building android project with maven. But I found when I call getChildFragmentManager in v4.Fragment, it shows an error that this method is not found. I doubt that the support-v4 package is not newer enough because if I using ant to build my project everything works just fine.
However I can't get the newer support-v4 to be included into my project.
Here is my pom.xml
<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelversion>4.0.0</modelversion>
<groupid>com.meishixing</groupid>
<artifactid>crazysight</artifactid>
<version>1.0.0-snapshot</version>
<packaging>apk</packaging>
<name>crazysight</name>
<properties>
<project.build.sourceencoding>utf-8</project.build.sourceencoding>
<fest-assert.version>1.0.4</fest-assert.version>
<junit.version>4.10</junit.version>
<robolectric.version>2.0</robolectric.version>
<intellij.annotations.version>12.0</intellij.annotations.version>
</properties>
<dependencies>
<dependency>
<groupid>com.google.android</groupid>
<artifactid>android</artifactid>
<version>4.1.1.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupid>com.viewpagerindicator</groupid>
<artifactid>library</artifactid>
<version>2.4.1</version>
<type>apklib</type>
</dependency>
<dependency>
<groupid>com.tjerkw</groupid>
<artifactid>slideexpandablelistview-library</artifactid>
<version>1.1.0-snapshot</version>
<type>apklib</type>
</dependency>
<dependency>
<groupid>com.meishixing</groupid>
<artifactid>android-async-http</artifactid>
<version>1.4.2</version>
<type>jar</type>
</dependency>
<dependency>
<groupid>com.meishixing</groupid>
<artifactid>baidu-locationp</artifactid>
<version>3.3</version>
<type>jar</type>
</dependency>
<dependency>
<groupid>com.google.code.gson</groupid>
<artifactid>gson</artifactid>
<version>2.2.4</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupid>org.apache.commons</groupid>
<artifactid>commons-lang3</artifactid>
<version>3.1</version>
</dependency>
<dependency>
<groupid>com.github.chrisbanes.pulltorefresh</groupid>
<artifactid>library</artifactid>
<version>2.1.1</version>
<type>apklib</type>
</dependency>
<dependency>
<groupid>com.novoda.imageloader</groupid>
<artifactid>imageloader-core</artifactid>
<version>1.5.8</version>
</dependency>
</dependencies>
<build>
<finalname>${project.artifactid}</finalname>
<sourcedirectory>src</sourcedirectory>
<plugins>
<plugin>
<groupid>com.jayway.maven.plugins.android.generation2</groupid>
<artifactid>android-maven-plugin</artifactid>
<version>3.6.2-snapshot</version>
<configuration>
<sdk>
<platform>17</platform>
</sdk>
<manifest>
<debuggable>true</debuggable>
</manifest>
</configuration>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
</project>
I've been stumped by this for hours (which is why I also added a bounty), but here's how I finally resolved it.
First of all, check your project's classpath dependancies using
mvn dependency:build-classpath
I could immediately see the problem in the output:
<home>/.m2\repository\android\extras\android-support\v4\android-support-v4.jar
<home>/.m2\repository\android\platforms\android\android-18\android-android-18.jar
<home>/.m2\repository\com\actionbarsherlock\actionbarsherlock\4.4.0\actionbarsherlock-4.4.0.apklib
<home>/.m2\repository\com\google\android\support-v4\r7\support-v4-r7.jar
<... more jars>
There are two support jars included, and the support-v4-r7.jar overrides my newer android-support-v4.jar.
The older one was being loaded by ActionBarSherlock in my project (in your project the culprit might be any of the apklib dependencies), so I excluded it in my project pom:
<dependency>
<groupId>com.actionbarsherlock</groupId>
<artifactId>actionbarsherlock</artifactId>
<version>4.4.0</version>
<type>apklib</type>
<exclusions>
<exclusion>
<groupId>com.google.android</groupId>
<artifactId>support-v4</artifactId>
</exclusion>
</exclusions>
</dependency>
Hopefully this helps someone.
How can I create an executable jar with dependencies in sub folder in jar using Maven?
The following pom.xml will make a executable jar with the dependencies in the root but what if I wanted all the dependencies in a sub-folder in the jar so its easy if someone else would want to look in the jar. How can I change the following pom.xml file:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.xxxx</groupId>
<artifactId>DRBP</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>DRBP</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java-version>1.7</java-version>
<springframework-version>3.2.1.RELEASE</springframework-version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${springframework-version}</version>
</dependency>
<!--
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${springframework-version}</version>
</dependency>
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${springframework-version}</version>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.5</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
<dependency>
<groupId>net.sf.jt400</groupId>
<artifactId>jt400-full</artifactId>
<version>4.7.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>DRBP</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>attached</goal>
</goals>
<phase>package</phase>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>org.xxxx.batch.MainBatch</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
You want to look at the Maven Shade plugin.
If you want a full web app in a single jar there are a couple of options:
Do what DropWizard does which is an example maven shade + jetty.
The Tomcat version +2 Maven plugin
Winstone
I've an application that uses Maven to build a JAR with the maven-assembly-plugin.
The project includes a list of dependencies and one of these is another Maven project.
I'm developing with Eclipse and when I run the project everything works fine. When I build with the Maven goal assembly:assembly, it makes the JAR but when I run the JAR it gives me this error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method configure(Map<String,String>) of type SMTPMailService must
override a superclass method
at rey.sto.utils.mail.SMTPMailService.configure(SMTPMailService.java:89)
at com.ppl_sftp.transfer.FileTransfer.startTransfer(FileTransfer.java:232)
at com.ppl_sftp.transfer.MainApp.main(MainApp.java:33)
Here is the pom.xml file of the main project:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org /2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ppl-sftp</groupId>
<artifactId>transfer</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>A Camel Route</name>
<url>http://www.myorganization.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<!-- logging -->
<!-- testing -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>2.10.0</version>
<type>jar</type>
<optional>false</optional>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.6</version>
<type>jar</type>
<optional>false</optional>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.6</version>
<type>jar</type>
<optional>false</optional>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
<type>jar</type>
<optional>false</optional>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.7.2</version>
<type>jar</type>
<optional>false</optional>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test</artifactId>
<version>2.10.0</version>
<type>jar</type>
<scope>test</scope>
<optional>false</optional>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-ftp</artifactId>
<version>2.9.2</version>
<type>jar</type>
<optional>false</optional>
</dependency>
<dependency>
<groupId>my-utils</groupId>
<artifactId>my-utils</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.4.5</version>
</dependency>
</dependencies>
<build>
<defaultGoal>install</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<!-- allows the route to be ran via 'mvn camel:run' -->
<plugin>
<groupId>org.apache.camel</groupId>
<artifactId>camel-maven-plugin</artifactId>
<version>2.10.0</version>
</plugin>
<!-- Allows the example to be run via 'mvn compile exec:java' -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>com.ppl_sftp.transfer.MainApp</mainClass>
<includePluginDependencies>false</includePluginDependencies>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.ppl_sftp.transfer.MainApp</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
and this is the pom of the my-utils project dependency:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>rey-sto-utils</groupId>
<artifactId>rey-sto-utils</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j</artifactId>
<version>2.0-beta2</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
<dependency>
<groupId>ojdbc6</groupId>
<artifactId>ojdbc6</artifactId>
<version>6</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.4.5</version>
</dependency>
</dependencies>
The problem is related to the Java mail dependency. As you can see I've used com.sun.mail but I saw some other different libraries that correspond to javax.mail.
Maybe I'm missing something in my poms, such as configuration parameters or plugins.
I've also tried to put in my local repository a mail.jar that works fine in other projects but I get the same error.
The "Unresolved compilation problem" comes from a class file in your utils project that Eclipse has compiled, but where Eclipse was unable to compile it properly.
The reason you are seeing different results from Eclipse and Maven is that they are using different versions of the class file. Eclipse is doing workspace resolution, so it's using the version of SMTPMailService in utils/target/classes. Maven is resolving the utils jar from your local Maven repository, which was put there by running mvn install while you had compile errors in Eclipse.
Try running mvn clean install in the utils project (clean to remove any class files generated by Eclipse, install to replace the jar in your local repository). Then rebuild your assembly.