Search code examples
ivy

Command to get paths to jars for ivy-managed dependencies


Is there a way to get the paths to the JAR files for ivy-managed dependencies so that a program can be called from the command line and the JAR files are all correctly included in the class path.

Some thing like (for Linux bash and similar scripts):

java -cp `ivyget.sh ivy.xml`

which could then expand to

java -cp "/path/to/first.jar:/path/to/second.jar"

Is something like this possible?


Solution

  • Ivy can be used in a standalone fashion, see the documentation. I have included an example below.

    While this is a nifty feature I prefer to just use ivy for downloading. If your java program has options (eg -option1 or --option2) then ivy is not capable of properly parsing the command-line.

    Example

    The following project is used to manage a database schema using liquibase

    ├── changesets
    │   └── scottTiger.xml
    ├── ivy.xml
    ├── liquibase.properties
    └── liquibase.sh
    

    The project is executed by running the shell script

    ./liquibase.sh update
    

    liquibase.sh

    #!/bin/bash
    java -jar $HOME/.ant/lib/ivy.jar \
         -error \
         -ivy ivy.xml \
         -main liquibase.integration.commandline.Main \
         -args $@
    

    ivy.xml

    The ivy file pulls in the liquibase and H2 database projects as dependencies

    <ivy-module version="2.0">
        <info organisation="com.myspotontheweb" module="demo"/>
        <dependencies>
            <dependency org="org.liquibase" name="liquibase-core" rev="latest.release" conf="default"/>
            <dependency org="com.h2database" name="h2" rev="latest.release" conf="default"/>
        </dependencies>
    </ivy-module>
    

    liquibase.properties

    Run-time configuration file, where the DB type is set to H2.

    url=jdbc:h2:./db/scottTiger
    driver=org.h2.Driver
    username=user
    password=pass
    changeLogFile=changesets/scottTiger.xml
    logLevel=info