How do I install JQuery into my web app using Maven? - maven-2

I am looking at using Maven 3 for some web development. I know how to use maven to resolve java based jar files. How do I use maven to resolve JavaScript dependencies for example I want to have maven automatically put jquery in my webapp/js folder?
Is it possible to do transative JavaScript dependencies with Maven 3?

You could create your own "war" that has jquery in it on the path you specified.
Afterwards add that war to your real web application. Maven should merge it. I think it was called somehting like "processing overlay".
I'm using the same for some GWT application where the javascript is generated by a maven plugin and it works well.

Perhaps WebJars can help you:
WebJars are client-side web libraries (e.g. jQuery & Bootstrap) packaged into JAR (Java Archive) files.
Explicitly and easily manage the client-side dependencies in JVM-based web applications
Use JVM-based build tools (e.g. Maven, Gradle, & SBT) to download your client-side dependencies
Know which client-side dependencies you are using
Transitive dependencies magically appear
Caveat: WebJars does not put the jquery files in the webapp/js folder. Please read documentation to see how it is used with some popular frameworks.

Try adding this to your pom.xml file. It worked for me :
<dependency>
<groupId>com.efsavage.jquery</groupId>
<artifactId>jquery-maven</artifactId>
<version>1.7.2</version>
</dependency>
This library gives you a way to drop jQuery into your project as a Maven dependency, which means you don't have to include these third party files in your own source control.
Source : here

May be you should use one of CDN to include external javascript libs to your webapps?
E.g. for jQuery you can simple include it from any of URLs from http://docs.jquery.com/Downloading_jQuery#CDN_Hosted_jQuery
Also libs from CDN will reduce your server bandwidth usage.

You can create a new zip artifact in your repo with your version of jquery inside.
Then use the maven war plugin to deploy it to your war.
Take a close look at <targetPath> and <includes> to be sure to deploy what you need where you need it :)
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<overlays>
<overlay>
<groupId>yourgroupid</groupId>
<artifactId>yourJqueryArtifactId</artifactId>
<type>zip</type>
<targetPath/>
<includes>
<include>js/jquery.js</include>
</includes>
</overlay>
</overlays>
</configuration>
</plugin>
the previous config supposes that you have a directory called 'js' in your zip with jquery.js inside. It will be unzipped in your war at js/jquery.js
More info here

Related

How to add a bundle manifest to an existing maven dependency?

I am using the Adobe XMP Core dependency within a maven file
<dependency>
<groupId>com.adobe.xmp</groupId>
<artifactId>xmpcore</artifactId>
<version>5.1.2</version>
</dependency>
The MANIFEST.MF of that bundle is not suited for osgi deployment, as no bundle specific information was provided. So I would need to add the following lines to that Manifest.mf
Bundle-ClassPath: .
Bundle-Version: 5.1.2
Bundle-Name: xmpcore
Bundle-ManifestVersion: 2
Bundle-SymbolicName: com.adobe.xmp.xmpcore
Export-Package:
com.adobe.xmp;version="5.1.2",
com.adobe.xmp.impl;version="5.1.2",
com.adobe.xmp.impl.xpath;version="5.1.2",
Is there a way of unpacking this maven artefact and exchanging the provided manifest via the maven dependency plugin or any different way?
For runtime enhancement use the Pax URL Wrap Project. If you have this bundle in your environment add the wrap: url-schema to your bundle installation it will auto-wrap your bundle on the fly.
IIRC, Eclipse Virgo Bundlor can do that work for you.
http://www.eclipse.org/virgo/documentation/bundlor-documentation-1.1.1.RELEASE/docs/user-guide/htmlsingle/user-guide.html#usage.command.line
Virgo Bundlor will add all export-Package statements + all import statements which he can find via declared import-statements in your java-files. If the jar uses dynamic classloading, you need to add a template.mf file with the additional import.

Maven - Best way to refer to a directory on the system path

I am trying to build an RPM from my Maven project. I have 5 different modules and each one has its own pom.xml, In the root I have one pom.xml which builds all modules (Typical Maven Setup). When I build an RPM, I want to include a directory that is not part of the maven directories. Its above a directory [from the root folder that contains my maven modules]. What is the best way to include that in my RPM? or rather what is the best way to refer to a directory with out hardcoding the path? I am confused about ${baseDir} and what it refers to?
Thank you.
${project.basedir} refers to the root of the project, ie where the pom.xml is, so you could use that in <systemPath>${project.baseDir}/../../dirYouWant</systemPath>
In general though, Maven best-practices would frown about relying on the relative paths around your projects from being there. Instead, I suggest deploying those files as there own project to your maven repository (as a zip, jar, whatever), and then getting them as part of your rpm build. Depending on what plugin you are using to build your RPM, you can unpack those files automatically.
Try this
<dependency>
...groupid,artifactid etc..
<scope>system</scope>
<systemPath>path/to/your/jar</systemPath>
</dependency>
Did you mean you want to add another project to your maven build being level above?
you can do it like this :
in your parent pom :
<modules>
<module>../projectdirectory</module>
</modules>
in your projectdirectory pom :
<parent>
<groupId>...</groupId>
<artifactId>...parent...</artifactId>
<version>...</version>
<relativePath>../parentProject/pom.xml</relativePath>
</parent>

Specifying jar file in maven build argument

We have our project build using maven. We try to run our unit test cases in maven build itself and for doing that we need to add DB2 driver jar in the dependency of all the sub projects.
Instead of doing that, we need a solution to specify the absolute path of the jar file as a mvn command line argument to use it in the running of unit test cases.
This is because the driver jar is available in our app server lib folder and we don't want to specify it in the dependencies of our projects.
Couldn't find a suitable solution googling it, hence requesting for an expert solution here.
Any workaround would be of greater help.
Thanks in advance.
The usual way would be to add a dependency to the database driver and limit the dependency to testing (test scope). So the library is available for unit tests but will not deployed and jar'ed.
Practically spoken, I'd create a maven artifact for this driver (just a basic POM file) and place it on the build servers maven repository (or the nexus, if you use it for the projects).
I'm using a dependency with scope set to 'system' to reference a jar that is available in the container but not in any maven repository. In this case the jar is put in a folder named 'lib' in the project like this, :
<dependency>
<groupId>groupId</groupId>
<artifactId>artifactId</artifactId>
<version>version</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/library.jar</systemPath>
</dependency>
The groupId, artifactId and version can be set to any value you want, the trick was that system dependencies have to be given with an absolute path, which is worked around by using the project.basedir property. It should also be possible to specify the complete path as a property.
We have our project build using maven. We try to run our unit test cases in maven build itself and for doing that we need to add DB2 driver jar in the dependency of all the sub projects.
Well, the maven way would be to declare the DB2 driver as dependency with a test scope in a parent project.
Instead of doing that, we need a solution to specify the absolute path of the jar file as a mvn command line argument to use it in the running of unit test cases.
You could use the additionalClasspathElement in the plugin configuration to pass the path to the driver:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<additionalClasspathElements>
<additionalClasspathElement>path/to/additional/resources</additionalClasspathElement>
</additionalClasspathElements>
</configuration>
</plugin>
If you variablelize it, you could pass the value on the command line.
But to be honest, I can't understand why you don't install the driver in a corporate repository and declare it as dependency. And if you don't have a corporate repository, use a file based repo as described in this previous answer (please, don't use the system scope bad practice). There is no good reason to go the hacky way.

Download Maven2 dependency from non-standard layout repository

I need to download a file from a non-standard layout repository.
The standard repository layout is groupId>/<artifactId>/<version>/<artifactId>-<version>.<packaging> however, I need to download the following file:
http://hudson.myserver.com:10000/repo/ocp-services/schemas/trunk/201/archive/schemas/dist/schemas.jar
where ocp-services is effectively the groupId, schemas is the artifactId and 201 is the version.
How would I add a dependency to this file and get it downloaded into my project and local repository?
This is a Hudson file repository if this is of any help, but it is a third parties so difficult to get them to change any location.
One option would be to register a custom ArtifactRepositoryLayout implementation and to declare a repository using this custom layout. I've never done that but it should be possible, check this blog post.
A second option would be to configure Maven to go through some kind of custom proxy (e.g. a Servlet) and to rewrite the URL on the fly for this particular dependency.
In both cases, I'm afraid Maven will complain about missing metadata ("A dependency in Maven isn't just a JAR file", see 3.5.5. Maven's Dependency Management) because the hudson file repository is just not a Maven repository. Maybe this can be handled programmatically though. But as I said, I've never done this.
A third option would be to ask the project building the JAR you need to deploy it (in the maven sense). That would be of course the best solution.
A last one option would be to just download this JAR and to install it manually in your local repository. If this is an option, go for it.
Have you tried adding this to your pom.xml :
<dependencies>
<dependency>
<groupId>ocp-services</groupId>
<artifactId>schemas</artifactId>
<version>201</version>
<type>jar</type>
</dependency>
</dependencies>
or if that don't work as Pascal says install it manually

using maven to manage java dependencies in a jruby rails app

I'm trying to write a pom.xml that will allow me to run a command locally and fetch all dependencies that my jruby Rails app has. I'm seeing two different configs though and I'm not totally sure which to use (as I'm not a java person whatsoever)
First, many Pom's i'm seeing just have a tag under the root of the pom.xml that list all dependencies. This doesn't however have any information about where these are stored etc... so I feel like this isn't what I want (I need to copy them to my rails lib dir)
Second option, I'm seeing in the mvn docs is to use the maven-dependency-plugin, which seems more like what i'm looking for. I assume then that my outputDirectory would be something like lib
So I don't fully understand what the purpose of the first option's dependency list is for. All I want is mvn to copy my jars locally (and then eventually when my CI server does a deploy). Can someone point me in the right direction?
First Option
<project>
<dependencies>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.4</version>
</dependency>
</project>
Second Option
<project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<configuration>
<artifactItems>
<artifactItem>
<groupId>[ groupId ]</groupId>
<artifactId>[ artifactId ]</artifactId>
<version>[ version ]</version>
<type>[ packaging ]</type>
<classifier> [classifier - optional] </classifier>
<overWrite>[ true or false ]</overWrite>
<outputDirectory>[ output directory ]</outputDirectory>
<destFileName>[ filename ]</destFileName>
</artifactItem>
</artifactItems>
<!-- other configurations here -->
</configuration>
</plugin>
</plugins>
</build>
</project>
First, many Pom's i'm seeing just have a tag under the root of the pom.xml that list all dependencies. This doesn't however have any information about where these are stored etc... so I feel like this isn't what I want (I need to copy them to my rails lib dir)
This is the traditional way to declare and use dependencies on a Java project. Dependencies declared under the <dependencies> element are downloaded from a "remote repository" and installed to your "local repository" (in ~/.m2/repository by default) and artifacts are then handled from there. Maven projects (at least the Java ones) don't use a local lib/ folder for their dependencies.
Second option, I'm seeing in the mvn docs is to use the maven-dependency-plugin, which seems more like what i'm looking for. I assume then that my outputDirectory would be something like lib
The maven dependency plugin allows to interact with artifacts and to copy/unpack them from the local or remote repositories to a specified location. So it can be used to get some dependencies and copy them in lets say a lib/ directory indeed. Actually, it has several goals allowing to do this:
dependency:copy takes a list of artifacts defined in the plugin
configuration section and copies them
to a specified location, renaming them
or stripping the version if desired.
This goal can resolve the artifacts
from remote repositories if they don't
exist in local.
dependency:copy-dependencies takes the list of project direct
dependencies and optionally transitive
dependencies and copies them to a
specified location, stripping the
version if desired. This goal can also
be run from the command line.
The first goal would use the setup you described in your second option. The second goal would use the standard project dependencies that you described in your first option. Both approaches would work.
The problem here is that I don't know exactly what a JRuby Rails app is, what the development workflow is, how to build such an app, etc so I don't know exactly what you need to do and, consequently, what would be the best way to implement that with Maven.
So I googled a bit and found this post that shows another approach based on OS commands (using the maven exec plugin) and has a complete pom.xml doing some other things. Maybe you should look at it and use it as a starting point instead of reinventing everything. This is my suggestion actually.