How to share common resources between many webapplications? - java

I have 4 web applications. But images, css, javascripts are the same.
When I do changes in one project after I need propagate changes to all projects.
What the best way - create common jar only with resoures or use something similar to links in SVN or else?
Thanks.

I had the same requirement and got exactly what I needed using Bower and a small Perl script.
The problem with doing a simple copy of files across your shared projects is if for any reason in the future you need one of your projects to temporarily have a slightly modified version, you will essentially either break your project or your deploy routine. It's a much cleaner way to manage shared resources across projects by placing them in their own version controlled repository so each project can refer to a particular version either "latest" or "1.0.0" etc... now the solution:
Bower is a package manager which can, among other things, help manage retrieval/dependencies of git repositories. You can fetch git repositories from urls or even local paths on a drive. So all the files that need to be shared across projects I place them in their own individual git repository. Bower can then fetch a particular version of the shared content. The only problem is that bower copies them all in one particular sub-directory which is not always ideal.
Script(bower-redeployer) so then I use this perl script to deploy the fetched shared resources to the right location in my project.

With the maven resources plugin, you can specify a copy-resources goal that will copy the common resources into the specified projects prior to building the war files.
I've also seen the use of a common directory that is links (ln -s) during the build. This avoids the space and time load of creating physical copies of the resources.

Couple of suggestions...
Put them in a common jar file
Put them on shared URL(s)

(Promoting user710818's comment to an answer)
Consider using the Maven WAR plugin's overlay feature as described in the following links. From the first link,
Overlays are used to share common resources across multiple web applications. The dependencies of a WAR project are collected in WEB-INF/lib, except for WAR artifacts which are overlayed on the WAR project itself.
http://maven.apache.org/plugins/maven-war-plugin/overlays.html
http://java.dzone.com/articles/mavens-war-overlay-what-are
http://www.manydesigns.com/en/portofino/portofino3/tutorials/using-maven-overlays

Related

Is it possible to save project dependencies using Maven to prevent redownloading?

Problem
I am building a Maven project inside a Docker container. I want the initial build to be quick. However, since during the first build, the ~/.m2 folder is empty, Maven downloads all the specified dependencies, and this process takes a while.
Question
Is there a way to somehow bundle all the dependencies in one jar file ( or something similar), and simply use that while building the project?
Notes
Note 1. The dependencies of this project will probably not change frequently.
Note 2. The reason the build happens inside the container is that depending on some user actions, the resource files are dynamically changed, thus each build generates a JAR file with a different set of resource files. This is the reason why I cannot just build the JAR beforehand, and simply use it inside the container.
Note 3. This container will be run by people other than me, so I cannot simply try mounting the local ./m2 directory to the container to speed up the process.
Thanks, and let me know if there are any questions.

Can Google App Engine Modules share source code just like Maven Modules?

I'm using Google App Engine to create a project consisting of multiple Google Modules. How do I set up my project (using Maven) so that I can share source code such as Objectify object model definitions, shared utility code, and unit test code across the modules?
I'm hoping the answer is simple and that I can just use Maven as suggested in answers such as these:
How do you use Maven to share source code for two projects?
How to create shared source folder across multiple projects in Eclipse?
For test code: Sharing src/test classes between modules in a multi-module maven project
For test code: Share test resources between maven projects
Eclipse Linked Resources: https://stackoverflow.com/a/7585095/2848676. Is this compatible with Maven though?
However, I'm concerned there might be something special about Google App Engine modules that makes them different from Maven modules. And then maybe the approaches above won't work.
As an example of why I'm concerned, notice that Google says "Although Java EE supports WAR files, module configuration uses unpacked WAR directories only." yet some of the solutions given above suggest packaging the shared code into JAR files. I realize WAR and JAR are different but I'm worried I'll waste my time trying to make something work that can't.
Any advice on how to share code among Google App Engine modules?
I have a share directory that contains code I want to share between modules.
Then I can make symlinks from my modules directories to the share directory.
The symlinks can be of a file, sub-directory, or the whole share directory itself.

Are there best practices in using bower in a source-controlled, mavenized, java web application

I'm quite new to bower but not web application development. Previously, I've just downloaded the required JavaScript and CSS files from third-party libraries/frameworks and placed them into my web application's src/main/webapp/scripts (or equivalent) folder. This ensures that only the files needed by the web application are deployed.
With my default setup, the entire bower_components directory will be committed to source control and if I follow the examples for referencing a bower package, e.g.,
<script src="/bower_components/jquery/jquery.js"></script>
I'm going to end up deploying the entire bower_components directory with my web application. This seems like huge overkill (especially were I to use jQuery UI because all the themes are downloaded into bower).
Is there a best practices in using bower with a web application such that the application isn't bloated with unnecessary third-party library files? Please remember that this is also Java and Maven web application.
Seeing that you tagged maven on this question, I completely disagree with checking in 3rd party libraries, after having tasted the goodness of maven dependency management :) no matter whether it is a jar or js.
This is something that we've been trying to reconcile at my work as there doesn't seem to be a natural way to do js dependencies in maven. Specifically for bower there looks to be a good maven plugin here:
https://bitbucket.org/cofarrell/bower-maven-plugin
for which you can specify the target directory. I haven't used it yet, but I would want to have it bring in the js files to my target directory so I don't have to put it in my source. If we move forward with this, this is what I envision.
If you're interested I have more to add (since there's not a lot out there about this topic)... We are currently using maven "js" artifacts so we can leverage maven's dependency management with our 3rd party js. A plugin that we've forked to do this for us is at:
https://github.com/cameroncan/js-import-maven-plugin.
It does its job, but was built for our use case. Please submit issues if you find it is in need of genericizing. We do have to manually upload these artifacts to nexus, but that hasn't been too big of an issue.
A big advantage of using the maven dependency mechanism is the transitive dependency resolution. We have our js broken out into different modules and without maven, there will likely be collisions with the versions of our js files, resulting in a big mess in the final app that pulls all of the js dependencies in.
I just read some articles on this subject and it seems that Bower itself recommends "checking-in" the bower_components into source control:
"...you should always check installed packages into source control.”
From the articles I read, I kind of get the following:
If your project is to be consumed by others (eg: as a bower package too), then don't include bower_components in source control
If your project is to be deployed, include bower_components in source control
You can use bower-installer which is a node package to control which files to be copied to your static resources folder from the downloaded distribution package folder. Please look into below link.
https://www.npmjs.com/package/bower-installer
I followed below steps to select which files to be copied to my lib folder
1) Install bower-installer by runnnig npm install -g bower-installer command
2) Create 'bower_components' folder outside of your src folder.
3) Edit bower.json configuration file(in the 'bower_component's folder ) and specify path for each js library components.
4) Run bower-installer from terminal

Maven: How to "best" use maven also for downloding "project related files" like configuration .xmls?

I am new to maven and ran into the following question/problem:
Given Hibernate as example: When I use maven, I can easily mange dependencies by including Hibernate as a dependency. The jars are manged perfectly. But Hinbernate consits of quite a lot of other (config) files that are required to get it running (like hibernate.cfg.xml).
1.) Does Maven provide any solution to also download these files (so I can use them as a basis for my additions. It would not make sense writing these complexe xmls newly from the scratch). => For Example is there "goal/target" in maven that spits out these required "relataed config" files into a specifc directory?
2.) How do you handle this case? Although I use mave, does this meand that I neverless have to download the common zip/gz Project-Files that used to contain these files? (As i did it in the past)=>So maven only manages/solves a part of the "problems" that I have in this regard.
Update: The files I am talking about are normally files I need to edit quite often (configuration files). So they are mostly not provided as a static config file inside the jars.
UPDATE 2 => Real live example: I just started to write a POM for my project and googled the dependency-names, like "hibernate-core", "hibernate-validation", "rome" (RSS Lib), "tuckey" (Rewrite Filter) and included them in my POM. Now I have all the jars downloaded via maven (great!), but however I do not have any (sample/base) config files. The Web-app can not be run...
Formerly (when I did not use maven) I downloaded the official distribution zip/gz package and they contained everything: jars and sample config files. Ok its great that maven helps me with the jars, but in the end I do have to navigate to every project webpage and also download the zip/gz distribuation package (as I did before) to only get the sample config files to include them in my project and then make some smaller changes... (without hibernate.cfg.xml hibernate does not start and writing it from scratch is an absolute nightmare, the best solution is take their sample file and update some specific stuff...
Thank you very much for any advice.
Markus
Most people handle this by including files like this inside their jars and referencing them via classpath.
If this doesn't appeal to you, and you have a bunch of them, the Maven solution is (a) use the assembly plugin to combine them into a jar or zip or tar; and attach the resulting item as an artifact with a non-empty classifier, and (b) use the maven-dependency-plugin to download the artifact and unpack it under target/something.

Could "attach source" be made easier for popular Java libraries?

I run into this obstacle when my debugger steps into some classfile
without corresponding source. Finding it is often difficult:
You have to search for the site hosting the respective project,
and find its ``download source'' page (for instance, last time I searched
for the JPA API, and it took me hours to obtain the sources). Or, you might
be required to check it out from revision control.
You need to know the exact version you are using. Otherwise
the debugger might step into comments and empty lines :)
There is no convention for packaging source code—some
projects include it in the jar itself; some provide a separate zip file;
others put it in a src/ subfolder within the zip.
I know Maven has the capability of downloading source from its repository and
including it in the src paths when an IDE config file is generated. But
Maven's repo is so poor in terms of content—few libs actually have
their source uploaded.
Why is it so complicated when it can be made straightforward? We could have
some central repo which relates a classfile (or a hash thereof) to the source
file it was compiled from (or a link to it). Maybe a rather huge repo, but
pretty simply structured. An IDE plugin could query it to fetch what's needed automatically.
Have you, guys, experienced the same?
How do you obtain the sources to attach?
Both m2eclipse and IDEA will download the sources and javadocs for any dependencies. The m2eclipse sources can be downloaded by right-clicking on a dependency (or the whole project if you want all sources) and clicking Maven->Download Sources.
On newer versions of m2eclipse you can also automatically download sources by going to Window->Preferences...->Maven, then selecting the "Download Artifact Sources" option. When you open a type in a dependency jar that there are currently no sources available for, Maven will download the sources in the background and update the source attachment in the background.
Haven't seen a satisfactory solution myself.
I tend to roll my own repo, without Maven (Maven is fine, but it doesn't click with me). I run something similar to the BSD ports system, that is, one big structured tree that contains little Ant build files. These build files either checkout the source of a project, pull its dependencies from somewhere else in the tree and build it (these are for the projects I want to build- i.e., mine) or pull binaries from somewhere else (which might be an external source or my own binaries repository).
The system could easily be extended to pull src jars, but I do that manually now.
it may be complicated but it is worth the initial effort.
i do it the following way:
in my project directory i have three major directories,
src (my own)
lib
suppl (sources / javadocs when no sources exist)
i put in suppl one zip file per library, containing the sources. in intellij this gives me not only debugger superpowers, but also javadocs.
you are right, obtaining the sources is a pain. sometimes the sources come deliveded in the .jar file of the lib, sometimes as a seperate download (my favorite) and sometimes i have to create a seperate cvs/svn dir where i can checkout the sources. i usually need to re-package them the way i like them, even if provided in a zip.
i am sceptical about maven. i just don't like to hand over my decisions about choosing libs to a program.
we do something similar to Andreas. Our lib directory has subdirectories categorizing further. One such sub dir is source or debug which has the source JAR/ZIPs of all the jars that we want to debug. Do it once and you're good. We use an IVY repository for the jars and source jars.
This is all done automatically if you use M2eclipse (http://m2eclipse.sonatype.org).

Categories

Resources