views:

1467

answers:

2

Hello,

I am using maven-assembly plugin to create a jar of my application, including its dependencies as follows:

<assembly>
    <id>macosx</id>
    <formats>
       <format>tar.gz</format>
       <format>dir</format>
    </formats>
    <dependencySets>
        <dependencySet>
            <includes>
                <include>*:jar</include>
            </includes>
            <outputDirectory>lib</outputDirectory>
        </dependencySet>
    </dependencySets>
</assembly>

(I omitted some other stuff that is not related to the question)

So far this has worked fine because it creates a lib directory with all dependencies. However, I recently added a new dependency whose scope is system, and it does not copy it to the lib output directory. i must be missing something basic here, so I call for help.

The dependency that I just added is:

<dependency>
  <groupId>sourceforge.jchart2d</groupId>
  <artifactId>jchart2d</artifactId>
  <version>3.1.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}/external/jchart2d-3.1.0.jar</systemPath>
</dependency>

The only way I was able to include this dependency was by adding the following to the assembly element:

<files>
    <file>
        <source>external/jchart2d-3.1.0.jar</source>
        <outputDirectory>lib</outputDirectory>
    </file>
</files>

However, this forces me to change the pom and the assembly file whenever this jar is renamed, if ever. Also, it seems just wrong.

I have tried with <scope>runtime</scope> in the dependencySets and <include>sourceforge.jchart2d:jchart2d</include> with no luck.

So how do you include a system scoped jar to your assembly file in maven 2?

Thanks a lot

+3  A: 

I'm not surprised that system scope dependencies are not added (after all, dependencies with a system scope must be explicitly provided by definition). Actually, if you really don't want to put that dependency in your local repository (for example because you want to distribute it as part of your project), this is what I would do:

  • I would put the dependency in a "file system repository" local to the project.
  • I would declare that repository in my pom.xml like this:

    <repositories>
      <repository>
        <id>my</id>
        <url>file://${basedir}/my-repo</url>
      </repository>
    </repositories>
    
  • I would just declare the artifact without the system scope, this is just a source of troubles:

    <dependency>
      <groupId>sourceforge.jchart2d</groupId>
      <artifactId>jchart2d</artifactId>
      <version>3.1.0</version>
    </dependency>
    

I'm not 100% sure this will suit your needs but I think it's a better solution than using the system scope.

Update: I should have mentioned that in my original answer and I'm fixing it now. To install a third party library in the file-based repository, use install:install-file with the localRepositoryPath parameter:

mvn install:install-file -Dfile=<path-to-file> \
                         -DgroupId=<myGroup> \
                         -DartifactId=<myArtifactId> \
                         -Dversion=<myVersion> \
                         -Dpackaging=<myPackaging> \
                         -DlocalRepositoryPath=<path-to-my-repo>

You can paste this as is in a *nix shell. On windows, remove the "\" and put everything on a single line.

Pascal Thivent
Excellent idea, I will try it.
YuppieNetworking
It did work, but it was not a trivial task to install the file in a local repo different than $HOME/.m2/repository.I had to do a mvn install:install-file [...install-file options...] -Dmaven.repo.local=path_to_my_local_repo which forced maven to download its most basic plugins. Could you suggest how to install files to this local repo?
YuppieNetworking
I should have mentioned that :) I would have installed the artifact in the local repo (`~/.m2/repository`) with `install:install-file` and then moved the directory tree to `${basedir}/my-repo`. I'd just delete unwanted stuff from `${basedir}/my-repo` in your case.
Pascal Thivent
Yes, I eventually did that... it's not pretty, but it works. Thanks a lot.
YuppieNetworking
+4  A: 

Btw you can automate it and make it a part of your maven build. The following will install your jar into your local repository before compilation:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-install-plugin</artifactId>
            <executions>
                <execution>
                    <id>hack-binary</id>
                    <phase>validate</phase>
                    <configuration>
                        <file>${basedir}/lib/your-lib.jar</file>
                        <repositoryLayout>default</repositoryLayout>
                        <groupId>your-group</groupId>
                        <artifactId>your-artifact</artifactId>
                        <version>0.1</version>
                        <packaging>jar</packaging>
                        <generatePom>true</generatePom>
                    </configuration>
                    <goals>
                        <goal>install-file</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
alx
Doesn't it support installling multiple files at once?
Tuukka Mustonen