Creating new maven2 project with plenty of custom jars

By neokrates, written on April 7, 2008

article

  • Join date: 11-30-99
  • Posts: 224
View Counter:
  • 66 views
Rate it
Ad
Poll
  • What build management tool does your project or firm use?

    View Results

    Loading ... Loading ...
Feeds:
  • bodytext bodytext bodytext

Problem: too many very custom jars

Usecase #1. You want to create Maven2 project but the jar files you depend on are custom or really hard to obtain from repositories.

Usecase #2. You try to port the large java project ASAP into maven2.

Precondition. You have all the jars you need locally.

Quick and dirty solution. Use maven’s capability to refer local jars

The only problem will be to “ingest” the dependencies into maven. There are surely many ways to do that. Following example normally takes 5 minutes of my time to get the project with complex dependencies running. After the task works as it should, I can take care of proper artifact deployment process.

I have done it for linux and bash. That will also work under any other unix or windows with bash enabled. For other shells, you will have to rewrite the script, it’s easy.

1

Create the basic maven2 project

It will contain pom.xml file and /src/main/java directory with you code.

After you finish this step you should have in your project directory :

...
|-- pom.xml
|-- src
|   `-- main
|       |-- java
...

2

Put the jars in the directory under maven

For this project, I put my jar files in /src/main/resources/lib.
I use some jmeter jars, but that is just an example.

...
|-- pom.xml
|-- src
|   `-- main
|       |-- java
|       |   `-- org
|       |       `-- apache
|       |           `-- jmeter
|       `-- resources
|           |-- lib
|              |-- ApacheJMeter.jar
|              `-- ApacheJMeter_functions.jar
...

3

Insert you Java source code, which depends on your custom jars

...
|-- pom.xml
|-- src
|   `-- main
|       |-- java
|       |   `-- org
|       |       `-- apache
|       |           `-- jmeter
|       |               |-- ClassPathHacker.java
|       |               `-- JMeterMojo.java
|       `-- resources
|           |-- lib
|              |-- ApacheJMeter.jar
|              `-- ApacheJMeter_functions.jar
...

4

Create a simple skript to generate local maven dependencies for you

I created a simple file depend.bash. Just use a vim or other editor you have, and paste there the following:

 #!/bin/bash

files=`ls $1 -All|awk '{print $8}'`

i=0;

for f in $files
do

        echo "<dependency>"
	    echo "<groupid>d_$i</groupid>"
	    echo "<artifactid>d_$i</artifactid>"
	    echo "<version>d_$i</version>"
	    echo "<scope>system</scope>"
	    echo "<systempath>$1/$f</systempath>"
        echo "</dependency>"

i=$(($i+1));

done

 
Then, type in console chmod a+x depend.bash

5

Generate your dependencies into some text file and copy-paste in your pom.xml

./depend.bash src/main/resource/lib > tmp.txt

in your pom.xml there is an

<dependencies>
</dependencies>

scope, put the generated content inside that scope. You will have something like that:

<dependency>
<groupid>d_0</groupid>
<artifactid>d_0</artifactid>
<version>d_0</version>
<scope>system</scope>
<systempath>src/main/resource/lib/ApacheJMeter_components.jar</systempath>
</dependency>
<dependency>
<groupid>d_1</groupid>
<artifactid>d_1</artifactid>
<version>d_1</version>
<scope>system</scope>
<systempath>src/main/resource/lib/ApacheJMeter_functions.jar</systempath>
</dependency>

6

Provide ‘standard’ maven dependencies if you need to

Now you can use all the custom jars. To work with the project you might need additional settings or dependencies inside the pom. But that’s what maven was done for ;)

7

Compile!

You can now use maven clean install or something like that. You can use your project now and there is time to both develop and properly deploy the jars into maven.

Be Sociable, Share!
 

LEARN MORE (amazon bookstore)

TAGS

SOCIAL
Be Sociable, Share!


2 Responses to “Creating new maven2 project with plenty of custom jars”

  1. Matthew Fitzpatrick says:

    Thanks for that neokrates, works like a charm!   -Matt.

  2. Sarah Bird says:

    Thanks for this. I needed to make systempath systemPath for it to work. Appreciate the post.

Leave a Reply