Home > front end >  How declare maven dependecny from URL?
How declare maven dependecny from URL?

Time:02-22

Maven has repositories and dependecies. Some plugins allow to use s3 or github as repository. But is there something to use direct URL? Like:

            <dependency>
                <groupId>my-group-id</groupId>
                <artifactId>artifact-id</artifactId>
                <version>some-version</version>
                <url>https:someurl</url>
            </dependency>

May be define URL and dependency info (group and artifact) in properties and for it installed during some maven phase.

Is it possible?

CodePudding user response:

No.

Maven resolves dependencies from Maven repositories. So you cannot just add a JAR that is present at some URL. The JAR has to come from a Maven repository and (if it is not MavenCentral or already specified in your settings.xml or POM), you need to add it.

CodePudding user response:

Yes this is officially supported but not in as convenient a mechanism as you perhaps desire.

Maven resolves dependencies against your local repository. If it can't find them there, it then tries to resolve them against any remote repositories you have configured.

Therefore, if you manually install the jar into your local repository, Maven will find it and use it.

To do this you must:

Download the jar

You have to do this yourself manually or automate the process e.g. using curl or wget

Specify the Maven Install Plugin version in your pom

You are going to be relying on the Maven Install Plugin so you should install you specify the version in your pom.xml, rather than relying on the default version.

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-install-plugin</artifactId>
                <version>2.5.2</version>
            </plugin>
        </plugins>
    </build>

Install the jar to your local repo

If the jar has been built with Maven, then it will include a pom.xml inside it. If you are unsure, open the jar with any archive manager and look inside the directory /META-INF/maven. You should find another directory named after the group ID, then another for the artifact ID, and then finally the pom.xml itself.

If the pom.xml is included, you can just run this and the tool will read the pom.xml to find the group ID and artifact ID so it can be placed in the right location in your repository:

mvn install:install-file -Dfile=/path/to/downloaded.jar

If the pom.xml is not included, you will need to specify all of this information yourself. It does not matter what you choose but you should choose something that isn't going to confuse you later:

mvn install:install-file -Dfile=/path/to/downloaded.jar -DgroupId=my-group-id -DartifactId=artifact-id -Dversion=some-version -Dpackaging=jar

Update your pom.xml

You can now just reference the dependency as normal in your pom.xml:

            <dependency>
                <groupId>my-group-id</groupId>
                <artifactId>artifact-id</artifactId>
                <version>some-version</version>
            </dependency>
  • Related