JPA Entities showing as not enhanced when referenced from another JAR - java

I have a Maven Java project with a JAR containing my business logic and JPA persistence entities. Those entities are enhanced by a Maven plug-in at build time. I also have another project which has a dependency on that JAR. i.e.
EAR
|->JAR - with entities and persistence.xml
|->WAR - dependent on JAR above
When I access the entities from within the JAR everything works fine. However if I try and access the entities from the WAR file I get this error:
11:55:07,916 ERROR [org.jboss.as.ejb3.invocation] (default task-4) JBAS014134:
EJB Invocation failed on component PersonResource for method
public za.co.shared.PersonDTO za.co.ws.PersonResource.get(java.lang.Long):
javax.ejb.EJBException: <openjpa-2.3.0-r422266:1540826 nonfatal user error>
org.apache.openjpa.persistence.ArgumentException: This configuration disallows
runtime optimization, but the following listed types were not enhanced at
build time or at class load time with a javaagent:
... long list of all my entities
The type "class za.co.entities.Person" has not been enhanced.
Which doesn't make sense to me because the classes are definitely enhanced at build time.
My POM for the JAR project contains:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>openjpa-maven-plugin</artifactId>
<version>1.2</version>
<configuration>
<includes>za.co.entities.Person</includes>
<enforcePropertyRestrictions>true</enforcePropertyRestrictions>
</configuration>
<executions>
<execution>
<id>JPA Enhance</id>
<phase>process-classes</phase>
<goals>
<goal>enhance</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.openjpa</groupId>
<artifactId>openjpa</artifactId>
<version>2.2.2</version>
</dependency>
</dependencies>
</plugin>
Can the JPA entities be referenced from a different library?
Edit
I've decompiled the WAR file and the JAR that is referenced within it and I can see that the classes are in fact enhanced. So clearly it's not a problem with the way that is working. Could be some sort of configuration problem?

I moved all my JPA entities into their own JAR file and referenced that from my EJB modules and from the WAR. i.e.
EAR
|->JAR - JPA entities only and pesistence.xml
|->JAR - business logic in EJBs (including EntityManager calls to retrieve data)
| dependent on JAR above.
|->WAR - dependent on the first JAR only
In this way the code in the WAR file can access the JPA entities in the first JAR. However if I add the second JAR as a dependency in the WAR file and try and access the entity manager calls then the original error returns. Also lazy loading on the JPA entities is disabled in the WAR.

Related

maven dependency plugin throwing error even if I add flag ignoreunuseddeclareddependecy

I am trying to build my code and I am getting this error:
[WARNING] Unused declared dependencies found:
[WARNING] com:test-client:jar:v1.0-SNAPSHOT:compile
This is the configuration of the dependency plugin in my pom:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>analyze</id>
<goals>
<goal>analyze-only</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- <usedDependencies>-->
<!-- <usedDependency>com:test-client</usedDependency>-->
<!-- </usedDependencies>-->
<failOnWarning>true</failOnWarning>
<ignoreNonCompile>true</ignoreNonCompile>
<ignoredUnusedDeclaredDependencies>
<!-- <ignoredUnusedDeclaredDependency>*:test-client:*</ignoredUnusedDeclaredDependency>-->
</ignoredUnusedDeclaredDependencies>
</configuration>
</plugin>
I tried to add the test-client to the flag ignoredUnsedDeclaredDependecy (see the commented part) but still getting error.
I tried to set the failOnWarning flag to false but also I get same warning.
When I added this section it works.
<usedDependencies>
<usedDependency>com:test-client</usedDependency>
</usedDependencies>
But why the 2 other flags are not being taken?
Note that I am working in a multi module project and this is a child pom of a parent pom.
failOnWarning makes the maven build fail if a warning was generated, it doesn't remove or prevent the warnings. See https://maven.apache.org/plugins/maven-dependency-plugin/examples/failing-the-build-on-dependency-analysis-warnings.html
Regarding the configuration of exlusions, see https://maven.apache.org/plugins/maven-dependency-plugin/examples/exclude-dependencies-from-dependency-analysis.html
That gives an example like so;
<configuration>
<failOnWarning>true</failOnWarning>
<!-- ignore jsr305 for "used but undeclared", "declared but unused", and "non-test scoped" -->
<ignoredDependencies>
<ignoredDependency>com.google.code.findbugs:jsr305</ignoredDependency>
</ignoredDependencies>
<!-- ignore annotations for "used but undeclared" warnings -->
<ignoredUsedUndeclaredDependencies>
<ignoredUsedUndeclaredDependency>com.google.code.findbugs:annotations</ignoredUsedUndeclaredDependency>
</ignoredUsedUndeclaredDependencies>
<!-- ignore annotations for "unused but declared" warnings -->
<ignoredUnusedDeclaredDependencies>
<ignoredUnusedDeclaredDependency>com.google.code.findbugs:annotations</ignoredUnusedDeclaredDependency>
</ignoredUnusedDeclaredDependencies>
<!-- ignore annotations for "non-test scoped" warnings -->
<ignoredNonTestScopedDependencies>
<ignoredNonTestScopedDependency>com.google.code.findbugs:annotations</ignoredNonTestScopedDependency>
</ignoredNonTestScopedDependencies>
</configuration>
So your syntax should be correct.
Do you depend on the latest version of the plugin? If not, try to update the version number to the most recent one you have access to: https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-dependency-plugin
ignoredUnusedDeclaredDependencies was added in the plugin version 2.10 and ignoredNonTestScopedDependencies since 3.3.0.
Also check if your wildcard filter syntax is correct according to https://maven.apache.org/plugins/maven-dependency-plugin/examples/filtering-the-dependency-tree.html
I figured out what was the issue! my settings were override by the parent pom, why so? because in the parent pom the configuration is written under execution, however in my pom the configuration is outside execution (directly under plugin) when I moved the configuration in my plugin under the execution, my settings were taken into consideration

#ComponentScan for external dependency package is not working in 2.1.x at the time of building the jar and running that jar

I'm facing an issuing while using the spring boot 2.1.x.RELEASE as it is not scanning the basepackages in component scan but It is working perfectly fine in 2.0.x.
#ComponentScan(basePackages = { "com.app.base*", "com.app.child.*" })
When creating a build using
mvn clean package
It's jar has been created but when running the jar it is not loading the beans from the base external repository.
java -jar child.jar
Exception:
ConfigServletWebServerApplicationContext : Exception encountered during context initialization
- cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException:
Failed to parse configuration class
[com.app.child.UserApiApplication];
nested exception is java.io.FileNotFoundException:
class path resource [com/app/base/service/BaseService.class] cannot be opened because it does not exist
I am adding base.jar in the lib/base.jar of the child project. Following is the pom.xml
<dependency>
<groupId>com.app</groupId>
<artifactId>base</artifactId>
<version>0.2</version>
</dependency>
<repository>
<id>myRepo</id>
<url>file://${basedir}/lib</url>
</repository>
For deploy base.jar into my local repository
mvn deploy:deploy-file -DgroupId=com.app -DartifactId=base-Dversion=0.2 -Durl=file:./myRepo -DrepositoryId=myRepo -DupdateReleaseInfo=true -Dfile=lib/base-0.2.jar
The component scan is well working when I run the application from STS. But when I am creating build and trying to run the jar It is throwing error.
I have also exploded the child.jar using
jar -tf child.jar
base.jar was there in the list.
Difficult to say what exactly is the issue.
First of all for you can use ComponentScan like this
#ComponentScan(basePackages = { "com.app.base", "com.app.child" })
See Spring Framework ComponentScan
Next to it, it looks like that you're trying to run a java file which is not a fat jar or you're not setting the expecting classes into your classpath.
you can add this class by adding a classpath to your call.
java -cp <path/to/base.jar> -jar child.jar
Give it a try. If this is working, at least you have a hint, that you base.jar isn't loaded correctly.
As of 2.1.x the repackage execution has an identifier. we are adding a second repackage so end up with two fat jars.
<executions>
<execution><!--
<goals>
<goal>repackage</goal>
</goals> -->
<id>repackage</id>
<configuration>
<classifier>exec</classifier>
</configuration>
</execution>
</executions>

Generating Java classes from XSD maven dependency in maven with JAXB (or others)

I have a data model stored in the Maven repo as an XSD file. My goal is to create a jar with all the Java classes representing this model. I want to use maven and JAXB to do this.
I know about the maven-jaxb2-plugin (codehouse-mojo and java-net, not sure yet how they differ) but I don't see a way to use XSD from the maven dependency as an input. Do I have to write my own plugin to do this?
It doesn't have to be JAXB if there is a better tool to it.
Disclaimer: I'm the author of the maven-jaxb2-plugin.
Check the documentation, it's right there. See Specifying What To Compile - Specifying URLs, filesets and Maven artifact resources.
Example:
<configuration>
<schemas>
<!--
Compiles a schema which resides
in another Maven artifact.
-->
<schema>
<dependencyResource>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin-tests-po</artifactId>
<!-- Can be defined in project dependencies or dependency management -->
<version>${project.version}</version>
<resource>purchaseorder.xsd</resource>
</dependencyResource>
</schema>
</schemas>
</configuration>
You can also use catalogs to rewrite schema URLs to Maven artifact resources.
Example:
REWRITE_SYSTEM "http://schemas.opengis.net" "maven:org.jvnet.ogc:ogc-schemas:jar::!/ogc"
This will rewrite an URI http://schemas.opengis.net/ows/2.0/owsAll.xsd to maven:org.jvnet.ogc:ogc-schemas:jar::!/ogc/ows/2.0/owsAll.xsd. This will reference the ogc/ows/2.0/owsAll.xsd resource in ogc-schemas JAR artifact.
As far as I know, these features are unique to the maven-jaxb2-plugin.

Cannot load UIMA PEAR package through the package installer GUI

I have a problem related to installing a UIMA PEAR package containing an Annotator component. I am using PearPackageMavenPlugin for the job with the following setup:
<plugin>
<groupId>org.apache.uima</groupId>
<artifactId>PearPackagingMavenPlugin</artifactId>
<version>2.6.0</version>
<extensions>true</extensions>
<executions>
<execution>
<phase>package</phase>
<configuration>
<!-- PEAR file component classpath settings -->
<classpath>$main_root/bin</classpath>
<!-- PEAR file main component descriptor -->
<mainComponentDesc>desc/S4DocumentUimaAnnotator.xml</mainComponentDesc>
<!-- PEAR file component ID -->
<componentId>S4DocumentAnnotator</componentId>
<!-- PEAR file UIMA datapath settings -->
<datapath>$main_root/resources</datapath>
</configuration>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
`
I have constructed a special maven profile building the project in a bin directory instead of target so all my compiled classes are there that is why I have pointed the classpath setting of the plugin at $main_root/bin.
Finally when I load the built pear package I get the following error:
Verification of S4DocumentAnnotator failed =>
org.apache.uima.resource.ResourceInitializationException: The class com.ontotext.s4.api.components.uima.S4DocumentUimaAnnotator is not a valid Analysis Component. You must specify an Annotator, CAS Consumer, Collection Reader, or CAS Multiplier. If you are calling ResourceManager.setExtensionClassPath, this error can also be caused if you have put UIMA framework jar files on the extension classpath, which is not allowed. (Descriptor: file:/home/ceco/s4_stuff/my_pear/S4DocumentAnnotator/desc/S4DocumentUimaAnnotator.xml)
at org.apache.uima.analysis_engine.impl.PrimitiveAnalysisEngine_impl.initializeAnalysisComponent(PrimitiveAnalysisEngine_impl.java:228)
at org.apache.uima.analysis_engine.impl.PrimitiveAnalysisEngine_impl.initialize(PrimitiveAnalysisEngine_impl.java:170)
at org.apache.uima.impl.AnalysisEngineFactory_impl.produceResource(AnalysisEngineFactory_impl.java:94)
at org.apache.uima.impl.CompositeResourceFactory_impl.produceResource(CompositeResourceFactory_impl.java:62)
at org.apache.uima.UIMAFramework.produceResource(UIMAFramework.java:279)
at org.apache.uima.UIMAFramework.produceResource(UIMAFramework.java:331)
at org.apache.uima.UIMAFramework.produceAnalysisEngine(UIMAFramework.java:448)
at org.apache.uima.pear.tools.InstallationTester.testAnalysisEngine(InstallationTester.java:218)
at org.apache.uima.pear.tools.InstallationTester.doTest(InstallationTester.java:113)
at org.apache.uima.pear.tools.InstallationController.verifyComponentInstallation(InstallationController.java:1110)
at org.apache.uima.pear.tools.InstallationController.verifyComponent(InstallationController.java:1993)
at org.apache.uima.tools.pear.install.InstallPear.installPear(InstallPear.java:389)
at org.apache.uima.tools.pear.install.InstallPear.access$000(InstallPear.java:80)
at org.apache.uima.tools.pear.install.InstallPear$RunInstallation.run(InstallPear.java:109)
at java.lang.Thread.run(Thread.java:744)
I do not understand why the UIMA jars are not supposed to be packaged when the idea of the PEAR package is to be self-contained and not depend on the system it is ran on?
This is what I would try:
The class com.ontotext.s4.api.components.uima.S4DocumentUimaAnnotator is not a valid Analysis Component
Check that S4DocumentUimaAnnotator is valid. Unzip the PEAR and check the xml.
If you are calling ResourceManager.setExtensionClassPath, this error can also be caused if you have put UIMA framework jar files on the extension classpath, which is not allowed.
Did you try to print the extension classpath?
Else you could try to use a plain java version of the PEAR, meaning: manually unzip it and create a normal java project with it.
One common issue I ran into several times and which creates exactly this error message is that my PEAR contains uimaj-common.jar in the lib folder. Have you checked this?
I know this one is old but what I think you should do is set the scope of the uima dependencies to provided. The PEAR needs only it's dependencies to run, the enviroment it'll be used should have the uima dependencies set to use all the uima features and pears
Provided dependencies won't be copied to the lib folder of the PEAR
There are a couple of things that could be going on, but going by your error message, the first thing I would determine is whether or not the uimaj-core jar file is being excluded from the build of the PEAR file. It should be. (Take a look at the error message above.) I just ran into this problem myself, and I got around it by adding this to my POM:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<!-- Copy the dependencies to the lib folder for the PEAR to copy. -->
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<stripVersion>true</stripVersion>
<outputDirectory>${basedir}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
<includeScope>runtime</includeScope>
<!-- An exception happens when using a PEAR if the archive includes this jar. -->
<excludeArtifactIds>uimaj-core</excludeArtifactIds>
</configuration>
</execution>
</executions>
</plugin>
I recently posted a parent POM file and a project-specific POM file to my Gist. You're welcome to take a look at what I'm doing. (One of the things I'm doing is copying over my desc directory, which contains my AE's descriptor XML file, to the root of my project directory, so that the maven-pear plugin can copy it into the PEAR archive properly.)
Parent POM:
https://gist.github.com/software-mariodiana/d46e10fca53dc6e6c0f16e20563476b8
Project-specific POM:
https://gist.github.com/software-mariodiana/e9a0f0f03a49d33dcc32655170fd4841
Good luck!

java.lang.NoSuchMethodError using JOOQ

I was trying to use JOOQ in glassfish. I used code generator like this:
java -cp jOOQ-lib/jooq-3.3.1.jar:jOOQ-lib/jooq-meta-3.3.1.jar:jOOQ-lib/jooq-codegen-3.3.1.jar:mysql-connector-java-5.1.29-bin.jar:. org.jooq.util.GenerationTool /db.xml
Then imported the generated folder to my project.(I'm not using jooq maven plugin). When I deploy web app in glassfish I see this in server.log
[#|2014-04-06T14:53:37.720+0430|SEVERE|glassfish3.1.2|com.sun.xml.ws.server.sei.TieHandler|_ThreadID=670;_ThreadName=Thread-2;|org.jooq.impl.TableImpl.<init>(Ljava/lang/String;Lorg/jooq/Schema;Lorg/jooq/Table;[Lorg/jooq/Field;Ljava/lang/String;)V
java.lang.NoSuchMethodError: org.jooq.impl.TableImpl.<init>(Ljava/lang/String;Lorg/jooq/Schema;Lorg/jooq/Table;[Lorg/jooq/Field;Ljava/lang/String;)V
I have not changed any maven config just netbeans default config. maven artifact:
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq</artifactId>
<version>3.3.1</version>
</dependency>
my db.xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<configuration>
<!-- Configure the database connection here -->
<jdbc>
<driver>com.mysql.jdbc.Driver</driver>
<url>jdbc:mysql://127.0.0.1/bulkdb?useUnicode=true</url>
<user>user</user>
<password>pass</password>
</jdbc>
<generator>
<database>
<name>org.jooq.util.mysql.MySQLDatabase</name>
<inputSchema>bulkdb</inputSchema>
<includes>.*</includes>
<excludes></excludes>
</database>
<target>
<packageName>bulkdb</packageName>
<directory>/home/user/jooq</directory>
</target>
</generator>
</configuration>
What is going wrong? Can someone help?
[UPDATE]
Actually there is two version of JOOQ in app server class path: one in lib directory of the domain(domain1/lib/) with version 3.1 and second one is 3.3.1 that is bundled in war file. Does this cause problems?
Actually there is two version of JOOQ in app server class path: one in lib directory of the domain(domain1/lib/) with version 3.1 and second one is 3.3.1 that is bundled in war file. Does this cause problems?
Yes, of course :-)
If you want to use both versions in parallel (do you really?), then you will probably need to resort to something like OSGi to be able to load the same class names in separate class loaders.
In your case, jOOQ 3.1 is loaded first by your application server, and thus jOOQ 3.3 cannot be loaded fully any more. The code generated with jOOQ 3.3 operates on new internal methods in TableImpl, which have been added in jOOQ 3.2 or 3.3, but since you're loading jOOQ 3.1, those methods aren't there. Note that this can happen with any external dependencies.
The solution here is really to remove jOOQ 3.1 from your application server.

Categories