Creating new maven2 project with plenty of custom jars

By neokrates, written on April 7, 2008

article

Rate it
  • 1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 4 out of 5)
    Loading ... Loading ...
Ad
Poll
  • What build management tool does your project or firm use?

    • Buildr (47%, 15 Votes)
    • Maven 2 (6%, 2 Votes)
    • Ant (6%, 2 Votes)
    • Maven (3%, 1 Votes)
    • Maven 3 (3%, 1 Votes)
    • Ivy (0%, 0 Votes)
    • GAnt (0%, 0 Votes)
    • Gradle (0%, 0 Votes)
    • Rake (0%, 0 Votes)
    • Raven (0%, 0 Votes)
    • Any Make Type Tool (0%, 0 Votes)

    Total Voters: 32

    Vote

    Loading ... Loading ...
Feeds:
  • bodytext bodytext bodytext
Most popular search terms:

Problem: too many very custom jars

[v] Usecase #1. You want to create Maven2 project but the jar files you depend on are custom or really hard to obtain from repositories.
[v] Usecase #2. You try to port the large java project ASAP into maven2.
[v] 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.

 

LEARN MORE (amazon bookstore)

TAGS

RELATED
Pages
Posts
    nope :(

SOCIAL
Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • BlinkList
  • Blogosphere News
  • E-mail this story to a friend!
  • Furl
  • LinkArena
  • Live
  • MisterWong
  • Print this article!
  • StumbleUpon
  • Technorati
  • Webnews.de
  • YahooMyWeb

INCOMING SEARCH TERMS


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.

    Like or Dislike: Thumb up 0 Thumb down 1

  2. Sarah Bird says:

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

    Like or Dislike: Thumb up 0 Thumb down 0

Leave a Reply