Manually creating a deployable JAR for Liferay - maven

I created a liferay workspace in gradle format and it basically only contains a theme and a TemplateContextContributor-module.
Now I want to build a maven "wrapper" around both artifacts to make them compatible with some other maven-processes/-plugins while keeping the original gradle structure. I dont want to use the liferay-maven-plugin or maven-tools to build those artifacts, because it seems to behave differently from the gradle/gulp toolset when it comes to compiling scss for example.
So I created some POMs from scratch for
Theme
TemplateContextContributor-Module
First off I will take about the mechanism for the theme, which is already working:
That wrapper uses the maven-war-plugin to bundle the contents of the build/-folder, where the previously built gradle artifact resides, into a WAR-file that can be deployed by Liferay without problems.
theme pom.xml:
<properties>
<src.dir>src</src.dir>
<com.liferay.portal.tools.theme.builder.outputDir>build</com.liferay.portal.tools.theme.builder.outputDir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
[...]
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<webResources>
<resource>
<directory>${com.liferay.portal.tools.theme.builder.outputDir}</directory>
<excludes>
<exclude>**/*.sass-cache/</exclude>
</excludes>
</resource>
</webResources>
</configuration>
</plugin>
However, I am having difficulties creating a OSGI-Compatible JAR-File for the module contents. It seems that only the META-INF/MANIFEST.MF does not contain the right information and I seemingly cannot generate it in a way that Liferay (or OSGI) understands.
this is the module pom.xml dependencies and plugins that I tried:
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.scr.ds-annotations</artifactId>
<version>1.2.10</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.liferay</groupId>
<artifactId>com.liferay.gradle.plugins</artifactId>
<version>3.9.9</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.liferay.portal</groupId>
<artifactId>com.liferay.portal.kernel</artifactId>
<version>2.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.service.component.annotations</artifactId>
<version>1.3.0</version>
<scope>provided</scope>
</dependency>
[...]
<plugin>
<groupId>biz.aQute.bnd</groupId>
<artifactId>bnd-maven-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<goals>
<goal>bnd-process</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>biz.aQute.bnd</groupId>
<artifactId>biz.aQute.bndlib</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>com.liferay</groupId>
<artifactId>com.liferay.ant.bnd</artifactId>
<version>2.0.48</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-scr-plugin</artifactId>
<version>1.25.0</version>
<executions>
<execution>
<id>generate-scr-scrdescriptor</id>
<goals>
<goal>scr</goal>
</goals>
</execution>
</executions>
</plugin>
I was able to create a JAR using the above but its' META-INF/MANIFEST.MF is not identical to the one produced by the gradle build:
I guess that's why Liferay does not deploy it. The log says "processing module xxx ....", but that never ends and the module does not work in Liferay.
These are the plugins I have tried in different combinations so far:
maven-build-plugin
maven-scr-plugin
maven-jar-plugin
maven-war-plugin
maven-compiler-plugin
Any help in creating a liferay-deployable module JAR would be great.

I'm not sure why you're manually building a maven wrapper for the Template Context Contributor. The Liferay (blade) samples are available for Liferay-workspace, pure Gradle as well as for Maven. I'd just go with the standard and not worry about re-inventing the wheel.
To make this answer self-contained: The current pom.xml listed in the Template Context Contributor plugin is:
<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>
<artifactId>template-context-contributor</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<parent>
<groupId>blade</groupId>
<artifactId>parent.bnd.bundle.plugin</artifactId>
<version>1.0.0</version>
<relativePath>../../parent.bnd.bundle.plugin</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>com.liferay.portal</groupId>
<artifactId>com.liferay.portal.kernel</artifactId>
<version>2.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.portlet</groupId>
<artifactId>portlet-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.service.component.annotations</artifactId>
<version>1.3.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>com.liferay.blade.template.context.contributor-${project.version}</finalName>
</build>
</project>

Related

java.lang.NoSuchMethodError: org.apache.hadoop.conf.Configuration.reloadExistingConfigurations()V

It looks like I am again stuck on the running a packaged spark app jar using spark submit. Following is my pom 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/maven-v4_0_0.xsd">
<parent>
<artifactId>oneview-forecaster</artifactId>
<groupId>com.dataxu.oneview.forecast</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>forecaster</artifactId>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-scala_${scala.binary.version}</artifactId>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming_${scala.binary.version}</artifactId>
<version>${spark.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_${scala.binary.version}</artifactId>
<version>${spark.version}</version>
<!--<scope>provided</scope>-->
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.spark/spark-hive -->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-hive_2.11</artifactId>
<version>2.2.0</version>
<!--<scope>provided</scope>-->
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-aws</artifactId>
<version>2.8.3</version>
<!--<scope>provided</scope>-->
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.10.60</version>
</dependency>
<!-- https://mvnrepository.com/artifact/joda-time/joda-time -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.8.0</version>
<!--<scope>provided</scope>-->
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<testSourceDirectory>src/test/scala</testSourceDirectory>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>${scala-maven-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.dataxu.oneview.forecaster.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>
</plugins>
</build>
Following is a simple snippet of code which fetches data from s3 location and prints it:
def getS3Data(path: String): Map[String, Any] = {
println("spark session start.........")
val spark = getSparkSession()
val configTxt = spark.sparkContext.textFile(path)
.collect().reduce(_ + _)
val mapper = new ObjectMapper
mapper.registerModule(DefaultScalaModule)
mapper.readValue(configTxt, classOf[Map[String, String]])
}
When I run it from intellij, everything works fine. the log is clear and looks good. However, when I package it using mvn package and try to run it using spark submit, I end up getting the following error at the .collect.reduce(_ + _). Following is the error I encounter:
"main" java.lang.NoSuchMethodError: org.apache.hadoop.conf.Configuration.reloadExistingConfigurations()V
at org.apache.hadoop.fs.s3a.S3AFileSystem.addDeprecatedKeys(S3AFileSystem.java:181)
at org.apache.hadoop.fs.s3a.S3AFileSystem.<clinit>(S3AFileSystem.java:185)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
...
I am not understanding which dependency was not packaged or what might be the issue as I did set the versions correctly expecting the hadoop aws should have all of them.
Any help will be appreciated.
The dependencies between hadoop and AWS JDK are very sensitive, and you should stick to using the correct versions that your hadoop dependency version was built with.
The first problem you need to solve is pick one version of Hadoop. I see you're mixing versions 2.8.3 and 2.8.0.
When I look at the dependency tree for org.apache.hadoop:hadoop-aws:2.8.0, I see that it is built against version 1.10.6 of the AWS SDK (same for hadoop-aws:2.8.3).
This is probably what's causing mismatches (you're mixing incompatible versions). So:
Choose the version of hadoop you want to use
Include hadoop-aws with the version compatible with your hadoop
Remove other dependencies, or only include them with versions matching the one compatible with your hadoop version.
In case anybody else is still stumbling on this error... it took me a while to find out, but check if your project has a dependency (direct or transitive) on the package org.apache.avro/avro-tools.
It was brought into my code by a transitive dependency.
Its problem is that it ships with a copy of org.apache.hadoop.conf.Configuration
that is much older than all current versions of hadoop, so it may end up being the one picked up in the classpath.
In my scala project, I just had to exclude it with
ExclusionRule("org.apache.avro","avro-tools")
and the error (finally!) disappear.
I am sure that the avro-tools coders had some good reason to include a copy of a file that belongs to another package (hadoop-common), I was really surprised to find it there and made me waste an entire day.
In my case, I was running a local Spark installation on a Cloudera edge node and was hitting this conflict (even though I made sure to download Spark with the correct hadoop binaries precompiled). I just went into my Spark home and moved the hadoop-common jar so it wouldn't be loaded:
mv ~/spark-2.4.4-bin-hadoop2.6/jars/hadoop-common-2.6.5.jar ~/spark-2.4.4-bin-hadoop2.6/jars/hadoop-common-2.6.5.jar.XXXXXX
After that, it ran... in local mode anyway.

Flexmojos-maven-plugin - How to configure multiple source mxml files to generate multiple swf files?

Current working code:
I am able to build a flex project using flexmojos-maven-plugin successfully. However, I can only provide one 'sourceFile' under my plugin configuration. Refer below my working pom.xml, which builds Main.mxml file correctly from 'src' directory. It generates the 'swf' file for Main.mxml successfully:
<?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.test</groupId>
<artifactId>TA_UI_Test2</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>swf</packaging>
<name>TA_UI_Test2 Flex</name>
<dependencies>
<dependency>
<groupId>com.adobe.flex.framework</groupId>
<artifactId>playerglobal</artifactId>
<version>10-3.3.0.4852</version>
<type>swc</type>
</dependency>
<dependency>
<groupId>com.adobe.flex.framework</groupId>
<artifactId>rpc</artifactId>
<version>4.5.1.21328</version>
<type>swc</type>
</dependency>
<dependency>
<groupId>com.adobe.flex.framework</groupId>
<artifactId>framework</artifactId>
<version>3.2.0.3958</version>
<type>swc</type>
</dependency>
<dependency>
<groupId>com.adobe.flex.framework</groupId>
<artifactId>mx</artifactId>
<version>4.5.0.19786</version>
<type>pom</type>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<groupId>org.sonatype.flexmojos</groupId>
<artifactId>flexmojos-maven-plugin</artifactId>
<version>3.8</version>
<extensions>true</extensions>
<configuration>
<sourceFile>Main.mxml</sourceFile>
<debug>true</debug>
<storepass/>
</configuration>
<dependencies>
<dependency>
<groupId>com.adobe.flex</groupId>
<artifactId>compiler</artifactId>
<version>3.2.0.3958</version>
<type>pom</type>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
Problem description:
Now I have got multiple other mxml files under my 'src' directory, i.e. all mxml files under one single 'src' directory. How do I add them in above plugin, which only expects one single name in sourceFile configuration?
Things I have tried:
I tried copy-pasting multiple blocks of flexmojos-maven-plugin, and each of them having different sourceFile specified. However, that does not help, because maven just generates the final 'swf' file from the last block of flexmojos-maven-plugin. For eg. if first plugin block has sourceFile Main.mxml, and second plugin block has sourceFile Secondary.mxml, then the 'swf' will be generated for Secondary.mxml only, not for Main.mxml.
Could you provide any other suggestions to generate individual swf files for respective mxml files using single pom/maven build?
After quite a lot of R&D, trying a few hacks, and based on valuable suggestion of Christofer Dutz on this forum, finally a clean solution is available for above problem. Here is the updated pom.xml, which will generate the individual swf files for provided source files:
<?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.test</groupId>
<artifactId>TA_UI_Test2</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>swf</packaging>
<name>TA_UI_Test2 Flex</name>
<dependencies>
<dependency>
<groupId>com.adobe.flex.framework</groupId>
<artifactId>playerglobal</artifactId>
<version>10-3.3.0.4852</version>
<type>swc</type>
</dependency>
<dependency>
<groupId>com.adobe.flex.framework</groupId>
<artifactId>rpc</artifactId>
<version>4.5.1.21328</version>
<type>swc</type>
</dependency>
<dependency>
<groupId>com.adobe.flex.framework</groupId>
<artifactId>framework</artifactId>
<version>3.2.0.3958</version>
<type>swc</type>
</dependency>
<dependency>
<groupId>com.adobe.flex.framework</groupId>
<artifactId>mx</artifactId>
<version>4.5.0.19786</version>
<type>pom</type>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<groupId>org.sonatype.flexmojos</groupId>
<artifactId>flexmojos-maven-plugin</artifactId>
<version>3.8</version>
<extensions>true</extensions>
<configuration>
<storepass></storepass>
</configuration>
<executions>
<execution>
<id>default-compile-swf</id> <!--DO NOT change this ID, else the plugin execution will fail.-->
<configuration>
<sourceFile>File1.mxml</sourceFile>
<debug>true</debug>
<output>${basedir}/target/File1.swf</output>
</configuration>
<goals>
<goal>compile-swf</goal>
</goals>
</execution>
<execution>
<id>default-compile-swf-2</id>
<configuration>
<sourceFile>File2.mxml</sourceFile>
<debug>true</debug>
<output>${basedir}/target/File2.swf</output>
</configuration>
<goals>
<goal>compile-swf</goal>
</goals>
</execution>
<execution>
<id>default-compile-swf-3</id>
<configuration>
<sourceFile>File3.mxml</sourceFile>
<debug>true</debug>
<output>${basedir}/target/File3.swf</output>
</configuration>
<goals>
<goal>compile-swf</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.adobe.flex</groupId>
<artifactId>compiler</artifactId>
<version>3.2.0.3958</version>
<type>pom</type>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
Important note: You have to provide the ID of any one of the executions as 'default-compile-swf', without which it would fail to identify the source file. For more information on the error which it will throw, refer to the link of the forum given above where I was discussing the same.
Hope this helps someone someday :)

Complete pom.xml for stormpath Web App with Java Servlet, JSP

There is a tutorial for stormpath (online user management). The pom.xml that is provided at https://stormpath.com/blog/java-webapp-instant-user-management#maven is a bit confusing.
pom.xml
4.0.0
com.stormpath.samples
stormpath-webapp-tutorial
0.1.0
war
com.stormpath.sdk
stormpath-servlet-plugin
1.0.RC3.1
javax.servlet
javax.servlet-api
3.0.1
provided
javax.servlet
jstl
1.2
ch.qos.logback
logback-classic
1.0.13
runtime
org.apache.tomcat.maven
tomcat7-maven-plugin
2.2
/
What kind of pom structure should this be? How would the complete and working pom.xml look like?
I am Stormpath's Java Developer Evangelist.
This section is in error in the blog. We are currently fixing it. I'll let you know when it's updated.
In the meantime, if you clone the Stormpath Java SDK at https://github.com/stormpath/stormpath-sdk-java.git, there's a fully functional servlet example in the examples/servlet folder. This has the proper pom.xml in it.
To build, you should be able to run:
mvn clean install
in the root folder of the project.
You can then drop examples/servlet/target/stormpath-sdk-examples-servlet-1.0.0.RC-SNAPSHOT.war into the container (like Tomcat) of your choice.
Feel free to drop us a line at: support#stormpath.com if you run into any trouble with this.
I ended up using this in my tutorial example. It works for me. Just add the <dependencies> part to the already existing default pom.xml of your project. Save the pom.xml and it will automatically download a bunch of .jar to your Libraries/Maven Dependencies.
<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>storm</groupId>
<artifactId>storm</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>com.stormpath.sdk</groupId>
<artifactId>stormpath-servlet-plugin</artifactId>
<version>1.0.RC9.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.13</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>

NoSuchFieldError: RESOURCE_PREFIX with a maven project using tess4j

tess4j is an OCR packed with native library, I made a maven project to test it,
I did add the installation path of maven to eclipse.
I added M2_HOME, MAVEN_HOME and JAVA_HOME env variable,
here is my parent pom
<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>fr.mssb.ongoing</groupId>
<artifactId>ongoing-parent</artifactId>
<packaging>pom</packaging>
<version>1.0</version>
<name>ongoing</name>
<modules>
<module>capcha-solver</module>
</modules>
<build>
<pluginManagement>
<plugins>
<!-- All project will be interpreted (source) and compiled (target) in java 7 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<!-- this will make eclipse:eclipse goal work and make the project Eclipse compatible -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
<classpathContainers>
<classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7</classpathContainer>
</classpathContainers>
<additionalBuildcommands>
<buildcommand>net.sf.eclipsecs.core.CheckstyleBuilder</buildcommand>
</additionalBuildcommands>
<additionalProjectnatures>
<projectnature>net.sf.eclipsecs.core.CheckstyleNature</projectnature>
</additionalProjectnatures>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<!-- All child pom will inherit those dependancies -->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
and here is my child pom
<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>
<parent>
<groupId>fr.mssb.ongoing</groupId>
<artifactId>ongoing-parent</artifactId>
<version>1.0</version>
</parent>
<groupId>fr.mssb.ongoing</groupId>
<artifactId>capcha-solver</artifactId>
<version>1.0</version>
<packaging>jar</packaging> <!-- I think this is useless -->
<name>A capcha solver based on terassec ocr</name>
<build>
<plugins>
<!-- autorun unit tests during maven compilation -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Xmx1024m -XX:MaxPermSize=256m -XX:-UseSplitVerifier</argLine>
<skipTests>-DskipTests</skipTests>
</configuration>
</plugin>
<!-- this should make the tesseract ocr native dll work without doing anything -->
<plugin>
<groupId>com.googlecode.mavennatives</groupId>
<artifactId>maven-nativedependencies-plugin</artifactId>
<version>0.0.7</version>
<executions>
<execution>
<id>unpacknatives</id>
<goals>
<goal>copy</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<!--
Log4j 2 is broken up in an API and an implementation (core), where the API
provides the interface that applications should code to. Strictly speaking
Log4j core is only needed at runtime and not at compile time.
However, below we list Log4j core as a compile time dependency to improve
the startup time for custom plugins.
-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.1</version>
</dependency>
<!--
Integration of tesseract OCR
-->
<dependency>
<groupId>net.sourceforge.tess4j</groupId>
<artifactId>tess4j</artifactId>
<version>1.4.1</version>
</dependency>
</dependencies>
</project>
and of course, the code (taken from tess4j example)
package test;
import java.io.File;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
/**
* Classe d'exemple.
*/
public class TesseractExample {
public static void main(String[] args) {
File imageFile = new File("C:\\DEV\\repo\\ongoing\\capcha-solver\\src\\test\\resources\\random.jpg");
Tesseract instance = Tesseract.getInstance(); // JNA Interface Mapping
// Tesseract1 instance = new Tesseract1(); // JNA Direct Mapping
try {
String result = instance.doOCR(imageFile);
System.out.println(result);
} catch (TesseractException e) {
System.err.println(e.getMessage());
}
}
}
When I lauch it I'm getting this exception
Exception in thread "main" java.lang.NoSuchFieldError: RESOURCE_PREFIX
at net.sourceforge.tess4j.util.LoadLibs.<clinit>(LoadLibs.java:60)
at net.sourceforge.tess4j.TessAPI.<clinit>(TessAPI.java:40)
at net.sourceforge.tess4j.Tesseract.init(Tesseract.java:303)
at net.sourceforge.tess4j.Tesseract.doOCR(Tesseract.java:239)
at net.sourceforge.tess4j.Tesseract.doOCR(Tesseract.java:188)
at net.sourceforge.tess4j.Tesseract.doOCR(Tesseract.java:172)
at test.TesseractExample.main(TesseractExample.java:19)
I don't know if this is tess4j related or a JNA/JNI problem, as you can see I have a plugin that "should" (never worked with DLLs before) make them work.
Also in the parent pom my plugin are betwen plugin managment tags, I think I should have put them betwen build tags, no?
Any idea?
Thanks.
There was 2 problems
1/ some dlls and files from tess4j had to be copied to the project root directory
2/ tess4j had a transitive dependancy toward com.sun.jna:jna:jar:3.0.9 conflicting with net.java.dev.jna:jna:jar:4.1.0 (also from tess4j) ecluding the 3.0.9 version makes everything work, the RESSOURCE_PREFIX error was coming from that
pom.xml for 32 bit version (you need a 32 bit JVM installed) which takes care of those 2 things, change win32-x86 to win32-x86-64 if you want to use this in 64 bits
<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>fr.mssb.ocr</groupId>
<artifactId>tesseractOcr</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>tesseract ocr project</name>
<build>
<plugins>
<!--
this extract the 32 bits dll and the tesseractdata folder to
the project root from tess4j.jar
-->
<plugin>
<groupId>org.apache.portals.jetspeed-2</groupId>
<artifactId>jetspeed-unpack-maven-plugin</artifactId>
<version>2.2.2</version>
<dependencies>
<dependency>
<groupId>net.sourceforge.tess4j</groupId>
<artifactId>tess4j</artifactId>
<version>1.4.1</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>unpack-step</id>
<phase>compile</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<unpack>
<artifact>net.sourceforge.tess4j:tess4j:jar</artifact>
<overwrite>true</overwrite>
<resources combine.children="append">
<resource>
<path>win32-x86</path>
<destination>../</destination>
<overwrite>true</overwrite>
<flat>true</flat>
<include>*</include>
</resource>
<resource>
<path>tessdata</path>
<destination>../tessdata</destination>
<overwrite>true</overwrite>
<flat>true</flat>
<include>*</include>
</resource>
<resource>
<path>tessdata/configs</path>
<destination>../tessdata/configs</destination>
<overwrite>true</overwrite>
<flat>true</flat>
<include>*</include>
</resource>
</resources>
</unpack>
<verbose>true</verbose>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>net.sourceforge.tess4j</groupId>
<artifactId>tess4j</artifactId>
<version>1.4.1</version>
<exclusions>
<exclusion>
<groupId>com.sun.jna</groupId>
<artifactId>jna</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
The child pom could be easily built without any problems and manually copying libs, this is not TESS4J related.
Anyway the jna 3.0.9 could be removed if not needed anymore: https://github.com/nguyenq/tess4j/issues/8
Still, all you have to do to run tess4j is the maven dependency:
<dependency>
<groupId>net.sourceforge.tess4j</groupId>
<artifactId>tess4j</artifactId>
<version>1.4.1</version>
</dependency>
and the correct use of the TESS4J-API, for example:
File imageFile = new File("C:\\random.png");
Tesseract instance = Tesseract.getInstance();
//In case you don't have your own tessdata, let it also be extracted for you
File tessDataFolder = LoadLibs.extractTessResources("tessdata");
//Set the tessdata path
instance.setDatapath(tessDataFolder.getAbsolutePath());
try {
String result = instance.doOCR(imageFile);
System.out.println(result);
} catch (TesseractException e) {
System.err.println(e.getMessage());
}
That's it!
The problem is caused by the conflict between net.java.dev.jna:jna and com.sun.jna:jna. Both jars contain a class com.sun.jna.Platform. Both jars are declared as tess4j dependencies. To solve this you can omit the second dependency in your pom:
<dependency>
<groupId>net.sourceforge.tess4j</groupId>
<artifactId>tess4j</artifactId>
<version>1.4.1</version>
<exclusions>
<exclusion>
<groupId>com.sun.jna</groupId>
<artifactId>jna</artifactId>
</exclusion>
</exclusions>
</dependency>
because the JNA version mismatch. you are using more than one version in class path library. just use one version of JNA.

maven project with jetty-maven-plugin and cdi not working

I have a maven project with dependencies for javaee7 jsf2.2 and cdi and jetty-maven-plugin.
The project works fine except for cdi. I cant figure out what configuration files are need and where to put them. The only example projects for this i could find where with jetty6 and jsf2.0 and even those would not work.
Here is my pom.xml and a screenshot of my filestructure.
<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">
<!-- pom.xml specification version -->
<modelVersion>4.0.0</modelVersion>
<!-- project settings -->
<groupId>de.beans</groupId>
<artifactId>cdi-test</artifactId>
<version>0.1</version>
<name>test</name>
<packaging>war</packaging>
<!-- project module dependencies -->
<dependencies>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.2.6</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.2.6</version>
</dependency>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet</artifactId>
<version>2.2.3.Final</version>
</dependency>
</dependencies>
<!-- project maven plugins -->
<build>
<plugins>
<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>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.0.v20140526</version>
<configuration>
<scanIntervalSeconds>1</scanIntervalSeconds>
<webApp>
<contextPath>/</contextPath>
</webApp>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
</configuration>
</plugin>
</plugins>
</build>
Any help, or a working example project would be greatly appreciated.
Edit:
i have updated my files as described in this post: stackoverflow answer
you can see the new file structure above.
Now i get the following error message:
INFO: WELD-000101: Transactional services not available. Injection of #Inject UserTransaction not available. Transactional observers will be invoked synchronously.
2014-07-21 09:15:40.682:WARN:oejuc.AbstractLifeCycle:main: FAILED org.eclipse.jetty.annotations.ServletContainerInitializersStarter#3278af54: java.lang.NoClassDefFoundError: org/apache/catalina/core/ApplicationContextFacade
I looked this error up and found that the missing class def is part of tomcat, which im not using.
a working example project would be really appreciated.
Edit2:
here is a github repository of my project.
github maven jetty cdi repo
Will be fixed in WELD 2.2.5.Final
https://issues.jboss.org/browse/WELD-1710
The error is caused by the combination of weld and the newest jetty. I changed jetty to 9.1.5 and everything works fine now. Thanks for all your suggestions.

Resources