Inside a GWT application we have a "shared" package which contains, as its name implies, objects shared between client-side and server-side code.
We have a multi-module maven project:
+ server
| |
| + businessLogicPackage
|
+ gwt
|
+ client
|
+ server
| |
| + converter
| |
| + rpc
|
+ shared
Each time I need to reuse a shared object in the server module, I need to convert the shared object using some converter located in gwt/server/converter.
I tried to use inheritance and have the shared objects inherit classes from the server/businessLogicPackage, thinking I could get away with a simple casting operation. This produces an error. Obviously GWT can't compile the sources from an external module.
> No source code is available for type server.businessLogicPackage.x; did you forget to inherit a required module ?
Knowing that:
We are manipulating a huge quantity of "shared" objects
I want my business logic to remain in a separate module so I can reuse it elsewhere
I don't want to write all those converters
Could anyone share some best practice / alternative with me ? What's trendy now in 2014 ?
Your problem is to use classes from gwt module and shared package in server module without manual conversion ?
You could simply create a third module named shared for example that contains the classes used by gwt client side and server module.
gwt and server modules depend on this new shared module.
For GWT to have access to the source, you add a MySharedModuleName.gwt.xml to the new module and add its package as source. In your GWT module, add an inherit tag for MySharedModuleName.
And then, GWT needs access to the sources. You can add in your gwt module pom a dependency to the sources jar if you create it : <classifier>sources</classifier>. Or add the goal gwt:resources in your shared module to include the sources inside the jar.
Finally I added the sources to the compiled jar, so adding only one dependency (without classifier) to the POM is enough. My GWT-module inherits from the GWT-module of the "shared" maven-module as proposed by Nicolas Morel.
<build>
<resources>
<resource>
<directory>${project.build.sourceDirectory}</directory>
</resource>
<resource>
<directory>${basedir}/src/main/resources</directory>
</resource>
</resources>
</build>
Related
I fail to configure a project based on Spring Boot, extended with OSGI, and using Jackson in a bundle
by using import/export package system (I would like to avoid to embed the lib).
I started from this project.
Maven modules:
a Spring Boot module, the main app, initialized with Felix, like the referenced project above
a bundle module, where I want to use ObjectMapper on activation like this
#Activate
public void activate() {
ObjectMapper objectMapper = new ObjectMapper();
}
But on activation I got the following error:
NoClassDefFoundError: com/fasterxml/jackson/databind/ObjectMapper
Yet, I had exported this package from the main app bundle (Spring Boot) and imported it in the bundle module.
This bundle module use maven-bundle-plugin (while the main app use nothing specific):
<instructions>
<_dsannotations>*</_dsannotations>
<_metatypeannotations>*</_metatypeannotations>
<Bundle-Vendor>Foo</Bundle-Vendor>
<Bundle-Version>${project.version}</Bundle-Version>
<Export-Service>
com.foo.bar.plugin.RestTaskPlugin
</Export-Service>
<Export-Package>
com.foo.bar.plugin.rest,
com.foo.bar.plugin.rest.web,
com.foo.bar.plugin.rest.config
</Export-Package>
<Import-Package>*;resolution:=optional</Import-Package>
<Embed-Dependency>*;scope=compile|runtime; type=!pom; inline=true</Embed-Dependency>
<Embed-Transitive>false</Embed-Transitive>
</instructions>
I only embed logback-classic and logback-core in this bundle (this could be another story...). In other words, I only have these 2 dependencies declared in the module. I also tried to add databind in provided scope but it changes nothing.
I don't get it since I have exported all "com.fasterxml.jackson" packages and sub-packages. To do so, the Spring boot initialize the Felix framework with a custom configuration, with the config key org.osgi.framework.system.packages.extra containing all packages to export automatically according to
the documention.
When I log them we can see around 100 exported packages including these ones :
....
com.fasterxml.jackson.databind.deser.impl,
com.fasterxml.jackson.databind,
com.fasterxml.jackson.core.sym,
....
The module bundle using databind MANIFEST.MF :
Export-Package: com.foooooooo.baaaar.plugin.rest;uses:="com.foooooooo.d
atagen.api.plugins,com.foooooooo.baaaaar.api.plugins.properties,com.baa
aaaaar.datagen.api.plugins.web,com.foooooooo.baaaaar.plugin.rest.config
,com.foooooooo.baaaaar.plugin.rest.web,com.foooooooo.baaaaar.task,com.d
atanumia.baaaaar.task.config,com.foooooooo.baaaaar.task.environment,com
.foooooooo.baaaaar.task.template,org.springframework.http,org.springfra
mework.web.client";version="0.1.0.SNAPSHOT",com.foooooooo.baaaaar.plugi
n.rest.web;uses:="com.foooooooo.baaaaar.api.plugins,com.foooooooo.baaaa
ar.api.plugins.web";version="0.1.0.SNAPSHOT",com.foooooooo.baaaaar.plug
in.rest.config;uses:="com.foooooooo.baaaaar.task.config,com.fasterxml.j
ackson.annotation,com.fasterxml.jackson.databind.annotation,org.springf
ramework.http";version="0.1.0.SNAPSHOT"
Export-Service: com.foooooooo.baaaaar.plugin.RestTaskPlugin
Import-Package: com.foooooooo.baaaar.api.plugins;resolution:=optional,c
om.foooooooo.baaaaar.api.plugins.properties;resolution:=optional,com.da
tanumia.baaaaar.api.plugins.web;resolution:=optional,com.foooooooo.baaa
aar.task;resolution:=optional,com.foooooooo.baaaaar.task.config;resolut
ion:=optional,com.foooooooo.baaaaar.task.environment;resolution:=option
al,com.foooooooo.baaaaar.task.template;resolution:=optional,com.foooooo
al,com.foooooooo.baaaaar.task.template;resolution:=optional,com.foooooo
oo.baaaaar.tools.file;resolution:=optional,com.fasterxml.jackson.annota
tion;resolution:=optional;version="[2.13,3)",com.fasterxml.jackson.data
bind;resolution:=optional;version="[2.13,3)",com.fasterxml.jackson.data
bind.annotation;resolution:=optional;version="[2.13,3)",com.jayway.json
path;resolution:=optional;version="[2.7,3)",java.io;resolution:=optiona
l,java.lang;resolution:=optional,java.lang.annotation;resolution:=optio
nal,java.lang.invoke;resolution:=optional,java.lang.management;resoluti
on:=optional,java.lang.reflect;resolution:=optional,java.net;resolution
:=optional,java.nio.channels;resolution:=optional,java.nio.charset;reso
lution:=optional,java.nio.file;resolution:=optional,java.security;resol
ution:=optional,java.security.cert;resolution:=optional,java.text;resol
ution:=optional,java.util;resolution:=optional,java.util.concurrent;res
olution:=optional,java.util.concurrent.atomic;resolution:=optional,java
.util.concurrent.locks;resolution:=optional,java.util.function;resoluti
on:=optional,java.util.logging;resolution:=optional,java.util.regex;res
olution:=optional,java.util.stream;resolution:=optional,java.util.zip;r
esolution:=optional,javax.mail;resolution:=optional,javax.mail.internet
;resolution:=optional,javax.management;resolution:=optional,javax.namin
g;resolution:=optional,javax.net;resolution:=optional,javax.net.ssl;res
olution:=optional,javax.servlet;resolution:=optional,javax.servlet.http
;resolution:=optional,javax.xml.namespace;resolution:=optional,javax.xm
l.parsers;resolution:=optional,javax.xml.stream;resolution:=optional,ja
vax.xml.stream.events;resolution:=optional,lombok;resolution:=optional,
org.apache.commons.lang3;resolution:=optional;version="[3.12,4)",org.co
dehaus.commons.compiler;resolution:=optional,org.codehaus.janino;resolu
tion:=optional,org.springframework.http;resolution:=optional,org.spring
framework.http.converter.json;resolution:=optional,org.springframework.
util;resolution:=optional,org.springframework.web.client;resolution:=op
tional,org.springframework.web.util;resolution:=optional,org.xml.sax;re
solution:=optional,org.xml.sax.helpers;resolution:=optional,sun.reflect
;resolution:=optional
It is difficult to see but we have the expected import-package:
com.fasterxml.jackson.databind;resolution:=optional;version="[2.13,3)"
I do not have any other loaded bundle excepted the framework bundles:
org.apache.felix.fileinstall-3.7.4.jar
org.apache.felix.scr-2.2.0.jar
org.osgi.service.cm-1.6.1.jar
org.osgi.util.function-1.2.0.jar
org.osgi.util.promise-1.2.0.jar
I spend days to understand what is happening but each new tests bring me new unresolved challenges.
I can have a working project if I change strategy and embed everything, but then in some particular situations I have a linkage problem since I use the same lib in different class loaders.
How to use import/export package system correctly ?
Any idea about the problem here ?
UPDATE: the export-import seems to work with other packages. For example I can use spring-web in provided scope, it is not in the final bundle module, and I can use some methods of spring-web, exported from main app. The problem seems to be the Jackson Databind or something in related to it.
UPDATE
If I use bnd-maven-plugin I do not have the problem, I will post it unless someone give a better answer.
So I've got this Scala + multi-module Maven project hierarchy:
- pom.xml
- nested1
| - pom.xml
| - src
| - main
| - scala
| - ...
| - MyClass.scala
| - ...
- nested2
| - pom.xml
| - src
| - main
| - scala
| - ...
| - MyClass.scala
| - ...
- app
| - pom.xml
| - Main.scala
Basically, the projects nested1 and nested2 have exactly the same class hierarchy: they declare the same classes, the same traits, all of them have the same content, etc.
app/pom.xml has these dependencies:
<dependency>
<groupId>${project-package}</groupId>
<artifactId>nested1</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>${project-package}</groupId>
<artifactId>nested2</artifactId>
<version>1.0</version>
</dependency>
The Main class actually imports MyClass at line 1, but (I would guess) it has no way to tell which version to take: both nested1/src/main/scala/${project-package}/MyClass.scala and nested2/src/main/scala/${project-package}/MyClass.scala have the same ${project-package}.
I actually tried this scenario, and maven seems to choose at random either of the two classes without even issuing a single warning or error.
What's happening behind the scenes? Why am I not getting an error like "ambiguous import statement: MyClass at line 1 in Main"?
You don't see errors like you describe because this situation is explicitly not supported by the JVM. It is not even related to Scala directly: it's just on the JVM level, there is no such thing as "libraries", there is only flat classpath (until JPMS in any case), where class names are supposed to be unique.
In general, such situation should never happen - you just can't have different classes having the same fully-qualified name (package + class name, basically) within one class loader (or, more often, within a single branch of the tree of classloaders). If you do, what happens is undefined. This is similar to the concept of "undefined behavior" in C/C++: the runtime just assumes that there can only be one class for the given name, and is free to behave based on this assumption; depending on classloaders configurations, you can get a random result, some fixed result, an error during classloading, or any combination of these. Things become even more funny when you have dependencies which in their turn depend on different versions of the same class, resulting in a whole host of potential runtime exceptions.
This is a part of a very well-known problem of the JVM world, so-called classpath/JAR hell. Basically, if your project is complex enough that it has transitive or direct dependencies on different versions of the same library, or, more specifically, the same set of class names, you will suffer. The amount of suffering depends on the complexity of your situation: in some cases, ensuring that you only have one version of a library in classpath is enough (which might require some tweaks in the build configuration); in other cases, you'll have to perform shading (which is exactly a way to solve the problem which you encountered) for certain subset of your dependencies. In even more difficult cases, shading won't work, and you'll have to rethink your architecture. Depending on your environment, you might need to use tools like OSGi or indeed the new Java Platform Module System to solve classpath issues.
Note that with JPMS, this particular situation becomes a bit better: Java modules by design cannot have the same package defined in a multiple modules loaded by the same JVM, so if you compile your projects as proper modules, and try to use them in the third project, you'll get an exception during startup about conflicting modules. That being said, I don't have much experience with JPMS so I can't say how exactly it will work, especially with Scala in the mix.
I have a GWT application (FooGwtApp) and a library module (FooLib) used as a dependency in FooGwtApp. The package structure of FooLib looks like this:
packageFoo.ImportantClass
packageFoo.UnimportantClass
packageBar.OtherClass
I want ImportantClass (and only ImportantClass) to be compiled to JS by the GWT compiler. Moving ImportantClass to another package is not an option.
I created ImportantClass.gwt.xml within packageFoo with the following content:
<module>
<inherits name="com.google.gwt.user.User"/>
<source path="" includes="**/ImportantClass*"/>
</module>
Next I put an inherited reference to the ImportantClass module definition in FooGwtApp.gwt.xml (this seems to work: the IDE recognizes it, and is able to parse the reference to ImportantClass.gwt.xml).
Now If I put references to ImportantClass into FooGwtApp's client code, the GWT compiler fails, because it does not find ImportantClass on the source path:
No source code is available for type packageFoo.ImportantClass; did you forget to inherit a required module?
I likely messed up sommething in the source path / includes attribute in ImportantClass.gwt.xml - either defining the current package as root package with path="" is not a valid notation or something's wrong with the includes attribute. Or both. Or neither.
Can you give me a clue about where it all went wrong?
It turns out the problem was not in ImportantClass.gwt.xml, but in other Maven related stuff:
ImportantClass.gwt.xml should be placed under src/main/resources/packageFoo, not src/main/java/packageFoo, otherwise it won't be packaged into the binary jar.
GWT compiler compiles from Java source to Javascript source. This means we don't just need ImportantClass.class in FooLib.jar, but also its source. Best solution for this is to use maven-source-plugin in FooLib's pom.xml and also to import the FooLib dependency into FooGwtApp with sources classifier.
On the latter topic, see the following SO answers:
Maven: Distribute source code with with jar-with-dependencies
How to properly include Java sources in Maven?
After fixing the above problems, the source path declaration present in the question works.
I am working on maven netbeans platform project consisting of several modules. I need to depend on some modules (say java.source module), but when I try to run the application, it reports, that required modules are not installed. And event despite I have dependency on java.source declared in my pom.xml
I think, that I have to tell maven somehow, to install (and turn on) these modules in the final assembled application before my module is loaded.
How could I do something like this?
UPDATE:
When I try to create complete netbeans application project from maven artifact and add Java Source API as a dependency into pom.xml... when I run the application, window with following message appears:
Warning - could not install some modules: Editor Library 2 - None of the modules providing the capability org.netbeans.modules.editor.actions could be installed. Editor Indentation for Projects - The module named org.netbeans.modules.editor.settings.storage/1 was needed and not found. Editor Indentation for Projects - The module named org.netbeans.modules.options.editor/1 was needed and not found. Project UI API - No module providing the capability org.netbeans.modules.project.uiapi.ActionsFactory could be found. Project UI API - No module providing the capability org.netbeans.modules.project.uiapi.OpenProjectsTrampoline could be found. Project UI API - No module providing the capability org.netbeans.modules.project.uiapi.ProjectChooserFactory could be found. Editor Error Stripe Impl - The module named org.netbeans.modules.editor.errorstripe.api/1 was needed and not found. Java Source - The module named org.netbeans.libs.javacimpl/1 was needed and not found. Java Source - The module named org.netbeans.modules.editor.indent.project/0-1 was needed and not found. Java Source - The module named org.netbeans.modules.java.preprocessorbridge was needed and not found. Java Source - The module named org.netbeans.modules.options.editor/1 was needed and not found. Java Source - The module named org.netbeans.modules.parsing.api/1 was needed and not found. Editor Settings - No module providing the capability org.netbeans.api.editor.settings.implementation could be found. Diff - The module named org.netbeans.modules.options.editor/1 was needed and not found. 11 further modules could not be installed due to the above problems.
The error-message "Module dependency has friend dependency [...] but is not listed as friend" means that you need to specify an implementation version of org.netbeans.modules.options.editor.
You can achieve this by editing src/main/nbm/module.xml to contain the following entry (I didn't use the actually needed values here. Make sure to find out which values to enter for id and explicitValue to satisfy the dependencies (You can find explanations / instructions in the article linked below):
<dependencies>
<dependency>
<id>org.netbeans.modules:org-netbeans-modules-editor</id>
<type>impl</type>
<explicitValue>org.netbeans.modules.editor/1 = 201107282000</explicitValue>
</dependency>
</dependencies>
I'm pretty sure that the following article will explain some issues and help you find out the needed values for id and explicitValue (language is english, author is me):
http://blog.macrominds.de/2011/08/open-favorites-per-default-in-netbeans-rich-client-platform-maven-standalone-application/
I'm currently having related problems with my application, so I might come back with a more concrete solution in a while.
the easiest way is to grab a class that its complaining about, say "org.netbeans.modules.editor.actions" and go to the Add Dependencies and plug it into the Query field.
From there you should be able to tell which module you will need to include
We're currently creating an app that needs ATG taglibs on SLING/OSGI, we have created a bundle with these taglibs and uploaded it, of course these taglibs call ATG classes, so we are including them in the bootdelegation, using sling.properties file.
sling.bootdelegation.simple=atg.nucleus
sling.bootdelegation.class.atg.nucleus.Nucleus=atg.appassembly, \
atg.appassembly.ant, \
atg.appassembly.progress, \
atg.appassembly.util, \
...ETC...
First we got this error:
org.apache.sling.api.scripting.ScriptEvaluationException: atg/taglib/dspjsp/ImportBeanTag
at org.apache.sling.scripting.core.impl.DefaultSlingScript.call(DefaultSlingScript.java:163)
at org.apache.sling.scripting.core.impl.DefaultSlingScript.eval(DefaultSlingScript.java:107)
at org.apache.sling.scripting.core.impl.DefaultSlingScript.service(DefaultSlingScript.java:226)
at org.apache.sling.engine.impl.request.RequestData.service(RequestData.java:465)
....
....
Caused by: java.lang.NoClassDefFoundError: atg/taglib/dspjsp/ImportBeanTag
at org.apache.jsp.apps.mygeometrixx.components.contenpage.center_jsp._jspx_meth_dsp_005fimportbean_005f0(center_jsp.java:177)
at org.apache.jsp.apps.mygeometrixx.components.contenpage.center_jsp._jspService(center_jsp.java:154)
at org.apache.sling.scripting.jsp.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at
So we added atg.taglib.dspjsp to the packages to be added in bootdelegation sling.properties file.
Then we got this error:
org.apache.sling.api.scripting.ScriptEvaluationException: atg.taglib.dspjsp.ImportBeanTag
at org.apache.sling.scripting.core.impl.DefaultSlingScript.call(DefaultSlingScript.java:163)
at org.apache.sling.scripting.core.impl.DefaultSlingScript.eval(DefaultSlingScript.java:107)
at org.apache.sling.scripting.core.impl.DefaultSlingScript.service(DefaultSlingScript.java:226)
at org.apache.sling.engine.impl.request.RequestData.service(RequestData.java:465)
...
Caused by: java.lang.ClassCastException: atg.taglib.dspjsp.ImportBeanTag
at org.apache.sling.scripting.jsp.jasper.runtime.TagHandlerPool.get(TagHandlerPool.java:125)
at org.apache.jsp.apps.mygeometrixx.components.contenpage.center_jsp._jspx_meth_dsp_005fimportbean_005f0(center_jsp.java:177)
at org.apache.jsp.apps.mygeometrixx.components.contenpage.center_jsp._jspService(center_jsp.java:154)
at org.apache.sling.scripting.jsp.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
All this is running on JBOSS.
Is there a way to avoid this class conflict that is causing the cast exception?
The class cast exception is usually a sign that that class is being made available in two different places (in your case probably via bootdelegation and maybe via a bundle that exports this class). That's what I would investigate first.
Also, to make things more explicit, I would in general advise you to not use boot delegation but instead export these packages explicitly through the system bundle. That way at least you can better debug where classes come from and how things are "wired" by the OSGi resolver.
When loading the ATG tag libraries from outside of the OSGi framework you also have to make sure to provide the JSP API from outside of the framework. By default Sling embeds the JSP API (in the JSP Scripting Bundle).
There are various ways to expose the JSP API into the framework. One is to add them to the system packages in the sling.properties file:
sling.system.packages.atg_jsp = javax.servlet.jsp;javax.servlet.jsp.el; \
javax.servlet.jsp.resources;javax.servlet.jsp.tagext;version=2.1.0