GWT and SpringBoot - Is there a smart way to deal with conflicting dependencies? - gwt

In an ideal world, you have your GWT app compiled to javascript, you serve it as static resource and you behind the scenes you have your back end code running on a JVM, and life goes well.
But that ideal world is called production during runtime.
However, during development time, when you would like to make use of the gwt code server...
You're GWT compile time dependencies are needed during runtime (sources + classes), for debugging and recompilation purposes of the GWT module.
At the same time, you may wish to have back-end supported by something like spring-boot 1.3.5.RELEASE.
In this case, spring boot, suffering multiple frequent releases, is at this point in time wanting to add as managed dependency, for example:
<hibernate-validator.version>5.2.4.Final</hibernate-validator.version>
Which, of course, is a very good thing. It is one of the many good features of spring, in fact.
Gwt, on the other hand, see link bellow,
http://www.gwtproject.org/doc/latest/DevGuideValidation.html
is still requiring you to use:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.1.0.Final</version>
<scope>runtime</scope>
</dependency>
Now, if you were to say: I do not care about productive development, just compile me something that works properly in production.
Then indeed, you can trivially solve the above problem by structuring your project in such manner:
ROOT
--- Backend-api
--- Backend-Impl
--- Gwt-Front-end
---- War or stand alone Spring jar module to glue it all together
Where you make the Gwt-Front end depend on the back-end APIs, for some RPC services and DTOs. The pom.xml dependencies you can for the most part manage only in your front-end module, independently of whichver dependencies you have in the back-end.
Ultimately, you make a war, or a spring boot runable jar, that carries your gwt code as static resources and carries your back-end code with all of its dependencies, namely Hirbernate validator latest version.
However, when you are trying to get a pom that works for development purposes, as far as i can see, you are stuck having to globally to manage the dependencies that are common between the back-end and the front-end layer in the ROOT pom.xml, and downgrading your dependencies to the version required by gwt.
That is, in the ideal world scenario.
You have your ROOT pom.xml simply declaring your modules and the order by which they get build. And you get your back-end-impl to have the power to state it wants to inherit dependencies from the spring-boot-starter pom.xml, etc...
In contrast to the ideal scenario, on the pom.xml configuration that actually helps you during the development time...
Well, you can have to revisit the Root pom.xml.
And you have to add in managed dependencies on this root pom.xml, so that for all those common conflicting dependencies between your GWT front-end and spring boot back-end, you always have to hammer the version of the GWT and put a downgrade in place.
This will ensure that when you do gwt:run, dev mode recompile, etc... you do not end up having the gwt compiler trying to javascript compile your Hibernate version 5, but instead the version that is indeed supported by GWT 4.1 final. Of course you might end up having a surprise in back-end code, one of these days by puting in place such a hack...
Does anybody have an idea of how to properly organize a multi module project where back-end and gwt based front-end are allowed to have conflicting depency requirements?
Ultimately, if the answer is no, I believe I will prefer to have network wastage and added communication delay, by having a pure stand-alone spring boot backend that integrates with a pure gwt stand alone jetty, where the back-end on this jetty does nothing more than dumbly kick the requests to the actual spring-boot backend. It is kind of pathetic to have gwt jetty back-end that gets called to do 1+1 by the UI and which forwards the computation to a second back-end running sprin boot that actual knows how to do the 1+1... but if this is the most productive way of working, ensuring that development is productive and the production will run without surprises, so be it.
Thanks for any feedback, and Ideally, I would like to see a multi-pom project that you can import as existing maven project into eclipse, and demonstrates how this can be achieved.

Figure out where the transitive dependency is that you want to exclude when compiling GWT code.
<dependency>
<groupId>spring-dependency-with-hibernate</groupId>
<artifactId>some-spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</exclusion>
</exclusions>
</dependency>
This way only the GWT provided hibernate version will be available at runtime.

Yes, what I wanted to do can in fact be done, though not trivially... not at all.
To setup a maven multi module pom that can be used productively in the sense that you can get code swapping both for back-end and front-end code without needing to choose either the gwt validator dependencies, or the springboot validator dependencies, essentially I organize my project as follows.
You have:
LEVEL 1: myproject-root
----- LEVEL 2: myproject-backend
---------- LEVEL 3: myproject-backend-api
---------- LEVEL 3: myproject-backend-impl
------ LEVEL 2: myproject-frontend
------ LEVEL 2: myproject-runner
-----------LEVEL 3: myproject-runner-jar
-----------LEVEL 3: myproject-runner-war
In myproject-root, you declare the submodules, naturally.
You do dependency management on some inocuous plugins such as surefire plugin. You control java source and target compile language. You do very little more than that. If you have some plugins that you can configure cross cutingly, you might as well put those in the plugin management.
In the myproject-backend, you essentially make it inherit into the dependency management the springboot depencies, by adding to your dependency management:
<dependencyManagement>
<dependencies>
<!-- Inherit dependencies from spring boot dependency management pom -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>${version.spring.boot}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
You declare the two submodules, and you're out.
You do not declare any cross cuting dependencies here. You only use the dependency management. The front-end component will be hiting the api submodule, and you do not want to be creating a dependency magnet in the backend pom, so here you are conservative as much as possible.
In the myproject-backend-api, you start by letting this submodule be a jar sub-module. Down the line, you will want to branch the api component further, by subdividing api-s by entrypoint nature.
To begin with the -api component will have apis needed for the communication between server and frontend, kind of like the typically seen "shared" in the documentation.
Here you are as careful as possible to have as little as possible in what regards to elements in the dependencies element. The front-end will be using these api's and you do not want your api package depending on any spring boot apis.
In the backend-impl, you write your actual business logic and you are free to depend on the full spring boot stack. The rule is, make sure no one ever points to this dependency magnet, especially, never let the gwt component even smell the existence of springboot libraries. Do not refer it ever.
In the myproject-frontend, you do not branch it into different sub-modules, as you might have done with the api component.
Here you make a monolitic component for gwt code so that you can more easily have the gwt maven plugin working for you doing your hot code recompilation.
In this component you depend on the myproject-api, as well as on any relevant gwt library, such as gwt-user, hibernate validator with the old version needed by gwt, etc...
The rule for the myproject-frontend is to declare every single dependency in the component as optional. You do not want to take any transitive depency on the component.
In production all you need is the myproject-frontend.jar/META-INF/resources/MyProjectModule.html...
And all such static resources that springboot will have to detect and serve.
While you are in development mode, however, what you want to do in this front end component is to:
(a) tune your gwt-plugin to not run the jetty server
<noServer>true</noServer>
The point being, if run this jetty server the only thing it would have in its classpath would be the front end code and the api, and definitely not the impl code.
And second, you do not want to use the gwt jetty, you either want to use the springboot tomcat or the springboot jetty. Much better to use the embedded containers of springboot with the appropriate websocket libraries and such.
(b) With springboot you want to server your static resources from the META-INF/resources/ or the public folder. I prefer the resources as it would be where you would normally put JSF components as well.
In any case, because the place static resources come from is special, you want to tune your gwt plugin to compile your java sources into the write folder.
<webappDirectory>${project.build.directory}/classes/META-INF/resources</webappDirectory>
So something like above.
Finally, and this was my main mistake, you do not run your code from where you write your front-end code.
That is wrong, because that forces your front-end code to need springboot depencies, which will cause you extreme pain due to conflicting libraries.
So for dodging this problem, you create the myproject-runner component, and if you want to be very detailed, you make that one a pom parent component so that you can fork your packaging into either jar or war.
I personally like the executable jar, much more to the point, than the war option. I will never want to deploy a sprinboot application in something like a heavy weight weblogic... but whatever rocks your boat.
In any case, the spring boot runner is your ultimate dependency magenet.
This component will depend on about every module you have and their transitive dependencies. But because you separate your runner component from the front-end code, and you make all the dependencies in the front-end code optinal...
Well, you essentially only bundle springboot, and the your gwt static resources.
While you are developing, what you do is .... once you know it ... simple.
(A) you start your spring boot jar application by triggering the main file. If you really want to deploy a war file, that-s your choice you can also build a war file and deploy it to a tomcat or whatever...
(b) you go to your frontend component, rirght click it, chooose run as Maven Build, and gwt:run.
The gwt run will start up the gwt code server, that will be completely blind to your backend code, the only thing it will see in front of its eyes is the code your have in the frontend component as well as all the gwt dependencies you've added in as optinal.
You can finally hot swap code in the back-end if you-re using springboot dev.
You can finally hot swap code in that monolitic frontend component, once you-ve started teh gwt plugin.
Conclusion, it-s possible, but its a hell of a mess.
Glad this problem is out of the way.

I believe I had similar problems and discovered the following while using Eclipse IDE, GWT Google Plugin, SpringBoot and Maven.
The recipe is not simple but it worked for my team.
GWT-dev and Jetty server doesn't work with SpringBoot.
You have to fix the classpath due to Jetty and gwt-dev hardcoded dependencies, only the dependencies gwt-dev and Jetty were built against will work.
I had to split the GWT UI project into two Maven artifacts.
One artifact was a WebApp capable of running only within Eclipse, with all SpringBoot libraries excluded from the classpath.
The other artifact was a deployable SpringBoot JAR with "war" maven packaging type and a WAR overlay of the Eclipse dedicated artifact, including only GWT specicic content.
I hope this helps

Just create dedicated maven profile for running GWT codeserver and add 'gwt-dev' dependency ONLY there
<profiles>
<profile>
<id>gwt-dev</id>
<dependencies>
<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>gwt-dev</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>gwt-maven-plugin</artifactId>
<version>${gwt.version}</version>
<executions>
<execution>
<id>run-codeserver</id>
<goals>
<goal>run-codeserver</goal>
</goals>
</execution>
</executions>
<configuration>
<sourceLevel>${java.version}</sourceLevel>
<module>kn.iopm.documentserver.IDocServer</module>
<extraJvmArgs>-Xmx1G -Xms512M -Xss1G -Dlog.root=${project.build.directory}/log
</extraJvmArgs>
<runTarget>index.html</runTarget>
<persistentunitcachedir>${project.build.directory}</persistentunitcachedir>
<webappDirectory>${project.build.outputDirectory}/public</webappDirectory>
<deploy>${project.build.directory}/gwt-deploy</deploy>
<codeServerWorkDir>${project.build.directory}/gwt</codeServerWorkDir>
<launcherDir>${project.build.outputDirectory}/static</launcherDir>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>

Related

How to package only the necessary libs in Google App Engine Project(Java)?

Let me explain first what I mean by necessary libs. I'm creating my first project using the Google App Engine for Java with the official Google Maven Plugin, the main problem that have Maven as a packaging solution (or maybe the Java development as a whole) is that if the dependency tree grows too much, the release process may be harder.
Let me illustrate it with an example. Let's start with the Jackson JSON library (it's a good starting point since it has no parent dependencies), now someone makes a JSON-RPC library and uses Jackson for the JSON serialization/deserialization. Imagine that this library not just provides a JSON-RPC client implementation, but also a server, that means that the POM of this lib will add some Java EE related libraries such us Jetty as dependencies.
Probably the guidelines say that the application should be either divided into modules or mark the server related deps as optional, but you know that many people don't follow the standards.
Now someone need a JSON-RPC client for his/her project, call it Project X, and uses the lib mentioned above, at compile time there will be no problems, Maven will successfully download the required libs and the application will compile fine, but the problem comes when that person wants to release the application. Which dependencies should be distributed along with the package (in a lib folder for example)?
Actually that's something that happened to me, I wasn't too much familiar with Maven so I used the Eclipse Runnable Jar Exporter, that produced jar file with all the maven libs copied to a lb subfolder, so the workaround that I did then was to just delete the libs that looked unnecessary and then tested if the application was still working. If there are classes that are not executed, as far as I know they are not loaded by the ClassLoader so they could be omitted and are unnecessary
I can't use the same trick now since the scenario is much more complex, we are talking of a Java Web Application, not a desktop application like the other one, and the library that I want to include is a Liquid Template Engine, which uses the ANTLR framework to generate the parsers plus Jackson for the JSON handler and Jsoup for HTML parsing.
Which libs should be packaged inside the WEB-INF/lib folder? I'm sure that I will need Jackson for JSON parsing but I'm not so sure about Jsoup, and what about ANTLR, it is necessary or is used just at compile time?
Update: I think I need to re-formulate my question, actually what I want is to determine which dependencies are really necessary for the application, and package those into the app WEB-INF/lib folder
Solution: It seems that the POM file that is packaged in the WAR file of the web app is used once the app is in the Google App Engine production environment to retrieve the necessary dependencies, and probably the appengine:update goal only packages those dependencies that can't be retrieved from the maven central repo, so there is no need to worry about that.
Thanks to David to point this.
You should check Maven's dependency scopes. Here's an extract from the documentation :
There are 6 scopes available:
compile This is the default scope, used if none is specified. Compile
dependencies are available in all classpaths of a project.
Furthermore, those dependencies are propagated to dependent projects.
provided This is much like compile, but indicates you expect the JDK
or a container to provide the dependency at runtime. For example, when
building a web application for the Java Enterprise Edition, you would
set the dependency on the Servlet API and related Java EE APIs to
scope provided because the web container provides those classes. This
scope is only available on the compilation and test classpath, and is
not transitive.
runtime This scope indicates that the dependency is
not required for compilation, but is for execution. It is in the
runtime and test classpaths, but not the compile classpath.
test This
scope indicates that the dependency is not required for normal use of
the application, and is only available for the test compilation and
execution phases.
system This scope is similar to provided except that
you have to provide the JAR which contains it explicitly. The artifact
is always available and is not looked up in a repository.
import (only
available in Maven 2.0.9 or later) This scope is only used on a
dependency of type pom in the section. It
indicates that the specified POM should be replaced with the
dependencies in that POM's section. Since they
are replaced, dependencies with a scope of import do not actually
participate in limiting the transitivity of a dependency.
So in a Maven project, the developer indicates which dependencies should be bundled in the application and which should not.
Basically there are two cases here :
If you're building a web application (WAR or EAR format) and want to deploy it, or if you're building an actual runnable jar, then you will need to bundle it with all the dependencies with scope compile and runtime.
If you're building a library, then you do not package any dependency with your library. Instead you include the pom.xml so that others know what dependency your library requires. For Maven to know how to find the associated POM for a given jar, the best and most common solution is to deploy the library to a Maven repository. Repos have a directory structure that helps Maven find the right version of a library, and find the POM that indicates the required dependencies.
Depending on wether your library is open source or not, you will be able to be hosted for free by some repositories such as Sonatype (complete list here). But you can also setup your own repository either by installing a dedicated software such as Nexus or by configuring a Github project as the repo, as is explained on this blog.
You can exclude any transitive dependency.
For your case, to remove jetty from this json-rpc-library, you need:
<dependency>
<groupId>com.somecomp</groupId>
<artifactId>jsonrpclib</artifactId>
<version>1.0</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
</exclusion>
</exclusions>
</dependency>
See docs: http://maven.apache.org/guides/introduction/introduction-to-optional-and-excludes-dependencies.html

Maven/Eclipse - Quick iteration acceptance Testing a project packaged as a WAR file

Eclipse makes working with multi module maven projects easy because you don't have to re-build and re-install a module before dependent modules will see changes to it. So you just change the code and eclipse updates the dependencies magically in the background.
I want to achieve this behaviour for acceptance testing as well.
I have
storage-service
storage-service-war
storage-service-acceptance-tests
If I use embedded jetty or tomcat to test inside the storage-service-war project then obviously code changes are immediately viewable in the tests, but I cannot see any way to achieve the same quick iteration of testing when testing from storage-service-acceptance-tests.
Every way I look at it it seems that I have to build storage-service-war and then use the artefact generated from that, but it seems like overkill when you only want to change one line.
Does anyone have a good method for doing this?
Cheers
Piers
So answering my own question :D The solution I came up with will not work on CI it will likely only work when doing a local build as it makes use of the relative paths of the projects. At the bottom I outline a more robust but more complex approach that should satisfy eclipse and CI.
I was looking at setting attachClasses to true for the war plugin configuration of the war project.
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<attachClasses>true</attachClasses>
</configuration>
</plugin>
</plugins>
You can then reference the jar in the dependent project as follows
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>storage-service-war</artifactId>
<version>${project.version}</version>
<classifier>classes</classifier>
</dependency>
Then I was thinking I could run my tests from within the acceptance test module using embedded jetty or tomcat and pointing them to the web.xml defined in the war project using a relative path.
This works fine with maven via the commandline but fails in eclipse :(
The problem with this is that the jar produced by attach classes is not picked up by the eclipse m2e integration see -https://bugs.eclipse.org/bugs/show_bug.cgi?id=365419 unfortunately it wont be fixed.
So My solution for the moment is to manually add the storage-service-war project to my acceptance test project build path in eclipse. Its not great but it works
The above solution is a bit hacky but the alternative outlined is a bit more involved.
By splitting the project into the following I think it would be possible to have correct eclipse integration and projects that work on CI
storage-service
storage-service-core
storage-service-war
storage-service-acceptance-tests
storage-service-config
The core project contains the logic and source of the webapp and is of type jar, the config contains the web.xml and any other config files and is also of type jar. Then the acceptance-tests and war project are both of type war and serve merely to package the core project into a war and extract the config to the webapp/WEB-INF dir so that they may share a common setup.

How should you load Spring related jar in Eclipse?

I new with spring and is following the example from "Spring in Action 3rd Edition".
I want to run the code from the example, so I copied the code.
I install Spring STS suite and have a test spring project. It seems it doesn't include spring's jar implicitly so I need to configure the build path and include and jar one by one.
And jar is in some strange location too (I think they are installed by Spring STS, although I have no idea whether it include Spring itself).
And the spring core depends on common logging from apache:
And I need to go to apache common logging site to download the jar and put it in the lib folder of the project, then set it in the build path.
The whole process is unbearable. What if spring got 20 jars? Is there other way to do this?
Thanks all.
To ease the pain of getting the dependencies, it's highly recommended that you use Maven.
All you need to get started is the following :
Checkout this 5 minute start for Apache Maven.
I have a 'Helloworld' Spring + Maven project (specifically to work with Spring In Action, I might add) setup on Git Hub which should get you started without any hassle.
If you are familiar with GIT then fork this repository otherwise,
Download the whole project as a zip/tarball from here.
This project can also be used as the starting point for a Spring app. Read more about how to get the Spring dependencies using Maven here.
Once you do that a mvn clean install inside the project directory is all you need to get all the required dependencies and there is no manual mucking about to get the jars, put them in the classpath and so on and so forth.
There should be a file called pom.xml in the root folder of your project. It contains all the dependencies.
Add this code block inside of the <dependencies> element:
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
This will add the missing dependency to your project. Alternatively, right click on pom.xml and select Maven -> Add Dependency ... and then type commons-logging in the search field. The editor will add the dependency in the right place when you click OK.

How does Maven handle WAR dependencies at compile time?

I'm using Maven 3 to handle a (large) project. This project defines a set of REST services, using Scala, Akka and the Spray framework. Each service is defined as an independent project, producing a WAR thanks to the "mvn package" command.
I have to "assemble" these different services into a "system", that is, a subset of these services to be deployed for a given purpose, in a single WAR. Maven WAR overlay works good as a first impression: simply create a new project, and add the services to be included as dependencies.
<dependencies>
<dependency>
<groupId>net.modelbased.sensapp.service</groupId>
<artifactId>net.modelbased.sensapp.service.metamodel.repository</artifactId>
<version>0.0.1</version>
<type>war</type>
</dependency>
</dependencies>
It works well, and the classes contained in the independent services are included in the final WAR.
jar tf target/net.modelbased.sensapp.system.envision-0.0.1-SNAPSHOT.war
[...]
WEB-INF/classes/net/modelbased/sensapp/service/metamodel/repository/ModelLister.class
[...]
But ... The Spray framework requires the implementation of a bootstrap, that is, a class that load the implemented services. This file has to be compiled, as it is a plain scala class. But it looks like maven did not includes the WAR dependencies in the compilation classpath.
I saw the WARpath plugin (http://static.appfuse.org/maven-warpath-plugin/) ... But It does not look more or less outdated. Is there a "standard" way of doing what I'd like to do (i.e., include a WAr in the compilation classpath).
Ps. One of the requirement of the project is that each service MUST be deployable as an independent artifact. A solution could be to implement the core of the service as a JAR project, and also provide a WAR project that includes a refarence to the JAR and the associated config file. But it will multiply by 2 the number of project defined, which is not optimal in a way ...
The solution you propose looks good.
You can build a jar from your classes, and then build a war using this jar as a dependency.
You probably can also do this in one single project, I can see no need for two different projects.
You can use the configuration of the maven-war-plugin which offers you a way to create a separate jar file out of the files from src/main/java.
You have to add the configuration items:
<configuration>
<archiveClasses>true</archiveClasses>
<attachClasses>true</attachClasses>
</configuration>
This will create a separate jar file with the classifier classes which can be used as a usual dependency.

Using Guava in a Maven GWT Project

I have a project which would like to utilize the Google Guava libraries (on both the server-side and the client-side), however, I am having trouble setting it up.
I was able to add GWT and Guava as dependencies, and my GWT projects are compiling correctly. My server-side code using Guava also works correctly.
But if I try to add it to my GWT project using the following:
<inherits name="com.google.common.collect.Collect" />
And use the application in development mode via mvn gwt:run, it brings up the Google Development Mode interface and gives errors of the flavor:
Unable to find 'com/google/common/collect/Collect.gwt.xml' on your classpath...
Presumably because the maven dependency is just the compiled class files, and not the source/.gwt.xml files it needs to compile down to the Javascript. I found that if I go to the guava website, and download the files, there is a file called guava-r08-gwt.jar, which I think is heading in the direction of a solution.
Ideally, there would be some dependency I could add in Maven that just lets me use the inherits command, but any other workarounds would be welcomed.
As mentioned in one of the answers, this is in the works.
In the meantime, I have set up a temporary public maven-repo for this purpose. I make no guarantees about it staying up, but here is the repo/dependency stuff:
<repository>
<id>deepthought</id>
<name>Deepthought Public Repository</name>
<url>http://deepthought.co/maven-repo</url>
</repository>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava-gwt</artifactId>
<version>r08</version>
</dependency>
Feel free to use it for now, though.
It's in the works.
http://code.google.com/p/guava-libraries/issues/detail?id=501