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

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>

Related

Maven2 unpack include only internal files

I have the next artifact item for maven unpack
<artifactItem>
<groupId>blabla</groupId>
<artifactId>foo-bar</artifactId>
<outputDirectory>${project.build.directory}/outer-resources/META-INF/wsdl/</outputDirectory>
<includes>xsd/*</includes>
</artifactItem>
I need to copy ONLY files and subfolders from xsd, but not xsd folder. How can I use includes/excludes to make this?
It looks like you may not be able to do this by using maven dependency plugin alone.
But you can try using a combination of maven dependency plugin and maven resource plugin to achieve this. You can use maven dependency plugin to unpack the contents of the dependency to a specific directory and then use maven resource plugin to copy the desired contents from there to another location, excluding the parent xsd folder.
You would need to ensure that both the plugins are invoked in the same phase and goal in the correct sequence.

how to configure maven to use jar files present on the system to satisfy dependency?

I need to configure the jars in my pom.xml file in my web application in such a way that I need not use the lib folder to store all the jar files.
Please help.
If you really have dependencies which are stored in a lib folder (I assume those jar's don't exist in Central) you can use the system dependency
<dependency>
<groupId>...</groupId>
<artifactId>..</artifactId>
<scope>system</scope>
<systemPath>PathOnYourSystem</systemPath>
</dependency>
But i assume you mean something different, cause the above will procuce a warning on Maven 3. If you have a dependency which is provided by the Container (for example Tocmat) you can define a dependency as provided.
But the best is to put such dependencies into a local repository manager which i hope you are using (Artifactory, Nexus, Archiva).
You can mannually add them to your local repository (since it seems that they are not at central).
But the best would be to set up your own (or company) repository to hold them for you.

How to copy resources from other module to specific location ? (maven)

I have a maven-managed project with some modules.
One module contains some native codes inside "src/main/resources/native" directory.
Second module packages all related modules to a WAR file.
Here comes the question : How to copy the "native/" directory (and its sub-directories) in first module to WEB-INF/native directory in the second module ?
I found a copy resources plugin , but it seems not what I want. (It copies directory inside the same module , but I want cross-module copy)
This is doable with dependency:unpack (that I would bind on the prepare-package phase) and the appropriate excludes/includes . See the Unpacking specific artifacts example.
The goal of modules in maven is to spearate them from each other. I am afraid there will be no satisfactory solution inside maven as this goes against the grain.
A solution could be to create a war archive with your resources and depend on that to build your final war.
I use for a project for example the camel-web resources by adding a dependency :
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-web</artifactId>
<version>${camel.version}</version>
<type>war</type>
<scope>runtime</scope>
</dependency>
The war resources are merged with my web resources.

Maven, how to add additional libs not available in repo

I have a maven project that has a set of library dependancies that are not available via any maven repository. How can I add those libraries to the pom? I want to do this so when I run 'mvn eclipse:eclipse' it doesnt remove those libraries from the eclipse classpath.
You can declare it as a dependency with system scope.
<project>
...
<dependencies>
<dependency>
<groupId>sun.jdk</groupId>
<artifactId>tools</artifactId>
<version>1.5.0</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
</dependencies>
...
</project>
You have 3 options:
Add your libraries to your local repository via install:install-file (obviously, this is not portable, you won't be able to build the project on another machine without doing the same).
Install and run an "enterprise repository" like Nexus, Archiva, or Artifactory and add your libraries via deploy:deploy-file.
Setup a file based repository as described in this previous answer and put your libraries in there.
Then, declare your libraries in your pom like any other dependency.
You can include them with your project in a sub-directory (perhaps lib/). You can also provide .bat and/or .sh files containing all the appropriate calls to the maven-install-plugin necessary for each project member (or server env) to add these jars to the local repo.
This approach allows new project members to get up & running quickly, without having to invest several hours in setting up a new public repo for your project or team.
You can't 'add them to the pom'. You have to put them in some repo. You can put them in the local repo with the maven-install-plugin, as suggested by the error message. Or you can deploy them in a local copy of Nexus or something like it.
recently I created a small UI Util to install libraries to you local repository.
It works the same way as install:install-file.
https://github.com/escv/maven-install-ui

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.